using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; //using AyaNova.PickList; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace AyaNova.Api.Controllers { [ApiController] [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/[controller]")] [Produces("application/json")] [Authorize] public class PickListController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public PickListController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get picklist /// /// The AyaType object type to select from /// The query to filter the returned list by /// Include inactive objects in the returned list /// Filtered list [HttpGet("List")] public async Task GetList([FromQuery]AyaType ayaType, [FromQuery]string query, [FromQuery] bool inactive) { if (!serverState.IsOpen) { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); } //Instantiate the business object handler PickListBiz biz = PickListBiz.GetBiz(ct, HttpContext); // //NOTE: This is the first check and often the only check but in some cases with some objects this will also need to check biz object rules // if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) // return StatusCode(403, new ApiNotAuthorizedResponse()); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); var UserRoles = UserRolesFromContext.Roles(HttpContext.Items); var o = await biz.GetPickListAsync(ayaType, query, inactive, UserRoles); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return Ok(ApiOkResponse.Response(o, true)); } /// /// List of all PickList templates /// /// List of strings [HttpGet("TemplateList")] public ActionResult GetTemplateList() { if (!serverState.IsOpen) { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); } //Instantiate the business object handler PickListBiz biz = PickListBiz.GetBiz(ct, HttpContext); long TranslationId = UserTranslationIdFromContext.Id(HttpContext.Items); var o = biz.GetListOfAllPickListTypes(TranslationId); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return Ok(ApiOkResponse.Response(o, true)); } /// /// POST (replace) Pick List template /// /// /// /// [HttpPost("Template/{ayatype}")] public async Task ReplacePickListTemplate([FromRoute] AyaType ayaType, [FromBody] string template) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); //Instantiate the business object handler PickListBiz biz = PickListBiz.GetBiz(ct, HttpContext); // var o = await biz.GetAsync(ayaType, false); // if (o == null) // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) return StatusCode(403, new ApiNotAuthorizedResponse()); try { if (!await biz.ReplaceAsync(ayaType, template)) return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) { return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); } return NoContent(); } /// /// Delete customized template /// (revert to default) /// /// /// Ok [HttpDelete("Template/{ayatype}")] public async Task DeletePickListTemplate([FromRoute] AyaType ayaType) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); //Instantiate the business object handler PickListBiz biz = PickListBiz.GetBiz(ct, HttpContext); if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType)) return StatusCode(403, new ApiNotAuthorizedResponse()); if (!await biz.DeleteAsync(ayaType)) return BadRequest(new ApiErrorResponse(biz.Errors)); return NoContent(); } }//eoc }//ens