diff --git a/server/AyaNova/Controllers/UserController.cs b/server/AyaNova/Controllers/UserController.cs index b148d4a3..2e662ac7 100644 --- a/server/AyaNova/Controllers/UserController.cs +++ b/server/AyaNova/Controllers/UserController.cs @@ -73,8 +73,8 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); - + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); + var o = await biz.GetAsync(id); if (o == null) @@ -118,7 +118,7 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); ApiPagedResponse pr = await biz.GetManyAsync(Url, nameof(ListUsers), pagingOptions); return Ok(new ApiOkWithPagingResponse(pr)); @@ -154,7 +154,7 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); ApiPagedResponse pr = await biz.GetPickListAsync(Url, nameof(UserPickList), pagingOptions, q); return Ok(new ApiOkWithPagingResponse(pr)); } @@ -196,19 +196,12 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); - - if (!biz.Put(o, inObj)) - { - return BadRequest(new ApiErrorResponse(biz.Errors)); - } + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); try { - //Log - EventLogProcessor.AddEntry(new Event(biz.UserId, o.Id, AyaType.User, AyaEvent.Modified), ct); - await ct.SaveChangesAsync(); - Search.ProcessUpdatedObjectKeywords(ct, UserLocaleIdFromContext.Id(HttpContext.Items), o.Id, AyaType.User, o.Name, o.EmployeeNumber, o.Notes, o.Name); + if (!biz.Put(o, inObj)) + return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) { @@ -225,8 +218,6 @@ namespace AyaNova.Api.Controllers } } - - return Ok(new ApiOkResponse(new { ConcurrencyToken = o.ConcurrencyToken })); } @@ -257,7 +248,7 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); var o = await ct.User.SingleOrDefaultAsync(m => m.Id == id); @@ -271,18 +262,13 @@ namespace AyaNova.Api.Controllers return StatusCode(401, new ApiNotAuthorizedResponse()); } - //patch and validate - if (!biz.Patch(o, objectPatch, concurrencyToken)) - { - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - try { - //Log - EventLogProcessor.AddEntry(new Event(biz.UserId, o.Id, AyaType.User, AyaEvent.Modified), ct); - await ct.SaveChangesAsync(); - Search.ProcessUpdatedObjectKeywords(ct, UserLocaleIdFromContext.Id(HttpContext.Items), o.Id, AyaType.User, o.Name, o.EmployeeNumber, o.Notes, o.Name); + //patch and validate + if (!biz.Patch(o, objectPatch, concurrencyToken)) + { + return BadRequest(new ApiErrorResponse(biz.Errors)); + } } catch (DbUpdateConcurrencyException) { @@ -296,9 +282,6 @@ namespace AyaNova.Api.Controllers } } - - - return Ok(new ApiOkResponse(new { ConcurrencyToken = o.ConcurrencyToken })); } @@ -332,7 +315,7 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); //Create and validate User o = await biz.CreateAsync(inObj); @@ -345,10 +328,6 @@ namespace AyaNova.Api.Controllers else { - //EVENT LOG - //Note that event log is processed in the biz object unlike the widget for no apparent reason - - //return success and link //NOTE: this is a USER object so we don't want to return some key fields for security reasons //which is why the object is "cleaned" before return @@ -393,23 +372,12 @@ namespace AyaNova.Api.Controllers } //Instantiate the business object handler - UserBiz biz = UserBiz.GetBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserLocaleIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); + UserBiz biz = UserBiz.GetBiz(ct, HttpContext); if (!biz.Delete(dbObj)) { return BadRequest(new ApiErrorResponse(biz.Errors)); } - //Log - EventLogProcessor.DeleteObject(biz.UserId, AyaType.User, dbObj.Id, dbObj.Name, ct); - await ct.SaveChangesAsync(); - - //Delete children / attached objects - biz.DeleteChildren(dbObj); - - //This will directly execute and is not part of context for saving purposes - Search.ProcessDeletedObjectKeywords(ct, dbObj.Id, AyaType.User); - - return NoContent(); } diff --git a/server/AyaNova/biz/BizObjectFactory.cs b/server/AyaNova/biz/BizObjectFactory.cs index cf9a7f72..3c186650 100644 --- a/server/AyaNova/biz/BizObjectFactory.cs +++ b/server/AyaNova/biz/BizObjectFactory.cs @@ -24,7 +24,7 @@ namespace AyaNova.Biz switch (aytype) { case AyaType.User: - return new UserBiz(dbcontext, userId, roles); + return new UserBiz(dbcontext, userId, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, roles); case AyaType.Widget: return new WidgetBiz(dbcontext, userId, roles); case AyaType.Tag: diff --git a/server/AyaNova/biz/UserBiz.cs b/server/AyaNova/biz/UserBiz.cs index e81175ba..6ddaa605 100644 --- a/server/AyaNova/biz/UserBiz.cs +++ b/server/AyaNova/biz/UserBiz.cs @@ -33,19 +33,18 @@ namespace AyaNova.Biz V7ValidationImportMode = false;//default } -todo: reorging this to be a simple static fetcher for the biz object, pass it the httpcontext and it will figure the rest -//then move all indexing and eventlog and associated record handling into here so that routes can just call into it simply and it handles all -//here at the biz level + //todo: + //then move all indexing and eventlog and associated record handling into here so that routes can just call into it simply and it handles all + //here at the biz level -//Then after that go into seeder and re-org to use the biz object to create the users (it will be slower but WTF other way is this going to work properly to generate data??) + //Then after that go into seeder and re-org to use the biz object to create the users (it will be slower but WTF other way is this going to work properly to generate data??) -//then after that go into widget and anywhere else that there is this associated type code being called for event and search and implement everywhere, -//then update seeder code to use it and get back on to the main critical path again in the todo + //then after that go into widget and anywhere else that there is this associated type code being called for event and search and implement everywhere, + //then update seeder code to use it and get back on to the main critical path again in the todo - internal static UserBiz GetBiz(AyContext ct, HttpContextExtensions , AuthorizationRoles UserRoles) + internal static UserBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext) { - //long currentUserId, long userLocaleId - return new UserBiz(ct, currentUserId, userLocaleId, UserRoles); + return new UserBiz(ct, UserIdFromContext.Id(httpContext.Items), UserLocaleIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); } //////////////////////////////////////////////////////////////////////////////////////////////// @@ -214,6 +213,13 @@ todo: reorging this to be a simple static fetcher for the biz object, pass it th if (HasErrors) return false; + + //Log modification + EventLogProcessor.AddEntry(new Event(UserId, dbObj.Id, AyaType.User, AyaEvent.Modified), ct); + ct.SaveChanges(); + //Update keywords + Search.ProcessUpdatedObjectKeywords(ct, UserLocaleId, dbObj.Id, AyaType.User, dbObj.Name, dbObj.EmployeeNumber, dbObj.Notes, dbObj.Name); + return true; } @@ -241,6 +247,13 @@ todo: reorging this to be a simple static fetcher for the biz object, pass it th Validate(dbObj, snapshotObj); if (HasErrors) return false; + + //Log modification + EventLogProcessor.AddEntry(new Event(UserId, dbObj.Id, AyaType.User, AyaEvent.Modified), ct); + ct.SaveChanges(); + //Update keywords + Search.ProcessUpdatedObjectKeywords(ct, UserLocaleId, dbObj.Id, AyaType.User, dbObj.Name, dbObj.EmployeeNumber, dbObj.Notes, dbObj.Name); + return true; } @@ -251,28 +264,32 @@ todo: reorging this to be a simple static fetcher for the biz object, pass it th internal bool Delete(User 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); if (HasErrors) return false; + + //Remove the object ct.User.Remove(dbObj); + ct.SaveChanges(); + + //Delete sibling objects + //USEROPTIONS + ct.Database.ExecuteSqlCommand($"delete from auseroptions where userid={dbObj.Id}"); + + //Event log process delete + EventLogProcessor.DeleteObject(UserId, AyaType.User, dbObj.Id, dbObj.Name, ct); + ct.SaveChanges(); + + //Delete search index + Search.ProcessDeletedObjectKeywords(ct, dbObj.Id, AyaType.User); + + //TAGS + TagMapBiz.DeleteAllForObject(new AyaTypeId(AyaType.User, dbObj.Id), ct); + ct.SaveChanges(); + return true; } - /// - /// Delete child objects like tags and attachments and etc - /// - /// - internal void DeleteChildren(User dbObj) - { - //TAGS - TagMapBiz.DeleteAllForObject(new AyaTypeId(AyaType.User, dbObj.Id), ct); - - //USEROPTIONS - ct.Database.ExecuteSqlCommand($"delete from auseroptions where userid={dbObj.Id}"); - } //////////////////////////////////////////////////////////////////////////////////////////////// //VALIDATION