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