Files
cms_thermo_active/Thermo.Active/Controllers/WebApi/FavoriteUserSoftKeyController.cs
T
2020-06-19 19:28:07 +02:00

55 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web.Http;
using Thermo.Active.Database.Controllers;
using Thermo.Active.Model.DTOModels;
using static Thermo.Active.Model.Constants;
namespace Thermo.Active.Controllers.WebApi
{
[RoutePrefix("api/user_softkey")]
public class FavoriteUserSoftkeyController : ApiController
{
[Route("favorite"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.READ)]
public IHttpActionResult GetFavoriteSoftkeys()
{
var identity = User.Identity as ClaimsIdentity;
// Find user id from the bearer token
var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY);
int userIdInt = Convert.ToInt32(userId.Value);
using (UserSoftkeysController controller = new UserSoftkeysController())
{
return Ok(
controller.GetUserFavoriteSoftkeys(userIdInt)
);
}
}
[Route("favorite"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.GENERAL, Action = ACTIONS.WRITE)]
public IHttpActionResult PutFavoriteSoftkeys(List<uint> favoriteSoftkeyIds)
{
var identity = User.Identity as ClaimsIdentity;
var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY);
int intUserId = Convert.ToInt32(userId.Value);
using (UserSoftkeysController controller = new UserSoftkeysController())
{
// Delete saved softkey
controller.DeleteUserSoftkeyModel(intUserId);
List<DTOUserSoftKeyConfigModel> favorite = new List<DTOUserSoftKeyConfigModel>();
if (favoriteSoftkeyIds != null && favoriteSoftkeyIds.Count > 0)
{
// Insert new list of id
favorite = controller.InsertUserSoftkeyModel(favoriteSoftkeyIds, intUserId);
}
return Ok(favorite);
}
}
}
}