This commit is contained in:
2020-03-24 23:59:05 +00:00
parent fa1e65935e
commit 322e648919
4 changed files with 213 additions and 1 deletions

View File

@@ -0,0 +1,100 @@
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

View File

@@ -31,6 +31,17 @@ namespace AyaNova.Biz
#region All roles initialization
//CoreBizObject add here
////////////////////////////////////////////////////////////
//GLOBAL BIZ SETTINGS
//
roles.Add(AyaType.Global, new BizRoleSet()
{
Change = AuthorizationRoles.BizAdminFull,
ReadFullRecord = AuthorizationRoles.BizAdminLimited
});
////////////////////////////////////////////////////////////
//USER
//

View File

@@ -0,0 +1,101 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Models;
namespace AyaNova.Biz
{
internal class GlobalBizSettingsBiz : BizObject
{
internal GlobalBizSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
{
ct = dbcontext;
UserId = currentUserId;
UserTranslationId = userTranslationId;
CurrentUserRoles = UserRoles;
BizType = AyaType.Global;
}
internal static GlobalBizSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
{
if (httpContext != null)
return new GlobalBizSettingsBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
else
return new GlobalBizSettingsBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// GET
//Get one
internal async Task<GlobalBizSettings> GetAsync(bool logTheGetEvent = true)
{
//first try to fetch from db
var ret = await ct.GlobalBizSettings.SingleOrDefaultAsync(m => m.Id == 1);
if (logTheGetEvent && ret != null)
{
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Retrieved), ct);
}
//not in db then get the default
if (ret == null)
{
throw new System.Exception("GlobalBizSettingsBiz::GetAsync -> Global settings object not found in database!!");
}
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE
//
//put
internal async Task<bool> ReplaceAsync(GlobalBizSettings global)
{
var o = await ct.GlobalBizSettings.FirstOrDefaultAsync(m => m.Id == 1);
if (o == null)
throw new System.Exception("GlobalBizSettingsBiz::ReplaceAsync -> Global settings object not found in database!!");
CopyObject.Copy(global, o);
Validate(o);
if (HasErrors)
return false;
await ct.SaveChangesAsync();
//Log modification and save context
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Modified), ct);
//Update the static copy for the server
ServerGlobalBizSettings.Initialize(o);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION
//
//Can save or update?
private void Validate(GlobalBizSettings inObj)
{
//currently nothing to validate
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons

View File

@@ -37,7 +37,7 @@ namespace AyaNova.Util
}
}
//We have the object, now copy the static values here
SearchCaseSensitiveOnly = global.SearchCaseSensitiveOnly;
}