114 lines
3.7 KiB
C#
114 lines
3.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,
|
|
Type = softKey.Type,
|
|
SubKeys = tmpSubKey
|
|
});
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
public List<FavoriteUserSoftkeyModel> FindUserFavoriteSoftkeys(int userId)
|
|
{
|
|
return dbCtx.
|
|
FavoriteUserSoftkeys
|
|
.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
|
|
});
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
}
|
|
}
|