using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;


namespace AyaNova.Api.Controllers
{

    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/workorder")]
    [Produces("application/json")]
    [Authorize]
    public class WorkOrderController : ControllerBase
    {
        private readonly AyContext ct;
        private readonly ILogger<WorkOrderController> log;
        private readonly ApiServerState serverState;


        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="dbcontext"></param>
        /// <param name="logger"></param>
        /// <param name="apiServerState"></param>  
        public WorkOrderController(AyContext dbcontext, ILogger<WorkOrderController> logger, ApiServerState apiServerState)
        {
            ct = dbcontext;
            log = logger;
            serverState = apiServerState;
        }



/*
    ██╗    ██╗ ██████╗ ██████╗ ██╗  ██╗      ██████╗ ██████╗ ██████╗ ███████╗██████╗ 
    ██║    ██║██╔═══██╗██╔══██╗██║ ██╔╝     ██╔═══██╗██╔══██╗██╔══██╗██╔════╝██╔══██╗
    ██║ █╗ ██║██║   ██║██████╔╝█████╔╝█████╗██║   ██║██████╔╝██║  ██║█████╗  ██████╔╝
    ██║███╗██║██║   ██║██╔══██╗██╔═██╗╚════╝██║   ██║██╔══██╗██║  ██║██╔══╝  ██╔══██╗
    ╚███╔███╔╝╚██████╔╝██║  ██║██║  ██╗     ╚██████╔╝██║  ██║██████╔╝███████╗██║  ██║
        ╚══╝╚══╝  ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝      ╚═════╝ ╚═╝  ╚═╝╚═════╝ ╚══════╝╚═╝  ╚═╝
*/

        #region WorkOrder top level routes
        /// <summary>
        /// Create WorkOrder
        /// </summary>
        /// <param name="newObject">WorkOrder - top level only, no descendents</param>
        /// <param name="apiVersion">From route path</param>
        /// <returns>WorkOrder</returns>
        [HttpPost]
        public async Task<IActionResult> PostWorkOrder([FromBody] dtWorkOrder newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrder o = await biz.WorkOrderCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Duplicate WorkOrder
        /// (Wiki and Attachments are not duplicated)
        /// </summary>
        /// <param name="id">Source object id</param>
        /// <param name="apiVersion">From route path</param>
        /// <returns>WorkOrder</returns>
        [HttpPost("duplicate/{id}")]
        public async Task<IActionResult> DuplicateWorkOrder([FromRoute] long id, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrder o = await biz.WorkOrderDuplicateAsync(id);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrder), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrder
        /// </summary>
        /// <param name="id"></param>
        /// <returns>WorkOrder</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetWorkOrder([FromRoute] long id)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.WorkOrderGetAsync(id);
            if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }


        /// <summary>
        /// Put (update) WorkOrder        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrder - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut]
        public async Task<IActionResult> PutWorkOrder([FromBody] dtWorkOrder updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.WorkOrderPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrder
        /// </summary>
        /// <param name="id"></param>
        /// <returns>NoContent</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));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrder))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.WorkOrderDeleteAsync(id))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

         /// <summary>
        /// ReStart serial number
        /// 
        /// </summary>
        /// <param name="newSerial">Next starting value for auto generated serial numbers</param>
        /// <param name="apiVersion">From route path</param>
        /// <returns>Widget</returns>
        [HttpPost("restart-serial/{newSerial}")]
        public async Task<IActionResult> ReStartSerial([FromRoute] long newSerial, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            bool result=await biz.RestartSerial(newSerial);
            if (!result)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return NoContent();
        }

        #endregion WorkOrderTopLevel routes


/*
    ██╗████████╗███████╗███╗   ███╗███████╗
    ██║╚══██╔══╝██╔════╝████╗ ████║██╔════╝
    ██║   ██║   █████╗  ██╔████╔██║███████╗
    ██║   ██║   ██╔══╝  ██║╚██╔╝██║╚════██║
    ██║   ██║   ███████╗██║ ╚═╝ ██║███████║
    ╚═╝   ╚═╝   ╚══════╝╚═╝     ╚═╝╚══════╝
*/

        #region WorkOrderItem
        /// <summary>
        /// Create WorkOrderItem
        /// </summary>       
        /// <param name="newObject">WorkOrderItem - no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItem object</returns>
        [HttpPost("items")]
        public async Task<IActionResult> PostWorkOrderItem([FromBody] dtWorkOrderItem newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItem))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItem o = await biz.ItemCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItem), new { WorkOrderItemId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItem object
        /// </summary>
        /// <param name="WorkOrderItemId"></param>
        /// <returns>A single WorkOrderItem</returns>
        [HttpGet("items/{WorkOrderItemId}")]
        public async Task<IActionResult> GetWorkOrderItem([FromRoute] long WorkOrderItemId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItem))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.ItemGetAsync(WorkOrderItemId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }



