using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; namespace AyaNova.Api.Controllers { /// /// /// [ApiController] [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/dashboard-view")] [Produces("application/json")] [Authorize] public class DashboardViewController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public DashboardViewController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get DashboardView object for current User /// There is always one for each user /// /// Dashboard view [HttpGet()] public async Task GetDashboardView() { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); //Instantiate the business object handler DashboardViewBiz biz = DashboardViewBiz.GetBiz(ct, HttpContext); //user always has full access to their own dashboard view and can only access their own through api so no need to check // if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) // return StatusCode(403, new ApiNotAuthorizedResponse()); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); var o = await biz.GetAsync(); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return Ok(ApiOkResponse.Response(o)); } /// /// Update logged in User's Dashboard view /// /// /// [HttpPut()] public async Task PutDashboardView([FromBody] string theView) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); //Instantiate the business object handler DashboardViewBiz biz = DashboardViewBiz.GetBiz(ct, HttpContext); var o = await biz.GetAsync(); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); //user always has full access to their own dashboard view and can only access their own through api so no need to check // if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) // return StatusCode(403, new ApiNotAuthorizedResponse()); try { if (!await biz.PutAsync(o, theView)) return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) { if (!await biz.ExistsAsync()) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); else return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); } return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); } //------------ }//eoc }//eons