From 9f5dadfc4972c0d9e660b6068ffd6f5009dcfc07 Mon Sep 17 00:00:00 2001 From: Lucio Maranta Date: Thu, 12 Jul 2018 15:54:46 +0200 Subject: [PATCH] Added softkey data in the Favorite softkey API --- .../FavoriteUserSoftkeysController.cs | 65 ---------- .../Controllers/UserSoftkeysController.cs | 113 ++++++++++++++++++ Step.Database/Step.Database.csproj | 2 +- .../WebApi/ConfigurationController.cs | 46 +++---- .../WebApi/FavoriteUserSoftKeyController.cs | 7 +- 5 files changed, 136 insertions(+), 97 deletions(-) delete mode 100644 Step.Database/Controllers/FavoriteUserSoftkeysController.cs create mode 100644 Step.Database/Controllers/UserSoftkeysController.cs diff --git a/Step.Database/Controllers/FavoriteUserSoftkeysController.cs b/Step.Database/Controllers/FavoriteUserSoftkeysController.cs deleted file mode 100644 index 11f5e6c4..00000000 --- a/Step.Database/Controllers/FavoriteUserSoftkeysController.cs +++ /dev/null @@ -1,65 +0,0 @@ -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 GetUserFavoriteSoftkeys(int userId) - { - return dbCtx. - FavoriteUserSoftkeys - .Where(x => x.UserId == userId) - .ToList(); - } - - public List InsertUserSoftkeyModel(List softKeyIds, int userId) - { - List softKeys = new List(); - // 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 softKeys = GetUserFavoriteSoftkeys(userId); - - foreach(FavoriteUserSoftkeyModel sofkey in softKeys) - { - // Delete - dbCtx.FavoriteUserSoftkeys.Remove(sofkey); - } - // Save database context - dbCtx.SaveChanges(); - } - } -} diff --git a/Step.Database/Controllers/UserSoftkeysController.cs b/Step.Database/Controllers/UserSoftkeysController.cs new file mode 100644 index 00000000..efa333e8 --- /dev/null +++ b/Step.Database/Controllers/UserSoftkeysController.cs @@ -0,0 +1,113 @@ +using Step.Model.DatabaseModels; +using Step.Model.DTOModels; +using System; +using System.Collections.Generic; +using System.Linq; +using static Step.Model.Constants; +using static Step.Config.ServerConfig; + +namespace Step.Database.Controllers +{ + public class UserSoftkeysController : IDisposable + { + private DatabaseContext dbCtx; + + public UserSoftkeysController() + { + // Initialize database context + dbCtx = new DatabaseContext(); + } + + public void Dispose() + { + // Clear database context + dbCtx.Dispose(); + } + + public List GetUserSoftkeyConfig() + { + List config = new List(); + + foreach (var softKey in SoftKeysConfig) + { + // Create subkeys dictionary for group + Dictionary tmpSubKey = new Dictionary(); + if (softKey.Type == SOFTKEY_TYPE.GROUP && softKey.SubKeys != null) + { + tmpSubKey = new Dictionary(); + foreach (var subKey in softKey.SubKeys) + { + tmpSubKey.Add(subKey.Id, subKey.Text); + } + } + // Add to the category the new softkey + config.Add(new DTOUserSoftKeyConfigModel() + { + Id = softKey.Id, + Category = softKey.Category, + OperatorConfirmationNeeded = softKey.OperatorConfirmationNeeded, + Type = softKey.Type, + SubKeys = tmpSubKey + }); + } + + return config; + } + + public List FindUserFavoriteSoftkeys(int userId) + { + return dbCtx. + FavoriteUserSoftkeys + .Where(x => x.UserId == userId) + .ToList(); + } + + public List GetUserFavoriteSoftkeys(int userId) + { + // Find user softkey stored in the database + List favoriteKey = FindUserFavoriteSoftkeys(userId); + // Get config + List userSoftkey = GetUserSoftkeyConfig(); + + return userSoftkey + .Where(x => + favoriteKey.Any(y => y.SoftkeyId == x.Id + )) + .ToList(); + } + + public List InsertUserSoftkeyModel(List softKeyIds, int userId) + { + List softKeys = new List(); + // 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 GetUserFavoriteSoftkeys(userId); + } + + public void DeleteUserSoftkeyModel(int userId) + { + // Get user favorite softkeys + List softKeys = FindUserFavoriteSoftkeys(userId); + + foreach(FavoriteUserSoftkeyModel sofkey in softKeys) + { + // Delete + dbCtx.FavoriteUserSoftkeys.Remove(sofkey); + } + // Save database context + dbCtx.SaveChanges(); + } + } +} diff --git a/Step.Database/Step.Database.csproj b/Step.Database/Step.Database.csproj index 6b272a17..e70b1a2d 100644 --- a/Step.Database/Step.Database.csproj +++ b/Step.Database/Step.Database.csproj @@ -70,7 +70,7 @@ - + diff --git a/Step/Controllers/WebApi/ConfigurationController.cs b/Step/Controllers/WebApi/ConfigurationController.cs index 9a116907..dc6c6044 100644 --- a/Step/Controllers/WebApi/ConfigurationController.cs +++ b/Step/Controllers/WebApi/ConfigurationController.cs @@ -1,4 +1,5 @@ -using Step.Model.DTOModels; +using Step.Database.Controllers; +using Step.Model.DTOModels; using Step.NC; using System.Collections.Generic; using System.Linq; @@ -53,38 +54,27 @@ namespace Step.Controllers.WebApi [Route("softKeys"), HttpGet] public IHttpActionResult GetUserSoftKeysConfig() { - Dictionary> output = new Dictionary>(); - foreach (var softKey in SoftKeysConfig) + Dictionary> config = new Dictionary>(); + + using (UserSoftkeysController controller = new UserSoftkeysController()) { - // Check if category exists - if (!output.ContainsKey(softKey.Category)) + List softkeys = controller.GetUserSoftkeyConfig(); + // Organize by category + foreach (var softkey in softkeys) { - // Add category if not exits - output.Add(softKey.Category, new List()); - } - - // Create subkeys dictionary for group - Dictionary tmpSubKey = new Dictionary(); - if (softKey.Type == SOFTKEY_TYPE.GROUP && softKey.SubKeys != null) - { - tmpSubKey = new Dictionary(); - foreach (var subKey in softKey.SubKeys) + // Check if category exists + if (!config.ContainsKey(softkey.Category)) { - tmpSubKey.Add(subKey.Id, subKey.Text); + // Add category if not exits + config.Add(softkey.Category, new List()); } - } - // Add to the category the new softkey - output[softKey.Category].Add(new DTOUserSoftKeyConfigModel() - { - Id = softKey.Id, - Category = softKey.Category, - OperatorConfirmationNeeded = softKey.OperatorConfirmationNeeded, - Type = softKey.Type, - SubKeys = tmpSubKey - }); - } - return Ok(output); + // Add to the category the new softkey + config[softkey.Category].Add(softkey); + } + } + + return Ok(config); } [Route("heads"), HttpGet] diff --git a/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs b/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs index bd22f812..2d56b122 100644 --- a/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs +++ b/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs @@ -1,5 +1,6 @@ using Step.Database.Controllers; using Step.Model.DatabaseModels; +using Step.Model.DTOModels; using System; using System.Collections.Generic; using System.Linq; @@ -22,7 +23,7 @@ namespace Step.Controllers.WebApi // 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()) + using (UserSoftkeysController controller = new UserSoftkeysController()) { return Ok( controller.GetUserFavoriteSoftkeys(userIdInt) @@ -38,11 +39,11 @@ namespace Step.Controllers.WebApi var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault(); int intUserId = Convert.ToInt32(userId.Value); - using (FavoriteUserSoftkeysController controller = new FavoriteUserSoftkeysController()) + using (UserSoftkeysController controller = new UserSoftkeysController()) { // Delete saved softkey controller.DeleteUserSoftkeyModel(intUserId); - List favorite = new List(); + List favorite = new List(); if (favoriteSoftkeyIds != null && favoriteSoftkeyIds.Count > 0) { // Insert new list of id