using System; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; using AyaNova.Models; namespace AyaNova.Biz { internal class GlobalOpsSettingsBiz : BizObject { internal GlobalOpsSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) { ct = dbcontext; UserId = currentUserId; UserTranslationId = userTranslationId; CurrentUserRoles = UserRoles; BizType = AyaType.GlobalOps; } internal static GlobalOpsSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) { if (httpContext != null) return new GlobalOpsSettingsBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); else return new GlobalOpsSettingsBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); } //////////////////////////////////////////////////////////////////////////////////////////////// /// GET //Get one internal async Task GetAsync(bool logTheGetEvent = true) { //first try to fetch from db var ret = await ct.GlobalOpsSettings.SingleOrDefaultAsync(m => m.Id == 1); if (logTheGetEvent && ret != null) 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("GlobalOpsSettingsBiz::GetAsync -> Global OPS settings object not found in database!!"); return ret; } //////////////////////////////////////////////////////////////////////////////////////////////// //UPDATE // //put internal async Task ReplaceAsync(GlobalOpsSettings putObject) { var dbObject = await ct.GlobalOpsSettings.FirstOrDefaultAsync(m => m.Id == 1); if (dbObject == null) throw new System.Exception("GlobalOpsSettingsBiz::ReplaceAsync -> Global settings object not found in database!!"); //If backup time has changed then reset last backup as well as it might block from taking effect if (putObject.BackupTime.Hour != dbObject.BackupTime.Hour && putObject.BackupTime.Minute != dbObject.BackupTime.Minute) { putObject.LastBackup = DateTime.MinValue; } CopyObject.Copy(putObject, dbObject, "Id"); ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency; Validate(dbObject); if (HasErrors) return false; await ct.SaveChangesAsync(); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Modified), ct); //Update the static copy for the server ServerGlobalOpsSettings.Initialize(dbObject); return true; } //////////////////////////////////////////////////////////////////////////////////////////////// //VALIDATION // //Can save or update? private void Validate(GlobalOpsSettings inObj) { //currently nothing to validate } ///////////////////////////////////////////////////////////////////// }//eoc }//eons