Files
Samuele E. Locatelli 496e38ab7d Added Backend
2020-11-09 15:05:01 +01:00

202 lines
6.7 KiB
C#

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<DTOUserSoftKeyConfigModel> GetUserSoftkeyConfig()
{
List<DTOUserSoftKeyConfigModel> config = new List<DTOUserSoftKeyConfigModel>();
foreach (var softKey in SoftKeysConfig)
{
// Create subkeys dictionary for group
Dictionary<int, string> tmpSubKey = new Dictionary<int, string>();
if (softKey.Type == SOFTKEY_TYPE.GROUP && softKey.SubKeys != null)
{
tmpSubKey = new Dictionary<int, string>();
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<FavoriteUserSoftkeyModel> FindUserFavoriteSoftkeys(int userId)
{
return dbCtx.
FavoriteUserSoftkeys
.Where(x => x.UserId == userId)
.ToList();
}
public List<KeyboardUserSoftKeyModel> FindkeyboardSoftkeys(int userId)
{
return dbCtx.
KeyboardUserSoftkeys
.Where(x => x.UserId == userId)
.ToList();
}
public List<DTOUserSoftKeyConfigModel> GetUserFavoriteSoftkeys(int userId)
{
// Find user softkey stored in the database
List<FavoriteUserSoftkeyModel> favoriteKey = FindUserFavoriteSoftkeys(userId);
// Get config
List<DTOUserSoftKeyConfigModel> userSoftkey = GetUserSoftkeyConfig();
return userSoftkey
.Where(x =>
favoriteKey.Any(y => y.SoftkeyId == x.Id
))
.ToList();
}
public List<DTOUserSoftKeyConfigModel> 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,
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<FavoriteUserSoftkeyModel> 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<KeyboardUserSoftKeyModel> 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<MachineUserModel> 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<KeyboardUserSoftKeyModel> 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);
}
}
}