        /// <summary>
        /// Put (update) WorkOrderItem        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItem - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/")]
        public async Task<IActionResult> PutWorkOrderItem([FromBody] dtWorkOrderItem updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItem))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.ItemPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }


        /// <summary>
        /// Delete WorkOrderItem
        /// </summary>
        /// <param name="WorkOrderItemId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/{WorkOrderItemId}")]
        public async Task<IActionResult> DeleteWorkOrderItem([FromRoute] long WorkOrderItemId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItem))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.ItemDeleteAsync(WorkOrderItemId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion workorderitem


/*
    ███████╗██╗  ██╗██████╗ ███████╗███╗   ██╗███████╗███████╗███████╗
    ██╔════╝╚██╗██╔╝██╔══██╗██╔════╝████╗  ██║██╔════╝██╔════╝██╔════╝
    █████╗   ╚███╔╝ ██████╔╝█████╗  ██╔██╗ ██║███████╗█████╗  ███████╗
    ██╔══╝   ██╔██╗ ██╔═══╝ ██╔══╝  ██║╚██╗██║╚════██║██╔══╝  ╚════██║
    ███████╗██╔╝ ██╗██║     ███████╗██║ ╚████║███████║███████╗███████║
    ╚══════╝╚═╝  ╚═╝╚═╝     ╚══════╝╚═╝  ╚═══╝╚══════╝╚══════╝╚══════╝
*/

        #region WorkOrderItemExpense

        /// <summary>
        /// Create WorkOrderItemExpense
        /// </summary>       
        /// <param name="newObject">WorkOrderItemExpense level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemExpense object (no descendents)</returns>
        [HttpPost("items/expenses")]
        public async Task<IActionResult> PostWorkOrderItemExpense([FromBody] WorkOrderItemExpense newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemExpense))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemExpense o = await biz.ExpenseCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemExpense), new { WorkOrderItemExpenseId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemExpense object
        /// </summary>
        /// <param name="WorkOrderItemExpenseId"></param>
        /// <returns>A single WorkOrderItemExpense</returns>
        [HttpGet("items/expenses/{WorkOrderItemExpenseId}")]
        public async Task<IActionResult> GetWorkOrderItemExpense([FromRoute] long WorkOrderItemExpenseId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemExpense))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.ExpenseGetAsync(WorkOrderItemExpenseId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemExpense        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemExpense - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/expenses")]
        public async Task<IActionResult> PutWorkOrderItemExpense([FromBody] WorkOrderItemExpense updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemExpense))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.ExpensePutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemExpense
        /// </summary>
        /// <param name="WorkOrderItemExpenseId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/expenses/{WorkOrderItemExpenseId}")]
        public async Task<IActionResult> DeleteWorkOrderItemExpense([FromRoute] long WorkOrderItemExpenseId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemExpense))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.ExpenseDeleteAsync(WorkOrderItemExpenseId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemExpense


/*
    ██╗      █████╗ ██████╗  ██████╗ ██████╗ 
    ██║     ██╔══██╗██╔══██╗██╔═══██╗██╔══██╗
    ██║     ███████║██████╔╝██║   ██║██████╔╝
    ██║     ██╔══██║██╔══██╗██║   ██║██╔══██╗
    ███████╗██║  ██║██████╔╝╚██████╔╝██║  ██║
    ╚══════╝╚═╝  ╚═╝╚═════╝  ╚═════╝ ╚═╝  ╚═╝

*/

        #region WorkOrderItemLabor

        /// <summary>
        /// Create WorkOrderItemLabor
        /// </summary>       
        /// <param name="newObject">WorkOrderItemLabor level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemLabor object (no descendents)</returns>
        [HttpPost("items/labors")]
        public async Task<IActionResult> PostWorkOrderItemLabor([FromBody] WorkOrderItemLabor newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemLabor))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemLabor o = await biz.LaborCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemLabor), new { WorkOrderItemLaborId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemLabor object
        /// </summary>
        /// <param name="workOrderItemLaborId"></param>
        /// <returns>A single WorkOrderItemLabor</returns>
        [HttpGet("items/labors/{workOrderItemLaborId}")]
        public async Task<IActionResult> GetWorkOrderItemLabor([FromRoute] long workOrderItemLaborId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemLabor))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.LaborGetAsync(workOrderItemLaborId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }


        /// <summary>
        /// Put (update) WorkOrderItemLabor        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemLabor - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/labors")]
        public async Task<IActionResult> PutWorkOrderItemLabor([FromBody] WorkOrderItemLabor updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemLabor))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.LaborPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }


        /// <summary>
        /// Delete WorkOrderItemLabor
        /// </summary>
        /// <param name="workOrderItemLaborId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/labors/{workOrderItemLaborId}")]
        public async Task<IActionResult> DeleteWorkOrderItemLabor([FromRoute] long workOrderItemLaborId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemLabor))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.LaborDeleteAsync(workOrderItemLaborId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }


        #endregion WorkOrderItemLabor


