This commit is contained in:
@@ -41,80 +41,52 @@ namespace AyaNova.Api.Controllers
|
|||||||
|
|
||||||
|
|
||||||
#region WorkOrder top level routes
|
#region WorkOrder top level routes
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create Workorder
|
/// Create WorkOrder "header"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="newObject"></param>
|
/// <param name="newObject">WorkOrder top level only, no descendents are evaluated</param>
|
||||||
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
||||||
/// <returns></returns>
|
/// <returns>WorkOrder "header" object</returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> PostWorkOrder([FromBody] WorkOrder newObject, ApiVersion apiVersion)
|
public async Task<IActionResult> PostWorkOrder([FromBody] WorkOrder newObject, ApiVersion apiVersion)
|
||||||
{
|
{
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
//Instantiate the business object handler
|
|
||||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||||
|
|
||||||
//If a user has change roles
|
|
||||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||||
|
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
return BadRequest(new ApiErrorResponse(ModelState));
|
return BadRequest(new ApiErrorResponse(ModelState));
|
||||||
|
|
||||||
//Create and validate
|
|
||||||
WorkOrder o = await biz.CreateAsync(newObject);
|
WorkOrder o = await biz.CreateAsync(newObject);
|
||||||
if (o == null)
|
if (o == null)
|
||||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||||
else
|
else
|
||||||
return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: CreateFromTemplate(templateid)
|
|
||||||
//TODO: Createfromquote(quoteid)
|
|
||||||
//todo: createfrompm(pmid)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Duplicate WorkOrder
|
/// Duplicate WorkOrder
|
||||||
|
/// (Wiki and Attachments are not duplicated)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">Create a duplicate of this items id</param>
|
/// <param name="id">Source object id</param>
|
||||||
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
||||||
/// <returns></returns>
|
/// <returns>Full WorkOrder object including all descendents</returns>
|
||||||
[HttpPost("duplicate/{id}")]
|
[HttpPost("duplicate/{id}")]
|
||||||
public async Task<IActionResult> DuplicateWorkOrder([FromRoute] long id, ApiVersion apiVersion)
|
public async Task<IActionResult> DuplicateWorkOrder([FromRoute] long id, ApiVersion apiVersion)
|
||||||
{
|
{
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
//Instantiate the business object handler
|
|
||||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||||
|
|
||||||
//If a user has change roles
|
|
||||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||||
|
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
return BadRequest(new ApiErrorResponse(ModelState));
|
return BadRequest(new ApiErrorResponse(ModelState));
|
||||||
|
|
||||||
// var oSrc = await biz.GetAsync(id, false);
|
|
||||||
// if (oSrc == null)
|
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
||||||
|
|
||||||
//Create and validate
|
|
||||||
WorkOrder o = await biz.DuplicateAsync(id);
|
WorkOrder o = await biz.DuplicateAsync(id);
|
||||||
if (o == null)
|
if (o == null)
|
||||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||||
else
|
else
|
||||||
return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -122,48 +94,31 @@ namespace AyaNova.Api.Controllers
|
|||||||
/// Get full WorkOrder object
|
/// Get full WorkOrder object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns>A single WorkOrder</returns>
|
/// <returns>Entire WorkOrder object including all descendents</returns>
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<IActionResult> GetWorkOrder([FromRoute] long id)
|
public async Task<IActionResult> GetWorkOrder([FromRoute] long id)
|
||||||
{
|
{
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
//Instantiate the business object handler
|
|
||||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||||
|
|
||||||
//NOTE: This is the first check and often the only check but in some cases with some objects this will also need to check biz object rules
|
|
||||||
if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
|
if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
|
||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
return StatusCode(403, 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));
|
|
||||||
|
|
||||||
// NOTE: HERE would be the second check of biz rules before returning the object
|
|
||||||
// in cases where there is also a business rule to affect retrieval on top of basic rights
|
|
||||||
//NO, NOT HERE, in biz object surely?
|
|
||||||
|
|
||||||
return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType)));
|
return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType)));
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: GET BY RELATIVE
|
|
||||||
//get by descendent type and id
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Put (update) WorkOrder
|
/// Put (update) WorkOrder
|
||||||
/// This route does not update the entire WorkOrder object graph, only the top level
|
/// This route does not evaluate nor update nor return the entire WorkOrder object graph
|
||||||
/// in other words nothing in any descendent child collections such as WorkOrderItems and below.
|
/// only the "header" or top level
|
||||||
/// Use the separate routes to update descendents of the WorkOrder
|
/// Descendent objects must be updated via their individual routes seperately
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">WorkOrder id</param>
|
/// <param name="id">WorkOrder id</param>
|
||||||
/// <param name="updatedObject">Workorder top level only, no descendents are evaluated</param>
|
/// <param name="updatedObject">WorkOrder top level only, no descendents are evaluated</param>
|
||||||
/// <returns>New concurrency token</returns>
|
/// <returns>New concurrency token</returns>
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
public async Task<IActionResult> PutWorkOrder([FromRoute] long id, [FromBody] WorkOrder updatedObject)
|
public async Task<IActionResult> PutWorkOrder([FromRoute] long id, [FromBody] WorkOrder updatedObject)
|
||||||
@@ -187,82 +142,55 @@ namespace AyaNova.Api.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: will need to traverse, don't need it now for PROPOSAL testing so not coding it yet
|
||||||
|
// /// <summary>
|
||||||
|
// /// Delete WorkOrder
|
||||||
|
// /// </summary>
|
||||||
|
// /// <param name="id"></param>
|
||||||
|
// /// <returns>Ok</returns>
|
||||||
|
// [HttpDelete("{id}")]
|
||||||
|
// public async Task<IActionResult> DeleteWorkOrder([FromRoute] long id)
|
||||||
|
// {
|
||||||
|
// if (!serverState.IsOpen)
|
||||||
|
// return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
|
// if (!ModelState.IsValid)
|
||||||
|
// return BadRequest(new ApiErrorResponse(ModelState));
|
||||||
|
|
||||||
|
// //Instantiate the business object handler
|
||||||
|
// WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||||
|
|
||||||
|
// var o = await biz.GetAsync(id, false);
|
||||||
|
// if (o == null)
|
||||||
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
|
// if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType))
|
||||||
|
// return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||||
|
|
||||||
|
// if (!await biz.DeleteAsync(o))
|
||||||
|
// return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||||
|
|
||||||
/// <summary>
|
// return NoContent();
|
||||||
/// Delete WorkOrder
|
// }
|
||||||
/// </summary>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <returns>Ok</returns>
|
|
||||||
[HttpDelete("{id}")]
|
|
||||||
public async Task<IActionResult> DeleteWorkOrder([FromRoute] long id)
|
|
||||||
{
|
|
||||||
if (!serverState.IsOpen)
|
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
|
||||||
|
|
||||||
if (!ModelState.IsValid)
|
|
||||||
return BadRequest(new ApiErrorResponse(ModelState));
|
|
||||||
|
|
||||||
//Instantiate the business object handler
|
|
||||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
|
||||||
|
|
||||||
var o = await biz.GetAsync(id, false);
|
|
||||||
if (o == null)
|
|
||||||
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
|
||||||
|
|
||||||
if (!Authorized.HasDeleteRole(HttpContext.Items, biz.BizType))
|
|
||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
|
||||||
|
|
||||||
if (!await biz.DeleteAsync(o))
|
|
||||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
|
||||||
|
|
||||||
return NoContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion WorkorderTopLevel routes
|
|
||||||
|
|
||||||
#region WorkOrder Item
|
|
||||||
//TODO: Plot exact routes needed here, not all are needed, particularly the collection ones
|
|
||||||
//but maybe handy? Like do I need delete on entire woitems collection?
|
|
||||||
//WorkOrder/{woid}/WorkorderItems <- all workorderitems, post to add new, put to update all as a collection
|
|
||||||
//WorkOrder/{woid}/WorkOrderItems/{woitemid} <- CRUD single woitemid
|
|
||||||
//https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design#define-operations-in-terms-of-http-methods
|
|
||||||
/*
|
|
||||||
Resource POST GET PUT DELETE
|
|
||||||
/customers Create a new customer Retrieve all customers Bulk update of customers Remove all customers
|
|
||||||
/customers/1 Error Retrieve the details for customer 1 Update the details of customer 1 if it exists Remove customer 1
|
|
||||||
/customers/1/orders Create a new order for customer 1 Retrieve all orders for customer 1 Bulk update of orders for customer 1 Remove all orders for customer 1
|
|
||||||
*/
|
|
||||||
|
|
||||||
//So Post into a collection means create one item in that collection, never a whole collection being created at once
|
|
||||||
//GET PUT and DELETE on a collecdtion always mean the entire collection, POST is the outlier here
|
|
||||||
|
|
||||||
|
|
||||||
|
#endregion WorkOrderTopLevel routes
|
||||||
|
|
||||||
|
#region WorkOrderItem
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create WorkOrderItem
|
/// Create WorkOrderItem
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="newObject"></param>
|
/// <param name="newObject">WorkOrderItem level only no descendents</param>
|
||||||
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
|
||||||
/// <returns></returns>
|
/// <returns>WorkOrderItem object (no descendents)</returns>
|
||||||
[HttpPost("items")]
|
[HttpPost("items")]
|
||||||
public async Task<IActionResult> PostWorkOrderItem([FromBody] WorkOrderItem newObject, ApiVersion apiVersion)
|
public async Task<IActionResult> PostWorkOrderItem([FromBody] WorkOrderItem newObject, ApiVersion apiVersion)
|
||||||
{
|
{
|
||||||
//NOTE: we don't need the workorder id in the route because the workorder item must contain the workorder id anyway
|
|
||||||
|
|
||||||
|
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
//Instantiate the business object handler
|
|
||||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||||
|
|
||||||
//If a user has change roles
|
//If a user has change roles
|
||||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItem))
|
||||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||||
|
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
@@ -370,8 +298,8 @@ namespace AyaNova.Api.Controllers
|
|||||||
{
|
{
|
||||||
//NOTE: we don't need the workorder id in the route because the workorder item must contain the workorder id anyway
|
//NOTE: we don't need the workorder id in the route because the workorder item must contain the workorder id anyway
|
||||||
|
|
||||||
//WorkOrder/{woid}/WorkorderItems <- all workorderitems, post to add new, put to update all as a collection
|
//WorkOrder/{woid}/WorkOrderItems <- all workorderitems, post to add new, put to update all as a collection
|
||||||
//WorkOrder/{WorkOrderId}/WorkorderItems
|
//WorkOrder/{WorkOrderId}/WorkOrderItems
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||||
|
|
||||||
@@ -393,7 +321,7 @@ namespace AyaNova.Api.Controllers
|
|||||||
// if (o == null)
|
// if (o == null)
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
// //Make sure the item exists first before getting into it
|
// //Make sure the item exists first before getting into it
|
||||||
// if (!o.WorkorderItems.Exists(m => m.Id == workOrderItemId))
|
// if (!o.WorkOrderItems.Exists(m => m.Id == workOrderItemId))
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
|
|
||||||
@@ -558,13 +486,13 @@ namespace AyaNova.Api.Controllers
|
|||||||
// if (o == null)
|
// if (o == null)
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
// //Get WorkorderItem
|
// //Get WorkOrderItem
|
||||||
// var woitem = o.WorkorderItems.FirstOrDefault(m => m.Id == workOrderItemId);
|
// var woitem = o.WorkOrderItems.FirstOrDefault(m => m.Id == workOrderItemId);
|
||||||
// if (woitem == null)
|
// if (woitem == null)
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
// //Get WorkOrderItemLabor
|
// //Get WorkOrderItemLabor
|
||||||
// var woitemlabor = woitem.WorkorderItemLabors.FirstOrDefault(m => m.Id == workOrderItemLaborId);
|
// var woitemlabor = woitem.WorkOrderItemLabors.FirstOrDefault(m => m.Id == workOrderItemLaborId);
|
||||||
// if (woitem == null)
|
// if (woitem == null)
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
@@ -729,7 +657,7 @@ namespace AyaNova.Api.Controllers
|
|||||||
// if (o == null)
|
// if (o == null)
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
// //Make sure the item exists first before getting into it
|
// //Make sure the item exists first before getting into it
|
||||||
// if (!o.WorkorderItems.Exists(m => m.Id == workOrderItemId))
|
// if (!o.WorkOrderItems.Exists(m => m.Id == workOrderItemId))
|
||||||
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
// return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,9 @@ namespace AyaNova.Biz
|
|||||||
//
|
//
|
||||||
internal async Task<WorkOrder> GetAsync(long id, bool logTheGetEvent = true)
|
internal async Task<WorkOrder> GetAsync(long id, bool logTheGetEvent = true)
|
||||||
{
|
{
|
||||||
|
//Note: there could be rules checking here in future, i.e. can only get own workorder or something
|
||||||
|
//if so, then need to implement AddError and in route handle Null return with Error check just like PUT route does now
|
||||||
|
|
||||||
//https://docs.microsoft.com/en-us/ef/core/querying/related-data
|
//https://docs.microsoft.com/en-us/ef/core/querying/related-data
|
||||||
//docs say this will not query twice but will recognize the duplicate woitem bit which is required for multiple grandchild collections
|
//docs say this will not query twice but will recognize the duplicate woitem bit which is required for multiple grandchild collections
|
||||||
var ret =
|
var ret =
|
||||||
|
|||||||
Reference in New Issue
Block a user