This commit is contained in:
2018-10-04 19:14:47 +00:00
parent dfad4c948e
commit bbaaa26a0a
3 changed files with 26 additions and 109 deletions

View File

@@ -47,8 +47,6 @@ namespace AyaNova.Api.Controllers
} }
/// <summary> /// <summary>
/// Get full widget object /// Get full widget object
/// ///
@@ -61,27 +59,21 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> GetWidget([FromRoute] long id) public async Task<IActionResult> GetWidget([FromRoute] long id)
{ {
if (serverState.IsClosed) if (serverState.IsClosed)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
//Instantiate the business object handler //Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType)) if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType))
{
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));
}
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));
} }
@@ -98,24 +90,16 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> ListWidgets([FromQuery] PagingOptions pagingOptions) public async Task<IActionResult> ListWidgets([FromQuery] PagingOptions pagingOptions)
{ {
if (serverState.IsClosed) if (serverState.IsClosed)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
//Instantiate the business object handler //Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType)) if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, biz.BizType))
{
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));
}
ApiPagedResponse<Widget> pr = await biz.GetManyAsync(Url, nameof(ListWidgets), pagingOptions); ApiPagedResponse<Widget> pr = await biz.GetManyAsync(Url, nameof(ListWidgets), pagingOptions);
return Ok(new ApiOkWithPagingResponse<Widget>(pr)); return Ok(new ApiOkWithPagingResponse<Widget>(pr));
@@ -141,14 +125,10 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> WidgetPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions) public async Task<IActionResult> WidgetPickList([FromQuery] string q, [FromQuery] PagingOptions pagingOptions)
{ {
if (serverState.IsClosed) if (serverState.IsClosed)
{
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
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
@@ -173,30 +153,20 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> PutWidget([FromRoute] long id, [FromBody] Widget inObj) public async Task<IActionResult> PutWidget([FromRoute] long id, [FromBody] Widget 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 (!ModelState.IsValid) if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState)); 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 //Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext);
if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) var o = await biz.GetNoLogAsync(id);
{ if (o == null)
return StatusCode(401, new ApiNotAuthorizedResponse()); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId))
return StatusCode(401, new ApiNotAuthorizedResponse());
try try
{ {
@@ -205,17 +175,10 @@ namespace AyaNova.Api.Controllers
} }
catch (DbUpdateConcurrencyException) catch (DbUpdateConcurrencyException)
{ {
if (!WidgetExists(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 = o.ConcurrencyToken })); 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/ //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
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); 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) if (o == null)
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId)) if (!Authorized.IsAuthorizedToModify(HttpContext.Items, biz.BizType, o.OwnerId))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
try try
{ {
//patch and validate //patch and validate
if (!biz.Patch(o, objectPatch, concurrencyToken)) if (!biz.Patch(o, objectPatch, concurrencyToken))
{
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
}
} }
catch (DbUpdateConcurrencyException) catch (DbUpdateConcurrencyException)
{ {
if (!WidgetExists(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 = o.ConcurrencyToken })); return Ok(new ApiOkResponse(new { ConcurrencyToken = o.ConcurrencyToken }));
} }
@@ -298,37 +246,25 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> PostWidget([FromBody] Widget inObj) public async Task<IActionResult> PostWidget([FromBody] Widget 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));
}
//Instantiate the business object handler //Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); 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 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)) if (!Authorized.IsAuthorizedToCreate(HttpContext.Items, biz.BizType))
{
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));
}
//Create and validate //Create and validate
Widget o = await biz.CreateAsync(inObj); Widget o = await biz.CreateAsync(inObj);
if (o == null) if (o == null)
{
//error return
return BadRequest(new ApiErrorResponse(biz.Errors)); return BadRequest(new ApiErrorResponse(biz.Errors));
}
else else
{
//return success and link
return CreatedAtAction("GetWidget", new { id = o.Id }, new ApiCreatedResponse(o)); return CreatedAtAction("GetWidget", new { id = o.Id }, new ApiCreatedResponse(o));
}
} }
@@ -347,47 +283,28 @@ namespace AyaNova.Api.Controllers
public async Task<IActionResult> DeleteWidget([FromRoute] long id) public async Task<IActionResult> DeleteWidget([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));
}
//Instantiate the business object handler //Instantiate the business object handler
WidgetBiz biz = WidgetBiz.GetBiz(ct, HttpContext); 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) if (dbObj == null)
{
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
}
if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, biz.BizType, dbObj.OwnerId)) if (!Authorized.IsAuthorizedToDelete(HttpContext.Items, biz.BizType, dbObj.OwnerId))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); 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));
}
return NoContent(); return NoContent();
} }
private bool WidgetExists(long id)
{
return ct.Widget.Any(e => e.Id == id);
}
/// <summary> /// <summary>
/// Get route that triggers exception for testing /// Get route that triggers exception for testing
/// </summary> /// </summary>
@@ -396,11 +313,7 @@ namespace AyaNova.Api.Controllers
public ActionResult GetException() public ActionResult GetException()
{ {
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));
}
throw new System.NotSupportedException("Test exception from widget controller"); throw new System.NotSupportedException("Test exception from widget controller");
} }
@@ -412,12 +325,7 @@ namespace AyaNova.Api.Controllers
public ActionResult GetAltException() public ActionResult GetAltException()
{ {
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));
}
throw new System.ArgumentException("Test exception (ALT) from widget controller"); throw new System.ArgumentException("Test exception (ALT) from widget controller");
} }
@@ -430,14 +338,10 @@ namespace AyaNova.Api.Controllers
public ActionResult TestWidgetJob() public ActionResult TestWidgetJob()
{ {
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.IsAuthorizedToModify(HttpContext.Items, AyaType.JobOperations)) if (!Authorized.IsAuthorizedToModify(HttpContext.Items, AyaType.JobOperations))
{
return StatusCode(401, new ApiNotAuthorizedResponse()); return StatusCode(401, new ApiNotAuthorizedResponse());
}
//Create the job here //Create the job here
OpsJob j = new OpsJob(); OpsJob j = new OpsJob();

View File

@@ -405,7 +405,7 @@ namespace AyaNova
if (TESTING_REFRESH_DB) if (TESTING_REFRESH_DB)
{ {
AyaNova.Core.License.Fetch(apiServerState, dbContext, _log); AyaNova.Core.License.Fetch(apiServerState, dbContext, _log);
Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.HugeForLoadTest); Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.SmallOneManShopTrialDataSet);
} }
//TESTING //TESTING
#endif #endif

View File

@@ -37,7 +37,20 @@ namespace AyaNova.Biz
return new WidgetBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, AuthorizationRoles.BizAdminFull); return new WidgetBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID, AuthorizationRoles.BizAdminFull);
} }
////////////////////////////////////////////////////////////////////////////////////////////////
//EXISTS
internal async Task<bool> ExistsAsync(long id)
{
return await ct.Widget.AnyAsync(e => e.Id == id);
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// GET
internal async Task<Widget> 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 //CREATE