This commit is contained in:
101
server/AyaNova/biz/GlobalBizSettingsBiz.cs
Normal file
101
server/AyaNova/biz/GlobalBizSettingsBiz.cs
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user