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.AspNetCore.JsonPatch; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; namespace AyaNova.Api.Controllers { /// /// /// [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/[controller]")] [Produces("application/json")] [Authorize] public class FormCustomController : Controller { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public FormCustomController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get form customizations for Client form display /// /// Required roles: /// Any /// /// /// /// /// A single FormCustom [HttpGet("{formkey}")] public async Task GetFormCustom([FromRoute] string formkey, [FromQuery] uint? concurrencyToken) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); //Instantiate the business object handler FormCustomBiz biz = FormCustomBiz.GetBiz(ct, HttpContext); //Just have to be authenticated for this one if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType)) return StatusCode(401, new ApiNotAuthorizedResponse()); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); var o = await biz.GetAsync(formkey); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); //If concurrency token specified then check if ours is newer if (concurrencyToken != null) { if (o.ConcurrencyToken != concurrencyToken) { //returns a code 304 (NOT MODIFIED) return StatusCode(304); } } return Ok(new ApiOkResponse(o)); } /// /// Get available fields for form specified /// Used to build UI for customizing a form /// /// Required roles: /// BizAdminFull only has rights to customize forms /// /// /// /// A single FormCustom [HttpGet("AvailableFields/{formkey}")] public ActionResult GetAvailableFields([FromRoute] string formkey) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.FormCustom)) return StatusCode(401, new ApiNotAuthorizedResponse()); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); if (FormAvailableFields.IsValidFormKey(formkey)) { return Ok(new ApiOkResponse(FormAvailableFields.FormFields(formkey))); } else { return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); } } // /// // /// Get FormCustom pick list // /// // /// Required roles: Any // /// // /// // /// List of public or owned data filters for listKey provided // [HttpGet("PickList", Name = nameof(FormCustomPickList))] // public async Task FormCustomPickList([FromQuery] string ListKey) // { // if (serverState.IsClosed) // return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); // if (!ModelState.IsValid) // return BadRequest(new ApiErrorResponse(ModelState)); // //Instantiate the business object handler // FormCustomBiz biz = FormCustomBiz.GetBiz(ct, HttpContext); // var l = await biz.GetPickListAsync(ListKey); // return Ok(new ApiOkResponse(l)); // } // /// // /// Put (update) FormCustom // /// // /// Required roles: // /// Any (public filter) or owned only (private filter) // /// // /// // /// // /// // /// // [HttpPut("{id}")] // public async Task PutFormCustom([FromRoute] long id, [FromBody] FormCustom inObj) // { // 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 // FormCustomBiz biz = FormCustomBiz.GetBiz(ct, HttpContext); // var o = await biz.GetNoLogAsync(id); // if (o == null) // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); // if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) // return StatusCode(401, new ApiNotAuthorizedResponse()); // try // { // if (!biz.Put(o, inObj)) // return BadRequest(new ApiErrorResponse(biz.Errors)); // } // catch (DbUpdateConcurrencyException) // { // if (!await biz.ExistsAsync(id)) // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); // else // return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); // } // return Ok(new ApiOkResponse(new { ConcurrencyToken = o.ConcurrencyToken })); // } // /// // /// Post FormCustom // /// // /// Required roles: // /// BizAdminFull, InventoryFull, TechFull // /// // /// // /// // [HttpPost] // public async Task PostFormCustom([FromBody] FormCustom inObj) // { // if (!serverState.IsOpen) // return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); // //Instantiate the business object handler // FormCustomBiz biz = FormCustomBiz.GetBiz(ct, HttpContext); // //If a user has change roles, or editOwnRoles then they can create, true is passed for isOwner since they are creating so by definition the owner // if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, biz.BizType)) // return StatusCode(401, new ApiNotAuthorizedResponse()); // if (!ModelState.IsValid) // return BadRequest(new ApiErrorResponse(ModelState)); // //Create and validate // FormCustom o = await biz.CreateAsync(inObj); // if (o == null) // return BadRequest(new ApiErrorResponse(biz.Errors)); // else // return CreatedAtAction("GetFormCustom", new { id = o.Id }, new ApiCreatedResponse(o)); // } // /// // /// Delete FormCustom // /// // /// Required roles: // /// Any if public otherwise creator only // /// // /// // /// // /// Ok // [HttpDelete("{id}")] // public async Task DeleteFormCustom([FromRoute] long id) // { // 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 // FormCustomBiz biz = FormCustomBiz.GetBiz(ct, HttpContext); // var o = await biz.GetNoLogAsync(id); // if (o == null) // return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); // if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, biz.BizType, o.OwnerId)) // return StatusCode(401, new ApiNotAuthorizedResponse()); // if (!biz.Delete(o)) // return BadRequest(new ApiErrorResponse(biz.Errors)); // return NoContent(); // } //------------ }//eoc }//eons