This commit is contained in:
2020-01-27 19:53:36 +00:00
parent 1a322eaa10
commit 1f1d1fbabc
2 changed files with 60 additions and 59 deletions

View File

@@ -124,7 +124,7 @@ namespace AyaNova.Api.Controllers
try try
{ {
if (!biz.Put(o, inObj)) if (!biz.PutAsync(o, inObj))
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
} }
catch (DbUpdateConcurrencyException) catch (DbUpdateConcurrencyException)
@@ -183,7 +183,7 @@ namespace AyaNova.Api.Controllers
try try
{ {
//patch and validate //patch and validate
if (!biz.Patch(o, objectPatch, concurrencyToken)) if (!biz.PatchAsync(o, objectPatch, concurrencyToken))
{ {
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
} }
@@ -288,7 +288,7 @@ namespace AyaNova.Api.Controllers
} }
if (!biz.Delete(dbObj)) if (!biz.DeleteAsync(dbObj))
{ {
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
} }

View File

@@ -66,7 +66,6 @@ namespace AyaNova.Biz
if (inObj.UserOptions == null) if (inObj.UserOptions == null)
inObj.UserOptions = new UserOptions(); inObj.UserOptions = new UserOptions();
await ValidateAsync(inObj, null); await ValidateAsync(inObj, null);
if (HasErrors) if (HasErrors)
return null; return null;
@@ -86,54 +85,54 @@ namespace AyaNova.Biz
await SearchIndexAsync(inObj, true); await SearchIndexAsync(inObj, true);
//TAGS //TAGS
TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, inObj.Tags, null); await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, inObj.Tags, null);
return inObj; return inObj;
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE // //CREATE
internal User Create(AyContext TempContext, User inObj) // internal User Create(AyContext TempContext, User inObj)
{ // {
//This is a new user so it will have been posted with a password in plaintext which needs to be salted and hashed // //This is a new user so it will have been posted with a password in plaintext which needs to be salted and hashed
inObj.Salt = Hasher.GenerateSalt(); // inObj.Salt = Hasher.GenerateSalt();
inObj.Password = Hasher.hash(inObj.Salt, inObj.Password); // inObj.Password = Hasher.hash(inObj.Salt, inObj.Password);
inObj.Tags = TagUtil.NormalizeTags(inObj.Tags); // inObj.Tags = TagUtil.NormalizeTags(inObj.Tags);
inObj.CustomFields = JsonUtil.CompactJson(inObj.CustomFields); // inObj.CustomFields = JsonUtil.CompactJson(inObj.CustomFields);
//Seeder sets user options in advance so no need to create them here in that case // //Seeder sets user options in advance so no need to create them here in that case
if (inObj.UserOptions == null) // if (inObj.UserOptions == null)
inObj.UserOptions = new UserOptions(); // inObj.UserOptions = new UserOptions();
ValidateAsync(inObj, null); // ValidateAsync(inObj, null);
if (HasErrors) // if (HasErrors)
return null; // return null;
else // else
{ // {
TempContext.User.Add(inObj); // TempContext.User.Add(inObj);
//save to get Id // //save to get Id
TempContext.SaveChanges(); // TempContext.SaveChanges();
//Handle child and associated items // //Handle child and associated items
//Log event // //Log event
EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, inObj.Id, BizType, AyaEvent.Created), TempContext); // EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, inObj.Id, BizType, AyaEvent.Created), TempContext);
//SEARCH INDEXING // //SEARCH INDEXING
SearchIndexAsync(inObj, true); // SearchIndexAsync(inObj, true);
//TAGS // //TAGS
TagUtil.ProcessUpdateTagsInRepositoryAsync(TempContext, inObj.Tags, null); // TagUtil.ProcessUpdateTagsInRepositoryAsync(TempContext, inObj.Tags, null);
return inObj; // return inObj;
} // }
} // }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
/// GET /// GET
@@ -146,7 +145,7 @@ namespace AyaNova.Biz
if (ret != null) if (ret != null)
{ {
//Log //Log
EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct);
} }
return ret; return ret;
} }
@@ -266,7 +265,7 @@ namespace AyaNova.Biz
// //
//put //put
internal bool Put(User dbObj, User inObj) internal async Task<bool> PutAsync(User dbObj, User inObj)
{ {
//Get a snapshot of the original db value object before changes //Get a snapshot of the original db value object before changes
User SnapshotOfOriginalDBObj = new User(); User SnapshotOfOriginalDBObj = new User();
@@ -295,23 +294,23 @@ namespace AyaNova.Biz
//this will allow EF to check it out //this will allow EF to check it out
ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken;
ValidateAsync(dbObj, SnapshotOfOriginalDBObj); await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
if (HasErrors) if (HasErrors)
return false; return false;
await ct.SaveChangesAsync();
//Log modification and save context //Log modification and save context
EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
SearchIndexAsync(dbObj, false); await SearchIndexAsync(dbObj, false);
TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
return true; return true;
} }
//patch //patch
internal bool Patch(User dbObj, JsonPatchDocument<User> objectPatch, uint concurrencyToken) internal async Task<bool> PatchAsync(User dbObj, JsonPatchDocument<User> objectPatch, uint concurrencyToken)
{ {
//Validate Patch is allowed //Validate Patch is allowed
if (!ValidateJsonPatch<User>.Validate(this, objectPatch)) return false; if (!ValidateJsonPatch<User>.Validate(this, objectPatch)) return false;
@@ -333,15 +332,16 @@ namespace AyaNova.Biz
} }
ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken;
ValidateAsync(dbObj, SnapshotOfOriginalDBObj); await ValidateAsync(dbObj, SnapshotOfOriginalDBObj);
if (HasErrors) if (HasErrors)
return false; return false;
await ct.SaveChangesAsync();
//Log modification and save context //Log modification and save context
EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct);
SearchIndexAsync(dbObj, false); await SearchIndexAsync(dbObj, false);
TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags);
return true; return true;
} }
@@ -363,25 +363,25 @@ namespace AyaNova.Biz
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
//DELETE //DELETE
// //
internal bool Delete(User dbObj) internal async Task<bool> DeleteAsync(User dbObj)
{ {
ValidateCanDelete(dbObj); await ValidateCanDelete(dbObj);
if (HasErrors) if (HasErrors)
return false; return false;
//Remove the object //Remove the object
ct.User.Remove(dbObj); ct.User.Remove(dbObj);
ct.SaveChanges(); await ct.SaveChangesAsync();
//Delete sibling objects //Delete sibling objects
//USEROPTIONS //USEROPTIONS
ct.Database.ExecuteSqlInterpolated($"delete from auseroptions where userid={dbObj.Id}"); await ct.Database.ExecuteSqlInterpolatedAsync($"delete from auseroptions where userid={dbObj.Id}");
EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct);
ct.SaveChanges();
Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType);
TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags);
return true; return true;
} }
@@ -556,7 +556,7 @@ namespace AyaNova.Biz
//Can delete? //Can delete?
private void ValidateCanDelete(User inObj) private async Task ValidateCanDelete(User inObj)
{ {
@@ -569,7 +569,8 @@ namespace AyaNova.Biz
//They can always make any user inactive to get rid of them and it will mean referential integrity issues are not there //They can always make any user inactive to get rid of them and it will mean referential integrity issues are not there
//There's only one rule - have they done anything eventlog worthy yet? //There's only one rule - have they done anything eventlog worthy yet?
if (ct.Event.Select(m => m).Where(m => m.UserId == inObj.Id).Count() > 0) //if (await ct.Event.Select(m => m).Where(m => m.UserId == inObj.Id).Count() > 0)
if (await ct.Event.AnyAsync(m => m.UserId == inObj.Id))
{ {
AddError(ApiErrorCode.INVALID_OPERATION, "user", "LT:ErrorDBForeignKeyViolation"); AddError(ApiErrorCode.INVALID_OPERATION, "user", "LT:ErrorDBForeignKeyViolation");
return; return;
@@ -829,7 +830,7 @@ namespace AyaNova.Biz
break; break;
case "eventlog": case "eventlog":
{ {
ImportAyaNova7Biz.LogEventCreatedModifiedEventsAsync(j, importMap, BizType, ct); await ImportAyaNova7Biz.LogEventCreatedModifiedEventsAsync(j, importMap, BizType, ct);
} }
break; break;
case "locale": case "locale":
@@ -884,7 +885,7 @@ namespace AyaNova.Biz
break; break;
} }
u.LocaleId = LocaleBiz.LocaleNameToIdStaticAsync(NewLocaleName, ct); u.LocaleId = await LocaleBiz.LocaleNameToIdStaticAsync(NewLocaleName, ct);
ct.SaveChanges(); ct.SaveChanges();
#endregion set locale #endregion set locale