100 lines
3.5 KiB
C#
100 lines
3.5 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 System.Threading.Tasks;
|
|
|
|
namespace AyaNova.Api.Controllers
|
|
{
|
|
|
|
[ApiController]
|
|
[ApiVersion("8.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
public class GlobalBizSettingsController : ControllerBase
|
|
{
|
|
private readonly AyContext ct;
|
|
private readonly ILogger<GlobalBizSettingsController> log;
|
|
private readonly ApiServerState serverState;
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// ctor
|
|
/// </summary>
|
|
/// <param name="dbcontext"></param>
|
|
/// <param name="logger"></param>
|
|
/// <param name="apiServerState"></param>
|
|
public GlobalBizSettingsController(AyContext dbcontext, ILogger<GlobalBizSettingsController> logger, ApiServerState apiServerState)
|
|
{
|
|
ct = dbcontext;
|
|
log = logger;
|
|
serverState = apiServerState;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get GlobalBizSettings
|
|
/// </summary>
|
|
/// <returns>Global settings object</returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetGlobalBizSettings()
|
|
{
|
|
if (serverState.IsClosed)
|
|
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
|
|
|
|
//Instantiate the business object handler
|
|
GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.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();
|
|
if (o == null)
|
|
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
|
|
return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType)));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// POST (replace) Global biz settings
|
|
/// </summary>
|
|
/// <param name="global"></param>
|
|
/// <returns>nothing</returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> ReplaceGlobalBizSettings([FromBody] GlobalBizSettings global)
|
|
{
|
|
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
|
|
GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext);
|
|
|
|
if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
|
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
|
|
try
|
|
{
|
|
if (!await biz.ReplaceAsync(global))
|
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT));
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
}//eoc
|
|
}//ens |