232 lines
8.9 KiB
C#
232 lines
8.9 KiB
C#
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<PickListController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public PickListController(AyContext dbcontext, ILogger<PickListController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get picklist of all Active objects of type specified and filtered by query specified
|
|
/// NOTE: Query is valid only if:
|
|
/// it is an empty string indicating not filtered just selected
|
|
/// if not an empty string, it has at most two space separated strings and one of them is a special TAG specific query that starts with two consecutive periods
|
|
/// i.e. "some" is valid (single query on all templated fields)
|
|
/// "..zon some" is valid (all tags like zon and all template fields like some)
|
|
/// "zon some" is NOT valid (missing TAGS indicator), "..zone some re" is NOT valid (too many strings)
|
|
/// Note that this list is capped automatically to return no more than 100 results
|
|
/// </summary>
|
|
/// <param name="ayaType">The AyaType object type to select from</param>
|
|
/// <param name="query">The query to filter the returned list by. Query text as provided will be case sensitively matched to all templated fields.
|
|
/// Independantely of this, if an addition space separated string that begins with two consecutive periods is encountered that will be considered a separate match to the TAGS collection of each object
|
|
/// So a tag query might be entered as "..zon some" which would match all tags LIKE 'zon' and template fields LIKE 'some'</param>
|
|
/// <param name="inactive">Include inactive objects in the returned list </param>
|
|
/// <returns>Filtered list</returns>
|
|
[HttpGet("List")]
|
|
public async Task<IActionResult> 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 PickList = PickListFactory.GetAyaPickList(ayaType);
|
|
|
|
//was the name not found as a pick list?
|
|
if (PickList == null)
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
|
|
//check rights
|
|
if (!Authorized.HasAnyRole(HttpContext.Items, PickList.AllowedRoles))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
var o = await biz.GetPickListAsync(PickList, query, inactive);
|
|
|
|
|
|
if (o == null)
|
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
else
|
|
return Ok(ApiOkResponse.Response(o, true));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get PickListTemplate
|
|
/// </summary>
|
|
/// <param name="ayaType"></param>
|
|
/// <returns>The current effective template, either a customized one or the default</returns>
|
|
[HttpGet("Template/{ayatype}")]
|
|
public async Task<IActionResult> GetDataListView([FromRoute] AyaType ayaType)
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
|
|
|
|
//Instantiate the business object handler
|
|
PickListBiz biz = PickListBiz.GetBiz(ct, HttpContext);
|
|
|
|
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(ayaType);
|
|
if (o == null)
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
|
|
return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType)));
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// List of all PickList templates
|
|
/// </summary>
|
|
/// <returns>List of strings</returns>
|
|
[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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// POST (replace) Pick List template
|
|
/// </summary>
|
|
/// <param name="ayaType"></param>
|
|
/// <param name="template"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("Template/{ayatype}")]
|
|
public async Task<IActionResult> 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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete customized template
|
|
/// (revert to default)
|
|
/// </summary>
|
|
/// <param name="ayaType"></param>
|
|
/// <returns>Ok</returns>
|
|
[HttpDelete("Template/{ayatype}")]
|
|
public async Task<IActionResult> 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 |