Added favorite softkey API

This commit is contained in:
Lucio Maranta
2018-07-12 11:41:37 +02:00
parent 1d2be186bf
commit 7e9d0dfe97
10 changed files with 164 additions and 7 deletions
@@ -0,0 +1,65 @@
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<FavoriteUserSoftkeyModel> GetUserFavoriteSoftkeys(int userId)
{
return dbCtx.
FavoriteUserSoftkeys
.Where(x => x.UserId == userId)
.ToList();
}
public List<FavoriteUserSoftkeyModel> 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 softKeys;
}
public void DeleteUserSoftkeyModel(int userId)
{
// Get user favorite softkeys
List<FavoriteUserSoftkeyModel> softKeys = GetUserFavoriteSoftkeys(userId);
foreach(FavoriteUserSoftkeyModel sofkey in softKeys)
{
// Delete
dbCtx.FavoriteUserSoftkeys.Remove(sofkey);
}
// Save database context
dbCtx.SaveChanges();
}
}
}