using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using AyaNova.DataList; using System.Threading.Tasks; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace AyaNova.Api.Controllers { [ApiController] [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/data-list")] [Produces("application/json")] [Authorize] public class DataListController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public DataListController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get list of data for selection / viewing /// /// Authorization varies list by list, will return 403 - Not Authorized if user has insufficient role /// /// /// List key, Paging, filtering and sorting options /// Collection with paging data // [HttpPost("List", Name = nameof(List))] [HttpPost] public async Task List([FromBody] ListOptions listOptions) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (listOptions.Limit == null || listOptions.Limit < 1) { listOptions.Limit = ListOptions.DefaultLimit; } if (listOptions.Offset == null) { listOptions.Offset = 0; } if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); var UserRoles = UserRolesFromContext.Roles(HttpContext.Items); var UserId = UserIdFromContext.Id(HttpContext.Items); try { ApiDataListResponse r = await DataListFetcher.GetResponseAsync(listOptions.DataListKey, ct, listOptions, UserRoles, log, UserId); return Ok(r); } catch (System.UnauthorizedAccessException) { return StatusCode(403, new ApiNotAuthorizedResponse()); } catch (System.ArgumentOutOfRangeException e) { return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, null, e.Message)); } } /// /// List of all DataList keys available /// /// List of strings [HttpGet("listkeys")] public ActionResult GetDataListKeys() { //NOTE: not used by AyaNova Client, convenience method for developers api usage if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames())); } /// /// List of all fields for data list key specified /// /// List of DataListFieldDefinition [HttpGet("listfields")] public ActionResult GetDataListFields([FromQuery] string DataListKey) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); var DataList = DataListFactory.GetAyaDataList(DataListKey); //was the name not found as a list? if (DataList == null) { return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, null, $"DataList \"{DataListKey}\" specified does not exist")); } var ExternalOnly = DataList.FieldDefinitions.Where(z => z.IsMeta == false); return Ok(ApiOkResponse.Response(ExternalOnly)); } }//eoc }//ens