This commit is contained in:
2018-10-04 20:34:00 +00:00
parent 707e24291b
commit 179cc0a522
2 changed files with 56 additions and 135 deletions

View File

@@ -56,30 +56,20 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> GetTagGroup([FromRoute] long id) public async Task<IActionResult> GetTagGroup([FromRoute] long id)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup)) if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.TagGroup))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
var o = await biz.GetAsync(id); var o = await biz.GetAsync(id);
if (o == null) if (o == null)
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
return Ok(new ApiOkResponse(o)); return Ok(new ApiOkResponse(o));
} }
@@ -104,19 +94,13 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> TagGroupPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions) public async Task<IActionResult> TagGroupPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); 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 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()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
@@ -139,19 +123,13 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> TagsInGroupPickList([FromRoute] long id) public async Task<IActionResult> TagsInGroupPickList([FromRoute] long id)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); 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 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()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
@@ -172,43 +150,24 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> PostTagGroup([FromBody] NameItem inObj) public async Task<IActionResult> PostTagGroup([FromBody] NameItem inObj)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); 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 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)) if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, AyaType.TagGroup))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
//Create and validate //Create and validate
TagGroup o = await biz.CreateAsync(inObj.Name); TagGroup o = await biz.CreateAsync(inObj.Name);
if (o == null) if (o == null)
{
//error return
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
}
else 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)); return CreatedAtAction("GetTagGroup", new { id = o.Id }, new ApiCreatedResponse(o));
}
} }
@@ -227,58 +186,34 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> PutTagGroup([FromRoute] long id, [FromBody] TagGroup oIn) public async Task<IActionResult> PutTagGroup([FromRoute] long id, [FromBody] TagGroup oIn)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); 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 //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
var oFromDb = await biz.GetAsync(id);
if (!biz.Put(oFromDb, oIn)) if (oFromDb == null)
{ return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
return BadRequest(new ApiErrorResponse(biz.Errors));
}
//Log if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId))
EventLogProcessor.AddEntryToContextNoSave(new Event(biz.UserId, oFromDb.Id, AyaType.TagGroup, AyaEvent.Modified), ct); return StatusCode(401, new ApiNotAuthorizedResponse());
try try
{ {
await ct.SaveChangesAsync(); if (!biz.Put(oFromDb, oIn))
return BadRequest(new ApiErrorResponse(biz.Errors));
} }
catch (DbUpdateConcurrencyException) catch (DbUpdateConcurrencyException)
{ {
if (!TagGroupExists(id)) if (!await biz.ExistsAsync(id))
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
else 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 StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT));
}
} }
return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken })); return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken }));
} }
@@ -294,59 +229,36 @@ namespace AyaNova.Api.Controllers
[HttpPatch("{id}/{concurrencyToken}")] [HttpPatch("{id}/{concurrencyToken}")]
public async Task<IActionResult> PatchTagGroup([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument<TagGroup> objectPatch) public async Task<IActionResult> PatchTagGroup([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument<TagGroup> objectPatch)
{ {
//https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext);
var oFromDb = await biz.GetAsync(id);
var oFromDb = await ct.TagGroup.SingleOrDefaultAsync(m => m.Id == id);
if (oFromDb == null) if (oFromDb == null)
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId)) if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.TagGroup, oFromDb.OwnerId))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); 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 try
{ {
await ct.SaveChangesAsync(); if (!biz.Patch(oFromDb, objectPatch, concurrencyToken))
return BadRequest(new ApiErrorResponse(biz.Errors));
} }
catch (DbUpdateConcurrencyException) catch (DbUpdateConcurrencyException)
{ {
if (!TagGroupExists(id)) if (!await biz.ExistsAsync(id))
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
else else
{
return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT));
}
} }
return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken })); return Ok(new ApiOkResponse(new { ConcurrencyToken = oFromDb.ConcurrencyToken }));
} }
@@ -361,47 +273,29 @@ namespace AyaNova.Api.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> DeleteTagGroup([FromRoute] long id) public async Task<IActionResult> DeleteTagGroup([FromRoute] long id)
{ {
if (!serverState.IsOpen) if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); 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 //Instantiate the business object handler
TagGroupBiz biz = TagGroupBiz.GetBiz(ct, HttpContext); 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)) if (!biz.Delete(dbObj))
{
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
}
//Log
EventLogProcessor.DeleteObject(biz.UserId, AyaType.TagGroup, dbObj.Id, dbObj.Name, ct);
await ct.SaveChangesAsync();
return NoContent(); return NoContent();
} }
/// <summary> /// <summary>
/// Post TagMap from group - Map a group of tags to an object / Id /// Post TagMap from group - Map a group of tags to an object / Id
/// Required roles: Same roles as tagged object /// Required roles: Same roles as tagged object

View File

@@ -63,9 +63,15 @@ namespace AyaNova.Biz
Name = inObj, Name = inObj,
OwnerId = UserId OwnerId = UserId
}; };
await ct.TagGroup.AddAsync(outObj); 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; return outObj;
} }
} }
@@ -195,6 +201,12 @@ namespace AyaNova.Biz
if (HasErrors) if (HasErrors)
return false; 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; return true;
} }
@@ -212,6 +224,13 @@ namespace AyaNova.Biz
Validate(dbObj.Name, false); Validate(dbObj.Name, false);
if (HasErrors) if (HasErrors)
return false; 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; return true;
} }
@@ -229,6 +248,14 @@ namespace AyaNova.Biz
return false; return false;
ct.Database.ExecuteSqlCommand($"delete from ataggroupmap where taggroupid = {dbObj.Id}"); ct.Database.ExecuteSqlCommand($"delete from ataggroupmap where taggroupid = {dbObj.Id}");
ct.Database.ExecuteSqlCommand($"delete from ataggroup where id = {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; return true;
} }