From 179cc0a5221ad4cd29fe1ab6d348720dbba90de7 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 4 Oct 2018 20:34:00 +0000 Subject: [PATCH] --- .../AyaNova/Controllers/TagGroupController.cs | 160 +++--------------- server/AyaNova/biz/TagGroupBiz.cs | 31 +++- 2 files changed, 56 insertions(+), 135 deletions(-) diff --git a/server/AyaNova/Controllers/TagGroupController.cs b/server/AyaNova/Controllers/TagGroupController.cs index 84e96eda..ad1bb58b 100644 --- a/server/AyaNova/Controllers/TagGroupController.cs +++ b/server/AyaNova/Controllers/TagGroupController.cs @@ -55,31 +55,21 @@ namespace AyaNova.Api.Controllers [HttpGet("{id}")] public async Task GetTagGroup([FromRoute] long id) { - if (!serverState.IsOpen) - { - return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup)) - { - return StatusCode(401, new ApiNotAuthorizedResponse()); - } + if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup)) + return StatusCode(401, new ApiNotAuthorizedResponse()); - if (!ModelState.IsValid) - { - return BadRequest(new ApiErrorResponse(ModelState)); - } + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); var o = await biz.GetAsync(id); - - if (o == null) - { - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } - + if (o == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return Ok(new ApiOkResponse(o)); } @@ -104,19 +94,13 @@ namespace AyaNova.Api.Controllers public async Task TagGroupPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup))//Note: anyone can read a TagGroup, but that might change in future so keeping this code in - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); @@ -139,19 +123,13 @@ namespace AyaNova.Api.Controllers public async Task TagsInGroupPickList([FromRoute] long id) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup))//Note: anyone can read a tag-group, but that might change in future so keeping this code in - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); @@ -172,43 +150,24 @@ namespace AyaNova.Api.Controllers public async Task PostTagGroup([FromBody] NameItem inObj) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } //If a user has change roles, or editOwnRoles then they can create, true is passed for isOwner since they are creating so by definition the owner if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, AyaType.TagGroup)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); //Create and validate TagGroup o = await biz.CreateAsync(inObj.Name); - if (o == null) - { - //error return return BadRequest(new ApiErrorResponse(biz.Errors)); - } else - { - //save and get ID for log then success return - await ct.SaveChangesAsync(); - - //Log - EventLogProcessor.AddEntryToContextNoSave(new Event(biz.UserId, o.Id, AyaType.TagGroup, AyaEvent.Created), ct); - await ct.SaveChangesAsync(); - return CreatedAtAction("GetTagGroup", new { id = o.Id }, new ApiCreatedResponse(o)); - } } @@ -227,58 +186,34 @@ namespace AyaNova.Api.Controllers public async Task PutTagGroup([FromRoute] long id, [FromBody] TagGroup oIn) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } - - var oFromDb = await ct.TagGroup.SingleOrDefaultAsync(m => m.Id == id); - - if (oFromDb == null) - { - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } - - if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId)) - { - return StatusCode(401, new ApiNotAuthorizedResponse()); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); + var oFromDb = await biz.GetAsync(id); - if (!biz.Put(oFromDb, oIn)) - { - return BadRequest(new ApiErrorResponse(biz.Errors)); - } + if (oFromDb == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - //Log - EventLogProcessor.AddEntryToContextNoSave(new Event(biz.UserId, oFromDb.Id, AyaType.TagGroup, AyaEvent.Modified), ct); + if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId)) + return StatusCode(401, new ApiNotAuthorizedResponse()); try { - await ct.SaveChangesAsync(); + if (!biz.Put(oFromDb, oIn)) + return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) { - if (!TagGroupExists(id)) - { + if (!await biz.ExistsAsync(id)) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } else - { - //exists but was changed by another user - //I considered returning new and old record, but where would it end? - //Better to let the client decide what to do than to send extra data that is not required return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } } - return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken })); } @@ -294,59 +229,36 @@ namespace AyaNova.Api.Controllers [HttpPatch("{id}/{concurrencyToken}")] public async Task PatchTagGroup([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); - - var oFromDb = await ct.TagGroup.SingleOrDefaultAsync(m => m.Id == id); + var oFromDb = await biz.GetAsync(id); if (oFromDb == null) - { return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } - - //patch and validate - if (!biz.Patch(oFromDb, objectPatch, concurrencyToken)) - { - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - - //Log - EventLogProcessor.AddEntryToContextNoSave(new Event(biz.UserId, oFromDb.Id, AyaType.TagGroup, AyaEvent.Modified), ct); try { - await ct.SaveChangesAsync(); + if (!biz.Patch(oFromDb, objectPatch, concurrencyToken)) + return BadRequest(new ApiErrorResponse(biz.Errors)); } catch (DbUpdateConcurrencyException) { - if (!TagGroupExists(id)) - { + if (!await biz.ExistsAsync(id)) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } else - { return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } } - return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken })); } @@ -361,47 +273,29 @@ namespace AyaNova.Api.Controllers [HttpDelete("{id}")] public async Task DeleteTagGroup([FromRoute] long id) { - if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } - - var dbObj = await ct.TagGroup.SingleOrDefaultAsync(m => m.Id == id); - if (dbObj == null) - { - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } - - if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, AyaType.TagGroup, dbObj.OwnerId)) - { - return StatusCode(401, new ApiNotAuthorizedResponse()); - } //Instantiate the business object handler TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); + var dbObj = await biz.GetAsync(id); + if (dbObj == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + + if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, AyaType.TagGroup, dbObj.OwnerId)) + return StatusCode(401, new ApiNotAuthorizedResponse()); + if (!biz.Delete(dbObj)) - { return BadRequest(new ApiErrorResponse(biz.Errors)); - } - //Log - EventLogProcessor.DeleteObject(biz.UserId, AyaType.TagGroup, dbObj.Id, dbObj.Name, ct); - await ct.SaveChangesAsync(); - - return NoContent(); } - - /// /// Post TagMap from group - Map a group of tags to an object / Id /// Required roles: Same roles as tagged object diff --git a/server/AyaNova/biz/TagGroupBiz.cs b/server/AyaNova/biz/TagGroupBiz.cs index a1b43c27..4db41fb8 100644 --- a/server/AyaNova/biz/TagGroupBiz.cs +++ b/server/AyaNova/biz/TagGroupBiz.cs @@ -63,9 +63,15 @@ namespace AyaNova.Biz Name = inObj, OwnerId = UserId }; - - await ct.TagGroup.AddAsync(outObj); + + //EVENT LOG + EventLogProcessor.AddEntryToContextNoSave(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); + await ct.SaveChangesAsync(); + + //SEARCH INDEXING + Search.ProcessNewObjectKeywords(ct, UserLocaleId, outObj.Id, BizType, outObj.Name, outObj.Name); + return outObj; } } @@ -195,6 +201,12 @@ namespace AyaNova.Biz if (HasErrors) return false; + //Log modification + EventLogProcessor.AddEntryToContextNoSave(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); + ct.SaveChanges(); + //Update keywords + Search.ProcessUpdatedObjectKeywords(ct, UserLocaleId, dbObj.Id, BizType, dbObj.Name, dbObj.Name); + return true; } @@ -212,6 +224,13 @@ namespace AyaNova.Biz Validate(dbObj.Name, false); if (HasErrors) return false; + + //Log modification + EventLogProcessor.AddEntryToContextNoSave(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); + ct.SaveChanges(); + //Update keywords + Search.ProcessUpdatedObjectKeywords(ct, UserLocaleId, dbObj.Id, BizType, dbObj.Name, dbObj.Name); + return true; } @@ -229,6 +248,14 @@ namespace AyaNova.Biz return false; ct.Database.ExecuteSqlCommand($"delete from ataggroupmap where taggroupid = {dbObj.Id}"); ct.Database.ExecuteSqlCommand($"delete from ataggroup where id = {dbObj.Id}"); + + //Event log process delete + EventLogProcessor.DeleteObject(UserId, BizType, dbObj.Id, dbObj.Name, ct); + ct.SaveChanges(); + + //Delete search index + Search.ProcessDeletedObjectKeywords(ct, dbObj.Id, BizType); + return true; }