diff --git a/server/AyaNova/Controllers/AttachmentController.cs b/server/AyaNova/Controllers/AttachmentController.cs index 531e8287..2264795e 100644 --- a/server/AyaNova/Controllers/AttachmentController.cs +++ b/server/AyaNova/Controllers/AttachmentController.cs @@ -306,7 +306,7 @@ namespace AyaNova.Api.Controllers EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.AttachToObjectId, dbObj.AttachToObjectType, AyaEvent.AttachmentDelete, dbObj.DisplayFileName), ct); //Delete search index - Search.ProcessDeletedObjectKeywords(dbObj.Id, AyaType.FileAttachment); + Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, AyaType.FileAttachment); return NoContent(); } diff --git a/server/AyaNova/Controllers/WidgetController.cs b/server/AyaNova/Controllers/WidgetController.cs index b845a38e..296d6174 100644 --- a/server/AyaNova/Controllers/WidgetController.cs +++ b/server/AyaNova/Controllers/WidgetController.cs @@ -109,7 +109,7 @@ namespace AyaNova.Api.Controllers try { - if (!biz.Put(o, inObj)) + if (!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.Patch(o, objectPatch, concurrencyToken)) + if (!biz.PatchAsync(o, objectPatch, concurrencyToken)) return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) @@ -262,7 +262,7 @@ namespace AyaNova.Api.Controllers if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType)) return StatusCode(403, new ApiNotAuthorizedResponse()); - if (!biz.Delete(o)) + if (!biz.DeleteAsync(o)) return BadRequest(new ApiErrorResponse(biz.Errors)); return NoContent(); diff --git a/server/AyaNova/biz/Search.cs b/server/AyaNova/biz/Search.cs index 87d37b05..e0c3e1bc 100644 --- a/server/AyaNova/biz/Search.cs +++ b/server/AyaNova/biz/Search.cs @@ -446,12 +446,12 @@ namespace AyaNova.Biz ProcessKeywords(searchIndexObjectParameters, false);//localeId, objectID, objectType, false, name, text); } - public static void ProcessDeletedObjectKeywords(long objectID, AyaType objectType) + public static async Task ProcessDeletedObjectKeywordsAsync(long objectID, AyaType objectType) { //Be careful in future, if you put ToString at the end of each object in the string interpolation //npgsql driver will assume it's a string and put quotes around it triggering an error that a string can't be compared to an int AyContext ct = ServiceProviderProvider.DBContext; - ct.Database.ExecuteSqlInterpolated($"delete from asearchkey where objectid={objectID} and objecttype={(int)objectType}"); + await ct.Database.ExecuteSqlInterpolatedAsync($"delete from asearchkey where objectid={objectID} and objecttype={(int)objectType}"); } @@ -477,7 +477,7 @@ namespace AyaNova.Biz //IF NOT NEW, DELETE ALL EXISTING ENTRIES FOR OBJECT TYPE AND ID if (!newRecord) { - ProcessDeletedObjectKeywords(p.ObjectId, p.ObjectType); + ProcessDeletedObjectKeywordsAsync(p.ObjectId, p.ObjectType); } //BREAK OBJECT TEXT STRINGS INTO KEYWORD LIST diff --git a/server/AyaNova/biz/UserBiz.cs b/server/AyaNova/biz/UserBiz.cs index c14f066d..91070d7b 100644 --- a/server/AyaNova/biz/UserBiz.cs +++ b/server/AyaNova/biz/UserBiz.cs @@ -381,7 +381,7 @@ namespace AyaNova.Biz EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); ct.SaveChanges(); - Search.ProcessDeletedObjectKeywords(dbObj.Id, BizType); + Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); return true; diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 77bf3e3f..73c175a8 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -56,7 +56,7 @@ namespace AyaNova.Biz if (log && ret != null) { //Log - EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); } return ret; } @@ -82,19 +82,19 @@ namespace AyaNova.Biz outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); //Save to db - await ct.Widget.AddAsync(outObj); + await ct.Widget.AddAsync(outObj); await ct.SaveChangesAsync(); //Handle child and associated items: - EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - SearchIndex(outObj, true); - TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); + await SearchIndex(outObj, true); + await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); return outObj; } } - + //////////////////////////////////////////////////////////////////////////////////////////////// //DUPLICATE @@ -116,9 +116,9 @@ namespace AyaNova.Biz await ct.SaveChangesAsync(); //Handle child and associated items: - EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - SearchIndex(outObj, true); - TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); + await SearchIndex(outObj, true); + await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); return outObj; } @@ -128,7 +128,7 @@ namespace AyaNova.Biz // //put - internal bool Put(Widget dbObj, Widget inObj) + internal async Task PutAsync(Widget dbObj, Widget inObj) { //make a snapshot of the original for validation but update the original to preserve workflow @@ -151,15 +151,15 @@ namespace AyaNova.Biz return false; //Log event and save context - EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - SearchIndex(dbObj, false); - TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); + await SearchIndex(dbObj, false); + await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); return true; } //patch - internal bool Patch(Widget dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) + internal async Task PatchAsync(Widget dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) { //Validate Patch is allowed //Note: Id and Serial are all checked for and disallowed in the validate code by default @@ -181,16 +181,16 @@ namespace AyaNova.Biz return false; //Log event and save context - EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - SearchIndex(dbObj, false); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); + await SearchIndex(dbObj, false); - TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); + await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); return true; } - private void SearchIndex(Widget obj, bool isNew) + private async Task SearchIndex(Widget obj, bool isNew) { //SEARCH INDEXING var SearchParams = new Search.SearchIndexProcessObjectParameters(UserLocaleId, obj.Id, BizType, obj.Name); @@ -198,31 +198,30 @@ namespace AyaNova.Biz if (isNew) - Search.ProcessNewObjectKeywords(SearchParams); + await Search.ProcessNewObjectKeywords(SearchParams); else - Search.ProcessUpdatedObjectKeywords(SearchParams); + await Search.ProcessUpdatedObjectKeywords(SearchParams); } //////////////////////////////////////////////////////////////////////////////////////////////// //DELETE // - internal bool Delete(Widget dbObj) + internal async Task DeleteAsync(Widget dbObj) { //Determine if the object can be deleted, do the deletion tentatively //Probably also in here deal with tags and associated search text etc - ValidateCanDelete(dbObj); + //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); if (HasErrors) return false; ct.Widget.Remove(dbObj); - ct.SaveChanges(); + await ct.SaveChangesAsync(); - //Log event and save context - EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - ct.SaveChanges(); - Search.ProcessDeletedObjectKeywords(dbObj.Id, BizType); - TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); + //Log event + await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); + await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); + await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); return true; } @@ -233,7 +232,7 @@ namespace AyaNova.Biz // //Can save or update? - private void Validate(Widget proposedObj, Widget currentObj) + private async Task Validate(Widget proposedObj, Widget currentObj) { //NOTE: In DB schema only name and serial are not nullable @@ -253,7 +252,7 @@ namespace AyaNova.Biz if (!PropertyHasErrors("Name")) { //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (ct.Widget.Any(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) + if (await ct.Widget.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) { AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); } @@ -280,7 +279,7 @@ namespace AyaNova.Biz } //Any form customizations to validate? - var FormCustomization = ct.FormCustom.SingleOrDefault(x => x.FormKey == AyaFormFieldDefinitions.WIDGET_KEY); + var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaFormFieldDefinitions.WIDGET_KEY); if (FormCustomization != null) { //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required @@ -291,17 +290,15 @@ namespace AyaNova.Biz //validate custom fields CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); } - - - return; + } //Can delete? - private void ValidateCanDelete(Widget inObj) - { - //whatever needs to be check to delete this object - } + // private void ValidateCanDelete(Widget inObj) + // { + // //whatever needs to be check to delete this object + // }