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(); } } }