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" />
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step.Model.DatabaseModels
{
[Table("favorite_user_softkey")]
public class FavoriteUserSoftkeyModel
{
[Key, Column("user_softkey_id", Order = 0)]
public int SoftkeyId { get; set; }
[Key, Column("user_id", Order = 1)]
public int UserId { get; set; }
}
}
+1
View File
@@ -69,6 +69,7 @@
<Compile Include="ConfigModels\NcConfigModel.cs" />
<Compile Include="ConfigModels\UserSoftKeyConfigModel.cs" />
<Compile Include="Constants.cs" />
<Compile Include="DatabaseModels\FavoriteUserSoftKeyModel.cs" />
<Compile Include="DatabaseModels\FunctionAccessModel.cs" />
<Compile Include="ConfigModels\MessageModel.cs" />
<Compile Include="DatabaseModels\MachineModel.cs" />
@@ -0,0 +1,58 @@
using Step.Database.Controllers;
using Step.Model.DatabaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using static Step.Model.Constants;
namespace Step.Controllers.WebApi
{
[RoutePrefix("api/user_softkey")]
public class FavoriteUserSoftkeyController : ApiController
{
[Route("favorite"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult GetFavoriteSoftkeys()
{
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
int userIdInt = Convert.ToInt32(userId.Value);
using (FavoriteUserSoftkeysController controller = new FavoriteUserSoftkeysController())
{
return Ok(
controller.GetUserFavoriteSoftkeys(userIdInt)
);
}
}
[Route("favorite"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)]
public IHttpActionResult PutFavoriteSoftkeys(List<uint> favoriteSoftkeyIds)
{
var identity = User.Identity as ClaimsIdentity;
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
int intUserId = Convert.ToInt32(userId.Value);
using (FavoriteUserSoftkeysController controller = new FavoriteUserSoftkeysController())
{
// Delete saved softkey
controller.DeleteUserSoftkeyModel(intUserId);
List<FavoriteUserSoftkeyModel> favorite = new List<FavoriteUserSoftkeyModel>();
if (favoriteSoftkeyIds == null && favoriteSoftkeyIds.Count > 0)
{
// Insert new list of id
favorite = controller.InsertUserSoftkeyModel(favoriteSoftkeyIds, intUserId);
}
return Ok(favorite);
}
}
}
}
+1
View File
@@ -172,6 +172,7 @@
<Compile Include="Controllers\SignalR\NcHub.cs" />
<Compile Include="Controllers\WebApi\AuthorizationController.cs" />
<Compile Include="Controllers\WebApi\ConfigurationController.cs" />
<Compile Include="Controllers\WebApi\FavoriteUserSoftKeyController.cs" />
<Compile Include="Controllers\WebApi\LanguageController.cs" />
<Compile Include="Controllers\WebApi\MaintenanceController.cs" />
<Compile Include="Controllers\WebApi\ToolTableController.cs" />