Added favorite softkey API

This commit is contained in:
Lucio Maranta
2018-07-12 11:41:37 +02:00
parent 1d2be186bf
commit 7e9d0dfe97
10 changed files with 164 additions and 7 deletions
@@ -0,0 +1,65 @@
using Step.Model.DatabaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Step.Database.Controllers
{
public class FavoriteUserSoftkeysController : IDisposable
{
private DatabaseContext dbCtx;
public FavoriteUserSoftkeysController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
public List<FavoriteUserSoftkeyModel> GetUserFavoriteSoftkeys(int userId)
{
return dbCtx.
FavoriteUserSoftkeys
.Where(x => x.UserId == userId)
.ToList();
}
public List<FavoriteUserSoftkeyModel> InsertUserSoftkeyModel(List<uint> softKeyIds, int userId)
{
List<FavoriteUserSoftkeyModel> softKeys = new List<FavoriteUserSoftkeyModel>();
// Create new list with db models
foreach (int softKeyId in softKeyIds)
{
softKeys.Add(new FavoriteUserSoftkeyModel()
{
UserId = userId,
SoftkeyId = softKeyId
});
}
// Add list to database
dbCtx.FavoriteUserSoftkeys.AddRange(softKeys);
dbCtx.SaveChanges();
return softKeys;
}
public void DeleteUserSoftkeyModel(int userId)
{
// Get user favorite softkeys
List<FavoriteUserSoftkeyModel> softKeys = GetUserFavoriteSoftkeys(userId);
foreach(FavoriteUserSoftkeyModel sofkey in softKeys)
{
// Delete
dbCtx.FavoriteUserSoftkeys.Remove(sofkey);
}
// Save database context
dbCtx.SaveChanges();
}
}
}
+1
View File
@@ -28,6 +28,7 @@ namespace Step.Database
public DbSet<PerformedMaintenanceModel> PerformedMaintenances { get; set; }
public DbSet<MaintenanceNoteModel> MaintenancesNotes { get; set; }
public DbSet<MaintenanceFileModel> MaintenanceFiles { get; set; }
public DbSet<FavoriteUserSoftkeyModel> FavoriteUserSoftkeys { get; set; }
// Create migration string
public static string CONNECTION_STRING = "Server = " + "localhost" + "; Database=" + DATABASE_NAME +";Uid="+ DATABASE_USER +";Pwd="+ DATABASE_PWD +";";
@@ -13,7 +13,7 @@ namespace Step.Database.Migrations
string IMigrationMetadata.Id
{
get { return "201806220920193_InitMigration"; }
get { return "201807120908403_InitMigration"; }
}
string IMigrationMetadata.Source
@@ -7,6 +7,15 @@ namespace Step.Database.Migrations
{
public override void Up()
{
CreateTable(
"dbo.favorite_user_softkey",
c => new
{
user_softkey_id = c.Int(nullable: false),
user_id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.user_softkey_id, t.user_id });
CreateTable(
"dbo.function_access",
c => new
@@ -181,6 +190,7 @@ namespace Step.Database.Migrations
DropTable("dbo.machine_user");
DropTable("dbo.machine");
DropTable("dbo.function_access");
DropTable("dbo.favorite_user_softkey");
}
}
}
+6 -5
View File
@@ -70,6 +70,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\FavoriteUserSoftkeysController.cs" />
<Compile Include="Controllers\FunctionsAccessController.cs" />
<Compile Include="Controllers\MachinesController.cs" />
<Compile Include="Controllers\MaintenancesController.cs" />
@@ -77,9 +78,9 @@
<Compile Include="Controllers\UsersController.cs" />
<Compile Include="Controllers\MachinesUsersController.cs" />
<Compile Include="DatabaseContext.cs" />
<Compile Include="Migrations\201806220920193_InitMigration.cs" />
<Compile Include="Migrations\201806220920193_InitMigration.Designer.cs">
<DependentUpon>201806220920193_InitMigration.cs</DependentUpon>
<Compile Include="Migrations\201807120908403_InitMigration.cs" />
<Compile Include="Migrations\201807120908403_InitMigration.Designer.cs">
<DependentUpon>201807120908403_InitMigration.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -115,8 +116,8 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Migrations\201806220920193_InitMigration.resx">
<DependentUpon>201806220920193_InitMigration.cs</DependentUpon>
<EmbeddedResource Include="Migrations\201807120908403_InitMigration.resx">
<DependentUpon>201807120908403_InitMigration.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />