using System; using System.Collections.Generic; using System.Linq; using Thermo.Active.Model.DatabaseModels; using Thermo.Active.Model.DTOModels; using static Thermo.Active.Config.ServerConfig; using static Thermo.Active.Model.Constants; namespace Thermo.Active.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, RefCallLabel = softKey.RefCallLabel, RefCallParam = softKey.RefCallParam, Type = softKey.Type, SubKeys = tmpSubKey }); } return config; } public List FindUserFavoriteSoftkeys(int userId) { return dbCtx. FavoriteUserSoftkeys .Where(x => x.UserId == userId) .ToList(); } public List FindkeyboardSoftkeys(int userId) { return dbCtx. KeyboardUserSoftkeys .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, Type = (int) SOFTKEY_PLACE.PADDLE }); } // 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(); } public DTOKeyboardSoftKeyModel GetKeyboardFavoriteSoftkeys(int userId) { // Find user softkey stored in the database List keyboardKey = FindkeyboardSoftkeys(userId); // Get config DTOKeyboardSoftKeyModel keybSoftkey = new DTOKeyboardSoftKeyModel(); if(keyboardKey.Count==0) { keybSoftkey.IdStar1 = 0; keybSoftkey.IdStar2 = 0; } else { keybSoftkey.IdStar1 = keyboardKey.FirstOrDefault().SoftkeyId1; keybSoftkey.IdStar2 = keyboardKey.FirstOrDefault().SoftkeyId2; } return keybSoftkey; } public DTOKeyboardSoftKeyModel GetKeyboardFavoriteSoftkeysLoggedUser() { List users = dbCtx .Sessions .Include("MachineUser") .Select(x => x.MachineUser) .GroupBy(x => x.MachineUserId) .Select(x => x.FirstOrDefault()) .ToList(); if (users.Count > 0) return GetKeyboardFavoriteSoftkeys(users[0].UserId); else return GetKeyboardFavoriteSoftkeys(-1); } public DTOKeyboardSoftKeyModel SetKeyboardFavoriteSoftkeys(int userId,int idSoftkey, int pos) { // Find user softkey stored in the database List keyboardKey = FindkeyboardSoftkeys(userId); if (keyboardKey.Count == 0) { KeyboardUserSoftKeyModel newModel = new KeyboardUserSoftKeyModel(); newModel.UserId = userId; if(pos==1) { newModel.SoftkeyId1 = idSoftkey; newModel.SoftkeyId2 = 0; } else if (pos == 2) { newModel.SoftkeyId1 = 0; newModel.SoftkeyId2 = idSoftkey; } dbCtx.KeyboardUserSoftkeys.Add(newModel); } else { KeyboardUserSoftKeyModel model = keyboardKey.FirstOrDefault(); if (pos == 1) model.SoftkeyId1 = idSoftkey; else if (pos == 2) model.SoftkeyId2 = idSoftkey; } dbCtx.SaveChanges(); return GetKeyboardFavoriteSoftkeys(userId); } } }