Files
activestep/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs
2020-09-12 16:11:43 +02:00

58 lines
2.2 KiB
C#

using Step.Database.Controllers;
using Step.Model.DatabaseModels;
using Step.Model.DTOModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using static Step.Model.Constants;
namespace Step.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);
}
}
}
}