using CMS_CORE_Library.Models;
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 Thermo.Active.NC;
using Thermo.Active.Utils;
using static Thermo.Active.Model.Constants;
namespace Thermo.Active.Controllers.WebApi
{
[RoutePrefix("api/user_softkey")]
public class FavoriteUserSoftkeyController : ApiController
{
///
/// Oggetto adapter condiviso da WebAPI
///
protected static NcAdapter ncAdapter = new NcAdapter();
[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 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 favorite = new List();
if (favoriteSoftkeyIds != null && favoriteSoftkeyIds.Count > 0)
{
// Insert new list of id
favorite = controller.InsertUserSoftkeyModel(favoriteSoftkeyIds, intUserId);
}
return Ok(favorite);
}
}
[Route("keyboard"), HttpGet]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.READ)]
public IHttpActionResult GetKeyboardSoftkeys()
{
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.GetKeyboardFavoriteSoftkeys(userIdInt));
}
}
[Route("keyboard"), HttpPut]
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.GENERAL, Action = ACTIONS.WRITE)]
public IHttpActionResult setKeyboardSoftkey(int softkeyId, int position)
{
if (position > 2)
return BadRequest("position_error");
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);
// Try connection
CmsError libraryError = ncAdapter.Connect();
using (UserSoftkeysController controller = new UserSoftkeysController())
{
DTOKeyboardSoftKeyModel softkey = controller.SetKeyboardFavoriteSoftkeys(userIdInt, softkeyId, position);
libraryError=ncAdapter.WriteKeyboardStarSoftkey(softkey.IdStar1, softkey.IdStar2);
if (libraryError.IsError())
return BadRequest(libraryError.localizationKey);
return Ok(softkey);
}
}
}
}