Files
sockeye/server/biz/GlobalOpsNotificationSettingsBiz.cs
2022-12-16 06:01:23 +00:00

111 lines
3.8 KiB
C#

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sockeye.Util;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Models;
namespace Sockeye.Biz
{
internal class GlobalOpsNotificationSettingsBiz : BizObject
{
internal GlobalOpsNotificationSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
{
ct = dbcontext;
UserId = currentUserId;
UserTranslationId = userTranslationId;
CurrentUserRoles = UserRoles;
BizType = SockType.OpsNotificationSettings;
}
internal static GlobalOpsNotificationSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
{
if (httpContext != null)
return new GlobalOpsNotificationSettingsBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
else
return new GlobalOpsNotificationSettingsBiz(ct, 1, ServerBootConfig.SOCKEYE_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdmin);
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// GET
//Get one
internal async Task<GlobalOpsNotificationSettings> GetAsync(bool logTheGetEvent = true)
{
//first try to fetch from db
var ret = await ct.GlobalOpsNotificationSettings.SingleOrDefaultAsync(m => m.Id == 1);
if (logTheGetEvent && ret != null)
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, SockEvent.Retrieved), ct);
//expected to exists because it's created on boot if not present
if (ret == null)
throw new System.Exception("GlobalOpsNotificationSettings::GetAsync -> Settings object not found in database!!");
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//UPDATE
//
//put
internal async Task<GlobalOpsNotificationSettings> PutAsync(GlobalOpsNotificationSettings putObject)
{
//todo: replace with new put methodology
var dbObject = await ct.GlobalOpsNotificationSettings.FirstOrDefaultAsync(m => m.Id == 1);
if (dbObject == null)
throw new System.Exception("GlobalOpsNotificationSettings::PutAsync -> Settings object not found in database!!");
CopyObject.Copy(putObject, dbObject, "Id");
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
Validate(dbObject);
if (HasErrors)
return null;
try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
return null;
}
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, SockEvent.Modified), ct);
//Update the static copy for the server
ServerGlobalOpsSettingsCache.Notify = dbObject;
return dbObject;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//VALIDATION
//
//Can save or update?
private void Validate(GlobalOpsNotificationSettings inObj)
{
//currently nothing to validate
}
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons