diff --git a/server/AyaNova/Controllers/WidgetController.cs b/server/AyaNova/Controllers/WidgetController.cs index ab685159..0ea9a363 100644 --- a/server/AyaNova/Controllers/WidgetController.cs +++ b/server/AyaNova/Controllers/WidgetController.cs @@ -47,8 +47,6 @@ namespace AyaNova.Api.Controllers } - - /// /// Get full widget object /// @@ -61,27 +59,21 @@ namespace AyaNova.Api.Controllers public async Task GetWidget([FromRoute] long id) { if (serverState.IsClosed) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } - var o = await biz.GetAsync(id); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + return Ok(new ApiOkResponse(o)); } @@ -98,24 +90,16 @@ namespace AyaNova.Api.Controllers public async Task ListWidgets([FromQuery] PagingOptions pagingOptions) { if (serverState.IsClosed) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } - - ApiPagedResponse pr = await biz.GetManyAsync(Url, nameof(ListWidgets), pagingOptions); return Ok(new ApiOkWithPagingResponse(pr)); @@ -141,14 +125,10 @@ namespace AyaNova.Api.Controllers public async Task WidgetPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions) { if (serverState.IsClosed) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); @@ -173,30 +153,20 @@ namespace AyaNova.Api.Controllers public async Task PutWidget([FromRoute] long id, [FromBody] Widget inObj) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } - - var o = await ct.Widget.SingleOrDefaultAsync(m => m.Id == id); - - if (o == null) - { - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); - if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) - { - return StatusCode(401, new ApiNotAuthorizedResponse()); - } + var o = await biz.GetNoLogAsync(id); + if (o == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) + return StatusCode(401, new ApiNotAuthorizedResponse()); try { @@ -205,17 +175,10 @@ namespace AyaNova.Api.Controllers } catch (DbUpdateConcurrencyException) { - if (!WidgetExists(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 = o.ConcurrencyToken })); } @@ -239,49 +202,34 @@ namespace AyaNova.Api.Controllers //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 WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); - var o = await ct.Widget.SingleOrDefaultAsync(m => m.Id == id); + var o = await biz.GetNoLogAsync(id); if (o == null) - { return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } try { //patch and validate if (!biz.Patch(o, objectPatch, concurrencyToken)) - { return BadRequest(new ApiErrorResponse(biz.Errors)); - } } catch (DbUpdateConcurrencyException) { - if (!WidgetExists(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 = o.ConcurrencyToken })); } @@ -298,37 +246,25 @@ namespace AyaNova.Api.Controllers public async Task PostWidget([FromBody] Widget inObj) { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); //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, biz.BizType)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!ModelState.IsValid) - { return BadRequest(new ApiErrorResponse(ModelState)); - } //Create and validate Widget o = await biz.CreateAsync(inObj); - if (o == null) - { - //error return return BadRequest(new ApiErrorResponse(biz.Errors)); - } else - { - //return success and link return CreatedAtAction("GetWidget", new { id = o.Id }, new ApiCreatedResponse(o)); - } + } @@ -347,47 +283,28 @@ namespace AyaNova.Api.Controllers public async Task DeleteWidget([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)); - } //Instantiate the business object handler WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); - var dbObj = await ct.Widget.SingleOrDefaultAsync(m => m.Id == id); + var dbObj = await biz.GetNoLogAsync(id); if (dbObj == null) - { return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - } if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, biz.BizType, dbObj.OwnerId)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } if (!biz.Delete(dbObj)) - { return BadRequest(new ApiErrorResponse(biz.Errors)); - } - return NoContent(); } - - - private bool WidgetExists(long id) - { - return ct.Widget.Any(e => e.Id == id); - } - - /// /// Get route that triggers exception for testing /// @@ -396,11 +313,7 @@ namespace AyaNova.Api.Controllers public ActionResult GetException() { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } - - throw new System.NotSupportedException("Test exception from widget controller"); } @@ -412,12 +325,7 @@ namespace AyaNova.Api.Controllers public ActionResult GetAltException() { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } - - - throw new System.ArgumentException("Test exception (ALT) from widget controller"); } @@ -430,14 +338,10 @@ namespace AyaNova.Api.Controllers public ActionResult TestWidgetJob() { if (!serverState.IsOpen) - { return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); - } if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.JobOperations)) - { return StatusCode(401, new ApiNotAuthorizedResponse()); - } //Create the job here OpsJob j = new OpsJob(); diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 0e131fea..b7b6d839 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -405,7 +405,7 @@ namespace AyaNova if (TESTING_REFRESH_DB) { AyaNova.Core.License.Fetch(apiServerState, dbContext, _log); - Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.HugeForLoadTest); + Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.SmallOneManShopTrialDataSet); } //TESTING #endif diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 757173f7..1a001d26 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -37,7 +37,20 @@ namespace AyaNova.Biz return new WidgetBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, AuthorizationRoles.BizAdminFull); } - + //////////////////////////////////////////////////////////////////////////////////////////////// + //EXISTS + internal async Task ExistsAsync(long id) + { + return await ct.Widget.AnyAsync(e => e.Id == id); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GET + internal async Task GetNoLogAsync(long fetchId) + { + //This is simple so nothing more here, but often will be copying to a different output object or some other ops + return await ct.Widget.SingleOrDefaultAsync(m => m.Id == fetchId); + } //////////////////////////////////////////////////////////////////////////////////////////////// //CREATE