118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
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 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 DataListController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<DataListController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public DataListController(AyContext dbcontext, ILogger<DataListController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get list of data for selection / viewing
|
|
///
|
|
/// Authorization varies list by list, will return 403 - Not Authorized if user has insufficient role
|
|
///
|
|
/// </summary>
|
|
/// <param name="listOptions">List key, Paging, filtering and sorting options</param>
|
|
/// <returns>Collection with paging data</returns>
|
|
[HttpPost("List", Name = nameof(List))]
|
|
public async Task<IActionResult> List([FromBody] ListOptions listOptions)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(new ApiErrorResponse(ModelState));
|
|
|
|
var UserId = UserIdFromContext.Id(HttpContext.Items);
|
|
var UserRoles = UserRolesFromContext.Roles(HttpContext.Items);
|
|
|
|
try
|
|
{
|
|
ApiDataListResponse r = await DataListFetcher.GetResponseAsync(listOptions.DataListKey, ct, Url, nameof(List), listOptions, UserId, UserRoles);
|
|
return Ok(r);
|
|
}
|
|
catch (System.UnauthorizedAccessException)
|
|
{
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
}
|
|
catch (System.ArgumentOutOfRangeException e)
|
|
{
|
|
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "DataListKey", e.Message));
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// List of all DataList keys available
|
|
/// </summary>
|
|
/// <returns>List of strings</returns>
|
|
[HttpGet("ListKeys")]
|
|
public ActionResult GetDataListKeys()
|
|
{
|
|
if (!serverState.IsOpen)
|
|
{
|
|
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
|
|
}
|
|
|
|
return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames(), true));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// List of all fields for data list key specified
|
|
/// </summary>
|
|
/// <returns>List of DataListFieldDefinition</returns>
|
|
[HttpGet("ListFields")]
|
|
public ActionResult GetDataListFields([FromQuery] string DataListKey)
|
|
{
|
|
if (!serverState.IsOpen)
|
|
{
|
|
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, 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, "DataListKey", $"DataList \"{DataListKey}\" specified does not exist"));
|
|
}
|
|
|
|
return Ok(ApiOkResponse.Response(DataList.FieldDefinitions, true));
|
|
}
|
|
|
|
}//eoc
|
|
}//ens |