172 lines
5.9 KiB
C#
172 lines
5.9 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Sockeye.Util;
|
|
using Sockeye.Api.ControllerHelpers;
|
|
using Sockeye.Models;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
//JSON KEY
|
|
using Org.BouncyCastle.Security;
|
|
using Org.BouncyCastle.OpenSsl;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Sockeye.Biz
|
|
{
|
|
|
|
internal class GlobalBizSettingsBiz : BizObject
|
|
{
|
|
|
|
internal GlobalBizSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
|
{
|
|
ct = dbcontext;
|
|
UserId = currentUserId;
|
|
UserTranslationId = userTranslationId;
|
|
CurrentUserRoles = UserRoles;
|
|
BizType = SockType.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.SOCKEYE_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdmin);
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/// GET
|
|
|
|
//Get one
|
|
internal async Task<GlobalBizSettings> GetAsync(bool logTheGetEvent = true)
|
|
{
|
|
//first try to fetch from db
|
|
var ret = await ct.GlobalBizSettings.AsNoTracking().SingleOrDefaultAsync(m => m.Id == 1);
|
|
if (logTheGetEvent && ret != null)
|
|
{
|
|
//Log
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, SockEvent.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
|
|
//
|
|
internal async Task<GlobalBizSettings> PutAsync(GlobalBizSettings putObject)
|
|
{
|
|
var dbObject = await GetAsync(false);
|
|
if (dbObject == null)
|
|
throw new System.Exception("GlobalBizSettingsBiz::PutAsync -> Global settings object not found in database. Contact support immediately!");
|
|
|
|
if (dbObject.Concurrency != putObject.Concurrency)
|
|
{
|
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
|
return null;
|
|
}
|
|
Validate(putObject, dbObject);
|
|
if (HasErrors) return null;
|
|
|
|
List<string> originalTags = dbObject.AllTags();
|
|
List<string> newTags = putObject.AllTags();
|
|
|
|
ct.Replace(dbObject, putObject);
|
|
|
|
|
|
try
|
|
{
|
|
await ct.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
|
return null;
|
|
}
|
|
//Update cache
|
|
ServerGlobalBizSettings.Initialize(putObject, null);
|
|
|
|
//Log modification and save context
|
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, SockEvent.Modified), ct);
|
|
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newTags, originalTags);
|
|
|
|
return putObject;
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//VALIDATION
|
|
//
|
|
|
|
//Can save or update?
|
|
private void Validate(GlobalBizSettings proposedObj, GlobalBizSettings currentObj)
|
|
{
|
|
//currently nothing to validate
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//IMPORT FROM ROCKFISH
|
|
|
|
public async Task ImportRockfish(AyContext ct, ILogger log)
|
|
{
|
|
//Authenticate to rockfish
|
|
//string sUrl = $"{LICENSE_SERVER_URL_ROCKFISH}rvr";
|
|
string URL_ROCKFISH = "https://rockfish.ayanova.com/";
|
|
|
|
try
|
|
{
|
|
var content = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
login = "john",
|
|
password = "b43698c255365ee739c05ba0d42855e96c2365c76bb2f9b9eb149cec7b52174c"
|
|
}), Encoding.UTF8, "application/json");
|
|
var client = ServiceProviderProvider.HttpClientFactory.CreateClient();
|
|
var res = await client.PostAsync($"{URL_ROCKFISH}/authenticate", content);
|
|
var responseText = await res.Content.ReadAsStringAsync();
|
|
if (res.IsSuccessStatusCode)
|
|
{
|
|
log.LogInformation(responseText);
|
|
}
|
|
else log.LogError($"E1020 - Error authenticating to rockfish: {res.ReasonPhrase}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var msg = "E1020 - Error authenticating to rockfish see log for details";
|
|
log.LogError(ex, msg);
|
|
}
|
|
|
|
|
|
//in the correct order retrieve every object and if it's not already present in sockeye, import it
|
|
//this should be callable any time and it will just update so it can be test live in sync / parallel until ready to switch over
|
|
//await Task.CompletedTask;
|
|
|
|
}
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
}//eoc
|
|
|
|
|
|
}//eons
|
|
|