This commit is contained in:
@@ -5,6 +5,7 @@ using AyaNova.Util;
|
||||
using AyaNova.Biz;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AyaNova.Api.Controllers
|
||||
{
|
||||
@@ -36,7 +37,7 @@ namespace AyaNova.Api.Controllers
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public ContentResult Index()
|
||||
public async Task<ContentResult> Index()
|
||||
{
|
||||
var errorBlock = string.Empty;
|
||||
if (serverState.IsSystemLocked)
|
||||
@@ -67,7 +68,7 @@ namespace AyaNova.Api.Controllers
|
||||
<h4>Schema version</h4>
|
||||
<pre>{AySchema.currentSchema.ToString()}</pre>
|
||||
<h4>Active techs</h4>
|
||||
<pre>{UserBiz.ActiveCount}</pre>
|
||||
<pre>{await UserBiz.ActiveCountAsync()}</pre>
|
||||
<h4>Server time</h4>
|
||||
<pre>{DateUtil.ServerDateTimeString(System.DateTime.UtcNow)}</pre>
|
||||
<pre>{TimeZoneInfo.Local.Id}</pre>
|
||||
|
||||
@@ -122,7 +122,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutAsync(o, inObj, DataList))
|
||||
if (!await biz.PutAsync(o, inObj, DataList))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
@@ -168,7 +168,7 @@ namespace AyaNova.Api.Controllers
|
||||
if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
|
||||
if (!biz.DeleteAsync(o))
|
||||
if (!await biz.DeleteAsync(o))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
|
||||
return NoContent();
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutAsync(o, inObj))
|
||||
if (!await biz.PutAsync(o, inObj))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
|
||||
@@ -231,14 +231,14 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutLocaleItemDisplayTextAsync(oFromDb, inObj, oDbParent))
|
||||
if (!await biz.PutLocaleItemDisplayTextAsync(oFromDb, inObj, oDbParent))
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!biz.LocaleItemExistsAsync(inObj.Id))
|
||||
if (!await biz.LocaleItemExistsAsync(inObj.Id))
|
||||
{
|
||||
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||
}
|
||||
@@ -293,7 +293,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutLocaleNameAsync(oFromDb, inObj))
|
||||
if (!await biz.PutLocaleNameAsync(oFromDb, inObj))
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
@@ -301,7 +301,7 @@ namespace AyaNova.Api.Controllers
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!biz.LocaleExistsAsync(inObj.Id))
|
||||
if (!await biz.LocaleExistsAsync(inObj.Id))
|
||||
{
|
||||
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||
}
|
||||
@@ -340,7 +340,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
//Fetch locale and it's children
|
||||
//(fetch here so can return proper REST responses on failing basic validity)
|
||||
var dbObj = ct.Locale.Include(x => x.LocaleItems).SingleOrDefault(m => m.Id == id);
|
||||
var dbObj = await ct.Locale.Include(x => x.LocaleItems).SingleOrDefaultAsync(m => m.Id == id);
|
||||
if (dbObj == null)
|
||||
{
|
||||
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||
@@ -353,21 +353,12 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
|
||||
//Instantiate the business object handler
|
||||
//LocaleBiz biz = new LocaleBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));
|
||||
LocaleBiz biz = LocaleBiz.GetBiz(ct, HttpContext);
|
||||
if (!biz.DeleteAsync(dbObj))
|
||||
if (!await biz.DeleteAsync(dbObj))
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
//Log
|
||||
EventLogProcessor.DeleteObjectLogAsync(biz.UserId, AyaType.Locale, dbObj.Id, dbObj.Name, ct);
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
//Delete children / attached objects
|
||||
// biz.DeleteChildren(dbObj);
|
||||
|
||||
|
||||
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutAsync(o, inObj))
|
||||
if (!await biz.PutAsync(o, inObj))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
@@ -183,7 +183,7 @@ namespace AyaNova.Api.Controllers
|
||||
try
|
||||
{
|
||||
//patch and validate
|
||||
if (!biz.PatchAsync(o, objectPatch, concurrencyToken))
|
||||
if (!await biz.PatchAsync(o, objectPatch, concurrencyToken))
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
@@ -288,7 +288,7 @@ namespace AyaNova.Api.Controllers
|
||||
}
|
||||
|
||||
|
||||
if (!biz.DeleteAsync(dbObj))
|
||||
if (!await biz.DeleteAsync(dbObj))
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
if (!biz.PutAsync(o, inObj))
|
||||
if (!await biz.PutAsync(o, inObj))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
@@ -155,7 +155,7 @@ namespace AyaNova.Api.Controllers
|
||||
try
|
||||
{
|
||||
//patch and validate
|
||||
if (!biz.PatchAsync(o, objectPatch, concurrencyToken))
|
||||
if (!await biz.PatchAsync(o, objectPatch, concurrencyToken))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
@@ -176,7 +176,7 @@ namespace AyaNova.Api.Controllers
|
||||
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public IActionResult PostWidget([FromBody] Widget inObj, ApiVersion apiVersion)
|
||||
public async Task<IActionResult> PostWidget([FromBody] Widget inObj, ApiVersion apiVersion)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
|
||||
@@ -192,7 +192,7 @@ namespace AyaNova.Api.Controllers
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
|
||||
//Create and validate
|
||||
Widget o = biz.Create(inObj);
|
||||
Widget o = await biz.CreateAsync(inObj);
|
||||
if (o == null)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
@@ -262,7 +262,7 @@ namespace AyaNova.Api.Controllers
|
||||
if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
|
||||
if (!biz.DeleteAsync(o))
|
||||
if (!await biz.DeleteAsync(o))
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
|
||||
return NoContent();
|
||||
|
||||
@@ -289,6 +289,8 @@ namespace AyaNova.Biz
|
||||
return false;
|
||||
ct.Locale.Remove(dbObj);
|
||||
await ct.SaveChangesAsync();
|
||||
//Log
|
||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, AyaType.Locale, dbObj.Id, dbObj.Name, ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -417,23 +417,23 @@ namespace AyaNova.Core
|
||||
/// Initialize the key
|
||||
/// Handle if first boot scenario to tag DB ID etc
|
||||
/// </summary>
|
||||
internal static void Initialize(AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ctx, ILogger log)
|
||||
internal static async Task Initialize(AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ct, ILogger log)
|
||||
{
|
||||
log.LogDebug("Initializing license");
|
||||
|
||||
try
|
||||
{
|
||||
//Fetch key from db as no tracking so doesn't hang round if need to immediately clear and then re-add the key
|
||||
Models.License ldb = ctx.License.AsNoTracking().SingleOrDefault();
|
||||
Models.License ldb = ct.License.AsNoTracking().SingleOrDefault();
|
||||
|
||||
//Non existent license should restrict server to ops routes only with closed API
|
||||
if (ldb == null)
|
||||
{
|
||||
ldb = new Models.License();
|
||||
ldb.DbId = Guid.NewGuid();
|
||||
ldb.DbId = Guid.NewGuid();
|
||||
ldb.Key = "none";
|
||||
ctx.License.Add(ldb);
|
||||
ctx.SaveChanges();
|
||||
ct.License.Add(ldb);
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
|
||||
//ensure DB ID
|
||||
@@ -442,8 +442,8 @@ namespace AyaNova.Core
|
||||
ldb.DbId = Guid.NewGuid();
|
||||
//Convert the no tracking record fetched above to tracking
|
||||
//this is required because a prior call to initialize before dumping the db would mean the license is still in memory in the context
|
||||
ctx.Entry(ldb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||
ctx.SaveChanges();
|
||||
ct.Entry(ldb).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
|
||||
//Get it early and set it here so that it can be displayed early to the user even if not licensed
|
||||
@@ -478,7 +478,7 @@ namespace AyaNova.Core
|
||||
}
|
||||
|
||||
//Has someone been trying funny business with the active techs in the db?
|
||||
if (AyaNova.Biz.UserBiz.ActiveCount > _ActiveLicense.ActiveNumber)
|
||||
if (await AyaNova.Biz.UserBiz.ActiveCountAsync() > _ActiveLicense.ActiveNumber)
|
||||
{
|
||||
var msg = $"E1020 - Active count exceeded capacity";
|
||||
apiServerState.SetSystemLock(msg);
|
||||
@@ -508,11 +508,11 @@ namespace AyaNova.Core
|
||||
/// <summary>
|
||||
/// Install key to db
|
||||
/// </summary>
|
||||
private static bool Install(string RawTextNewKey, AyaNovaLicenseKey ParsedNewKey, AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ctx, ILogger log)
|
||||
private static async Task<bool> Install(string RawTextNewKey, AyaNovaLicenseKey ParsedNewKey, AyaNova.Api.ControllerHelpers.ApiServerState apiServerState, AyContext ct, ILogger log)
|
||||
{
|
||||
try
|
||||
{
|
||||
var CurrentInDbKeyRecord = ctx.License.OrderBy(x => x.Id).FirstOrDefault();
|
||||
var CurrentInDbKeyRecord = await ct.License.OrderBy(x => x.Id).FirstOrDefaultAsync();
|
||||
if (CurrentInDbKeyRecord == null)
|
||||
throw new ApplicationException("E1020 - Can't install key, no key record found");
|
||||
|
||||
@@ -522,13 +522,13 @@ namespace AyaNova.Core
|
||||
}
|
||||
|
||||
//Can't install a trial into a non-empty db
|
||||
if (ParsedNewKey.TrialLicense && !DbUtil.DBIsEmpty(ctx, log))
|
||||
if (ParsedNewKey.TrialLicense && !DbUtil.DBIsEmpty(ct, log))
|
||||
{
|
||||
throw new ApplicationException("E1020 - Can't install a trial key into a non empty AyaNova database. Erase the database first.");
|
||||
}
|
||||
|
||||
//TODO: TECHCOUNT - new license causes exceeding count?
|
||||
if (AyaNova.Biz.UserBiz.ActiveCount > ParsedNewKey.GetLicenseFeature(SERVICE_TECHS_FEATURE_NAME).Count)
|
||||
if (await AyaNova.Biz.UserBiz.ActiveCountAsync() > ParsedNewKey.GetLicenseFeature(SERVICE_TECHS_FEATURE_NAME).Count)
|
||||
{
|
||||
throw new ApplicationException("E1020 - Can't install key, too many active techs and / or subcontractors in database. Deactivate enough to install key.");
|
||||
}
|
||||
@@ -537,7 +537,7 @@ namespace AyaNova.Core
|
||||
CurrentInDbKeyRecord.Key = RawTextNewKey;
|
||||
//LOOKAT: reason, resultcode etc
|
||||
//There is similar block related to this in ayschema for db schema version 8
|
||||
ctx.SaveChanges();
|
||||
ct.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -547,7 +547,7 @@ namespace AyaNova.Core
|
||||
}
|
||||
finally
|
||||
{
|
||||
Initialize(apiServerState, ctx, log);
|
||||
await Initialize(apiServerState, ct, log);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user