/*
    ██╗      ██████╗  █████╗ ███╗   ██╗
    ██║     ██╔═══██╗██╔══██╗████╗  ██║
    ██║     ██║   ██║███████║██╔██╗ ██║
    ██║     ██║   ██║██╔══██║██║╚██╗██║
    ███████╗╚██████╔╝██║  ██║██║ ╚████║
    ╚══════╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═══╝
*/

        #region WorkOrderItemLoan

        /// <summary>
        /// Create WorkOrderItemLoan
        /// </summary>       
        /// <param name="newObject">WorkOrderItemLoan level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemLoan object (no descendents)</returns>
        [HttpPost("items/loans")]
        public async Task<IActionResult> PostWorkOrderItemLoan([FromBody] WorkOrderItemLoan newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemLoan))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemLoan o = await biz.LoanCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemLoan), new { WorkOrderItemLoanId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemLoan object
        /// </summary>
        /// <param name="WorkOrderItemLoanId"></param>
        /// <returns>A single WorkOrderItemLoan</returns>
        [HttpGet("items/loans/{WorkOrderItemLoanId}")]
        public async Task<IActionResult> GetWorkOrderItemLoan([FromRoute] long WorkOrderItemLoanId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemLoan))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.LoanGetAsync(WorkOrderItemLoanId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemLoan        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemLoan - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/loans")]
        public async Task<IActionResult> PutWorkOrderItemLoan([FromBody] WorkOrderItemLoan updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemLoan))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.LoanPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemLoan
        /// </summary>
        /// <param name="WorkOrderItemLoanId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/loans/{WorkOrderItemLoanId}")]
        public async Task<IActionResult> DeleteWorkOrderItemLoan([FromRoute] long WorkOrderItemLoanId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemLoan))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.LoanDeleteAsync(WorkOrderItemLoanId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemLoan


/*
    ██████╗  █████╗ ██████╗ ████████╗███████╗
    ██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝
    ██████╔╝███████║██████╔╝   ██║   ███████╗
    ██╔═══╝ ██╔══██║██╔══██╗   ██║   ╚════██║
    ██║     ██║  ██║██║  ██║   ██║   ███████║
    ╚═╝     ╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝   ╚══════╝
*/

        #region WorkOrderItemPart

        /// <summary>
        /// Create WorkOrderItemPart
        /// </summary>       
        /// <param name="newObject">WorkOrderItemPart level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemPart object (no descendents)</returns>
        [HttpPost("items/parts")]
        public async Task<IActionResult> PostWorkOrderItemPart([FromBody] WorkOrderItemPart newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemPart))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemPart o = await biz.CreatePartAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemPart), new { WorkOrderItemPartId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemPart object
        /// </summary>
        /// <param name="WorkOrderItemPartId"></param>
        /// <returns>A single WorkOrderItemPart</returns>
        [HttpGet("items/parts/{WorkOrderItemPartId}")]
        public async Task<IActionResult> GetWorkOrderItemPart([FromRoute] long WorkOrderItemPartId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemPart))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.PartGetAsync(WorkOrderItemPartId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemPart        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemPart - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/parts")]
        public async Task<IActionResult> PutWorkOrderItemPart([FromBody] WorkOrderItemPart updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemPart))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.PartPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemPart
        /// </summary>
        /// <param name="WorkOrderItemPartId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/parts/{WorkOrderItemPartId}")]
        public async Task<IActionResult> DeleteWorkOrderItemPart([FromRoute] long WorkOrderItemPartId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemPart))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.PartDeleteAsync(WorkOrderItemPartId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemPart


/*     
    ██████╗  █████╗ ██████╗ ████████╗   ██████╗ ███████╗ ██████╗ ██╗   ██╗███████╗███████╗████████╗███████╗
    ██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝   ██╔══██╗██╔════╝██╔═══██╗██║   ██║██╔════╝██╔════╝╚══██╔══╝██╔════╝
    ██████╔╝███████║██████╔╝   ██║█████╗██████╔╝█████╗  ██║   ██║██║   ██║█████╗  ███████╗   ██║   ███████╗
    ██╔═══╝ ██╔══██║██╔══██╗   ██║╚════╝██╔══██╗██╔══╝  ██║▄▄ ██║██║   ██║██╔══╝  ╚════██║   ██║   ╚════██║
    ██║     ██║  ██║██║  ██║   ██║      ██║  ██║███████╗╚██████╔╝╚██████╔╝███████╗███████║   ██║   ███████║
    ╚═╝     ╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝      ╚═╝  ╚═╝╚══════╝ ╚══▀▀═╝  ╚═════╝ ╚══════╝╚══════╝   ╚═╝   ╚══════╝
*/

        #region WorkOrderItemPartRequest

        /// <summary>
        /// Create WorkOrderItemPartRequest
        /// </summary>       
        /// <param name="newObject">WorkOrderItemPartRequest level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemPartRequest object (no descendents)</returns>
        [HttpPost("items/partrequests")]
        public async Task<IActionResult> PostWorkOrderItemPartRequest([FromBody] WorkOrderItemPartRequest newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemPartRequest))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemPartRequest o = await biz.PartRequestCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemPartRequest), new { WorkOrderItemPartRequestId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemPartRequest object
        /// </summary>
        /// <param name="WorkOrderItemPartRequestId"></param>
        /// <returns>A single WorkOrderItemPartRequest</returns>
        [HttpGet("items/partrequests/{WorkOrderItemPartRequestId}")]
        public async Task<IActionResult> GetWorkOrderItemPartRequest([FromRoute] long WorkOrderItemPartRequestId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemPartRequest))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.PartRequestGetAsync(WorkOrderItemPartRequestId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemPartRequest        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemPartRequest - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/partrequests")]
        public async Task<IActionResult> PutWorkOrderItemPartRequest([FromBody] WorkOrderItemPartRequest updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemPartRequest))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.PartRequestPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemPartRequest
        /// </summary>
        /// <param name="WorkOrderItemPartRequestId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/partrequests/{WorkOrderItemPartRequestId}")]
        public async Task<IActionResult> DeleteWorkOrderItemPartRequest([FromRoute] long WorkOrderItemPartRequestId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemPartRequest))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.PartRequestDeleteAsync(WorkOrderItemPartRequestId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemPartRequest


/*
    ███████╗ ██████╗██╗  ██╗███████╗██████╗ ██╗   ██╗██╗     ███████╗██████╗       ██╗   ██╗███████╗███████╗██████╗ ███████╗
    ██╔════╝██╔════╝██║  ██║██╔════╝██╔══██╗██║   ██║██║     ██╔════╝██╔══██╗      ██║   ██║██╔════╝██╔════╝██╔══██╗██╔════╝
    ███████╗██║     ███████║█████╗  ██║  ██║██║   ██║██║     █████╗  ██║  ██║█████╗██║   ██║███████╗█████╗  ██████╔╝███████╗
    ╚════██║██║     ██╔══██║██╔══╝  ██║  ██║██║   ██║██║     ██╔══╝  ██║  ██║╚════╝██║   ██║╚════██║██╔══╝  ██╔══██╗╚════██║
    ███████║╚██████╗██║  ██║███████╗██████╔╝╚██████╔╝███████╗███████╗██████╔╝      ╚██████╔╝███████║███████╗██║  ██║███████║
    ╚══════╝ ╚═════╝╚═╝  ╚═╝╚══════╝╚═════╝  ╚═════╝ ╚══════╝╚══════╝╚═════╝        ╚═════╝ ╚══════╝╚══════╝╚═╝  ╚═╝╚══════╝
*/

        #region WorkOrderItemScheduledUser

        /// <summary>
        /// Create WorkOrderItemScheduledUser
        /// </summary>       
        /// <param name="newObject">WorkOrderItemScheduledUser level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemScheduledUser object (no descendents)</returns>
        [HttpPost("items/scheduledusers")]
        public async Task<IActionResult> PostWorkOrderItemScheduledUser([FromBody] WorkOrderItemScheduledUser newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemScheduledUser))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemScheduledUser o = await biz.ScheduledUserCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemScheduledUser), new { WorkOrderItemScheduledUserId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemScheduledUser object
        /// </summary>
        /// <param name="WorkOrderItemScheduledUserId"></param>
        /// <returns>A single WorkOrderItemScheduledUser</returns>
        [HttpGet("items/scheduledusers/{WorkOrderItemScheduledUserId}")]
        public async Task<IActionResult> GetWorkOrderItemScheduledUser([FromRoute] long WorkOrderItemScheduledUserId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemScheduledUser))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.ScheduledUserGetAsync(WorkOrderItemScheduledUserId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemScheduledUser        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemScheduledUser - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/scheduledusers")]
        public async Task<IActionResult> PutWorkOrderItemScheduledUser([FromBody] WorkOrderItemScheduledUser updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemScheduledUser))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.ScheduledUserPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemScheduledUser
        /// </summary>
        /// <param name="WorkOrderItemScheduledUserId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/scheduledusers/{WorkOrderItemScheduledUserId}")]
        public async Task<IActionResult> DeleteWorkOrderItemScheduledUser([FromRoute] long WorkOrderItemScheduledUserId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemScheduledUser))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.ScheduledUserDeleteAsync(WorkOrderItemScheduledUserId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemScheduledUser


/*
    ████████╗ █████╗ ███████╗██╗  ██╗
    ╚══██╔══╝██╔══██╗██╔════╝██║ ██╔╝
       ██║   ███████║███████╗█████╔╝ 
       ██║   ██╔══██║╚════██║██╔═██╗ 
       ██║   ██║  ██║███████║██║  ██╗
       ╚═╝   ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝
*/

        #region WorkOrderItemtask

        /// <summary>
        /// Create WorkOrderItemTask
        /// </summary>       
        /// <param name="newObject">WorkOrderItemTask level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemTask object (no descendents)</returns>
        [HttpPost("items/tasks")]
        public async Task<IActionResult> PostWorkOrderItemTask([FromBody] WorkOrderItemTask newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemTask))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemTask o = await biz.TaskCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemTask), new { WorkOrderItemTaskId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemTask object
        /// </summary>
        /// <param name="WorkOrderItemTaskId"></param>
        /// <returns>A single WorkOrderItemTask</returns>
        [HttpGet("items/tasks/{WorkOrderItemTaskId}")]
        public async Task<IActionResult> GetWorkOrderItemTask([FromRoute] long WorkOrderItemTaskId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemTask))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.TaskGetAsync(WorkOrderItemTaskId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemTask        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemTask - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/tasks")]
        public async Task<IActionResult> PutWorkOrderItemTask([FromBody] WorkOrderItemTask updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemTask))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.TaskPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemTask
        /// </summary>
        /// <param name="WorkOrderItemTaskId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/tasks/{WorkOrderItemTaskId}")]
        public async Task<IActionResult> DeleteWorkOrderItemTask([FromRoute] long WorkOrderItemTaskId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemTask))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.TaskDeleteAsync(WorkOrderItemTaskId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemTask


/*
    ████████╗██████╗  █████╗ ██╗   ██╗███████╗██╗     
    ╚══██╔══╝██╔══██╗██╔══██╗██║   ██║██╔════╝██║     
       ██║   ██████╔╝███████║██║   ██║█████╗  ██║     
       ██║   ██╔══██╗██╔══██║╚██╗ ██╔╝██╔══╝  ██║     
       ██║   ██║  ██║██║  ██║ ╚████╔╝ ███████╗███████╗
       ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝  ╚═══╝  ╚══════╝╚══════╝
*/

        #region WorkOrderItemTravel

        /// <summary>
        /// Create WorkOrderItemTravel
        /// </summary>       
        /// <param name="newObject">WorkOrderItemTravel level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemTravel object (no descendents)</returns>
        [HttpPost("items/travels")]
        public async Task<IActionResult> PostWorkOrderItemTravel([FromBody] WorkOrderItemTravel newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemTravel))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemTravel o = await biz.TravelCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemTravel), new { WorkOrderItemTravelId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemTravel object
        /// </summary>
        /// <param name="WorkOrderItemTravelId"></param>
        /// <returns>A single WorkOrderItemTravel</returns>
        [HttpGet("items/travels/{WorkOrderItemTravelId}")]
        public async Task<IActionResult> GetWorkOrderItemTravel([FromRoute] long WorkOrderItemTravelId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemTravel))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.TravelGetAsync(WorkOrderItemTravelId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemTravel        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemTravel - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/travels")]
        public async Task<IActionResult> PutWorkOrderItemTravel([FromBody] WorkOrderItemTravel updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemTravel))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.TravelPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemTravel
        /// </summary>
        /// <param name="WorkOrderItemTravelId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/travels/{WorkOrderItemTravelId}")]
        public async Task<IActionResult> DeleteWorkOrderItemTravel([FromRoute] long WorkOrderItemTravelId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemTravel))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.TravelDeleteAsync(WorkOrderItemTravelId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemTravel

     
/*
    ██╗   ██╗███╗   ██╗██╗████████╗
    ██║   ██║████╗  ██║██║╚══██╔══╝
    ██║   ██║██╔██╗ ██║██║   ██║   
    ██║   ██║██║╚██╗██║██║   ██║   
    ╚██████╔╝██║ ╚████║██║   ██║   
    ╚═════╝ ╚═╝  ╚═══╝╚═╝   ╚═╝  
*/

        #region WorkOrderItemUnit

        /// <summary>
        /// Create WorkOrderItemUnit
        /// </summary>       
        /// <param name="newObject">WorkOrderItemUnit level only no descendents</param>
        /// <param name="apiVersion"></param>
        /// <returns>WorkOrderItemUnit object (no descendents)</returns>
        [HttpPost("items/units")]
        public async Task<IActionResult> PostWorkOrderItemUnit([FromBody] WorkOrderItemUnit newObject, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasCreateRole(HttpContext.Items, AyaType.WorkOrderItemUnit))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderItemUnit o = await biz.UnitCreateAsync(newObject);
            if (o == null)
                return BadRequest(new ApiErrorResponse(biz.Errors));
            else
                return CreatedAtAction(nameof(WorkOrderController.GetWorkOrderItemUnit), new { WorkOrderItemUnitId = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
        }


        /// <summary>
        /// Get WorkOrderItemUnit object
        /// </summary>
        /// <param name="WorkOrderItemUnitId"></param>
        /// <returns>A single WorkOrderItemUnit</returns>
        [HttpGet("items/units/{WorkOrderItemUnitId}")]
        public async Task<IActionResult> GetWorkOrderItemUnit([FromRoute] long WorkOrderItemUnitId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.WorkOrderItemUnit))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            var o = await biz.UnitGetAsync(WorkOrderItemUnitId);
            if (o == null)
                return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
            return Ok(ApiOkResponse.Response(o));
        }

        /// <summary>
        /// Put (update) WorkOrderItemUnit        
        /// (Descendent objects must be updated via their individual routes seperately)
        /// </summary>       
        /// <param name="updatedObject">WorkOrderItemUnit - top level only, no descendents</param>
        /// <returns>New concurrency token</returns>
        [HttpPut("items/units")]
        public async Task<IActionResult> PutWorkOrderItemUnit([FromBody] WorkOrderItemUnit updatedObject)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.WorkOrderItemUnit))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            var o = await biz.UnitPutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token 
            if (o == null)
            {
                if (biz.Errors.Exists(z => z.Code == ApiErrorCode.CONCURRENCY_CONFLICT))
                    return StatusCode(409, new ApiErrorResponse(biz.Errors));
                else
                    return BadRequest(new ApiErrorResponse(biz.Errors));
            }
            return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency }));
        }

        /// <summary>
        /// Delete WorkOrderItemUnit
        /// </summary>
        /// <param name="WorkOrderItemUnitId"></param>
        /// <returns>NoContent</returns>
        [HttpDelete("items/units/{WorkOrderItemUnitId}")]
        public async Task<IActionResult> DeleteWorkOrderItemUnit([FromRoute] long WorkOrderItemUnitId)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));
            WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
            if (!Authorized.HasDeleteRole(HttpContext.Items, AyaType.WorkOrderItemUnit))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!await biz.UnitDeleteAsync(WorkOrderItemUnitId))
                return BadRequest(new ApiErrorResponse(biz.Errors));
            return NoContent();
        }

        #endregion WorkOrderItemUnit




        //------------


    }//eoc
}//eons