diff --git a/server/AyaNova/Controllers/PMItemController.cs b/server/AyaNova/Controllers/PMItemController.cs deleted file mode 100644 index ec152910..00000000 --- a/server/AyaNova/Controllers/PMItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class PMItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public PMItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full PMItem object - /// - /// - /// A single PMItem - [HttpGet("{id}")] - public async Task GetPMItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMItemBiz biz = PMItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) PMItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutPMItem([FromRoute] long id, [FromBody] PMItem inObj) - { - 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 - PMItemBiz biz = PMItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) PMItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchPMItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - PMItemBiz biz = PMItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post PMItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostPMItem([FromBody] PMItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMItemBiz biz = PMItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - PMItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(PMItemController.GetPMItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate PMItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicatePMItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMItemBiz biz = PMItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - PMItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(PMItemController.GetPMItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete PMItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeletePMItem([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 - PMItemBiz biz = PMItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/PMTemplateItemController.cs b/server/AyaNova/Controllers/PMTemplateItemController.cs deleted file mode 100644 index c6a80e16..00000000 --- a/server/AyaNova/Controllers/PMTemplateItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class PMTemplateItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public PMTemplateItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full PMTemplateItem object - /// - /// - /// A single PMTemplateItem - [HttpGet("{id}")] - public async Task GetPMTemplateItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMTemplateItemBiz biz = PMTemplateItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) PMTemplateItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutPMTemplateItem([FromRoute] long id, [FromBody] PMTemplateItem inObj) - { - 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 - PMTemplateItemBiz biz = PMTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) PMTemplateItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchPMTemplateItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - PMTemplateItemBiz biz = PMTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post PMTemplateItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostPMTemplateItem([FromBody] PMTemplateItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMTemplateItemBiz biz = PMTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - PMTemplateItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(PMTemplateItemController.GetPMTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate PMTemplateItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicatePMTemplateItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - PMTemplateItemBiz biz = PMTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - PMTemplateItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(PMTemplateItemController.GetPMTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete PMTemplateItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeletePMTemplateItem([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 - PMTemplateItemBiz biz = PMTemplateItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/QuoteItemController.cs b/server/AyaNova/Controllers/QuoteItemController.cs deleted file mode 100644 index cd5afbe5..00000000 --- a/server/AyaNova/Controllers/QuoteItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class QuoteItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public QuoteItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full QuoteItem object - /// - /// - /// A single QuoteItem - [HttpGet("{id}")] - public async Task GetQuoteItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteItemBiz biz = QuoteItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) QuoteItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutQuoteItem([FromRoute] long id, [FromBody] QuoteItem inObj) - { - 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 - QuoteItemBiz biz = QuoteItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) QuoteItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchQuoteItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - QuoteItemBiz biz = QuoteItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post QuoteItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostQuoteItem([FromBody] QuoteItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteItemBiz biz = QuoteItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - QuoteItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(QuoteItemController.GetQuoteItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate QuoteItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicateQuoteItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteItemBiz biz = QuoteItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - QuoteItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(QuoteItemController.GetQuoteItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete QuoteItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeleteQuoteItem([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 - QuoteItemBiz biz = QuoteItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/QuoteTemplateItemController.cs b/server/AyaNova/Controllers/QuoteTemplateItemController.cs deleted file mode 100644 index a9166e95..00000000 --- a/server/AyaNova/Controllers/QuoteTemplateItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class QuoteTemplateItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public QuoteTemplateItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full QuoteTemplateItem object - /// - /// - /// A single QuoteTemplateItem - [HttpGet("{id}")] - public async Task GetQuoteTemplateItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) QuoteTemplateItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutQuoteTemplateItem([FromRoute] long id, [FromBody] QuoteTemplateItem inObj) - { - 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 - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) QuoteTemplateItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchQuoteTemplateItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post QuoteTemplateItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostQuoteTemplateItem([FromBody] QuoteTemplateItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - QuoteTemplateItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(QuoteTemplateItemController.GetQuoteTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate QuoteTemplateItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicateQuoteTemplateItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - QuoteTemplateItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(QuoteTemplateItemController.GetQuoteTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete QuoteTemplateItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeleteQuoteTemplateItem([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 - QuoteTemplateItemBiz biz = QuoteTemplateItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/WorkOrderItemController.cs b/server/AyaNova/Controllers/WorkOrderItemController.cs deleted file mode 100644 index 941ed338..00000000 --- a/server/AyaNova/Controllers/WorkOrderItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class WorkOrderItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public WorkOrderItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full WorkOrderItem object - /// - /// - /// A single WorkOrderItem - [HttpGet("{id}")] - public async Task GetWorkOrderItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderItemBiz biz = WorkOrderItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) WorkOrderItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutWorkOrderItem([FromRoute] long id, [FromBody] WorkOrderItem inObj) - { - 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 - WorkOrderItemBiz biz = WorkOrderItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) WorkOrderItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchWorkOrderItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - WorkOrderItemBiz biz = WorkOrderItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post WorkOrderItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostWorkOrderItem([FromBody] WorkOrderItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderItemBiz biz = WorkOrderItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - WorkOrderItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(WorkOrderItemController.GetWorkOrderItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate WorkOrderItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicateWorkOrderItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderItemBiz biz = WorkOrderItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - WorkOrderItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(WorkOrderItemController.GetWorkOrderItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete WorkOrderItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeleteWorkOrderItem([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 - WorkOrderItemBiz biz = WorkOrderItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/Controllers/WorkOrderTemplateItemController.cs b/server/AyaNova/Controllers/WorkOrderTemplateItemController.cs deleted file mode 100644 index a76f6cd1..00000000 --- a/server/AyaNova/Controllers/WorkOrderTemplateItemController.cs +++ /dev/null @@ -1,270 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.EntityFrameworkCore; -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}/[controller]")] - [Produces("application/json")] - [Authorize] - public class WorkOrderTemplateItemController : ControllerBase - { - private readonly AyContext ct; - private readonly ILogger log; - private readonly ApiServerState serverState; - - - /// - /// ctor - /// - /// - /// - /// - public WorkOrderTemplateItemController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) - { - ct = dbcontext; - log = logger; - serverState = apiServerState; - } - - - /// - /// Get full WorkOrderTemplateItem object - /// - /// - /// A single WorkOrderTemplateItem - [HttpGet("{id}")] - public async Task GetWorkOrderTemplateItem([FromRoute] long id) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.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)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - var o = await biz.GetAsync(id); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - // 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 - - return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); - } - - - - /// - /// Put (update) WorkOrderTemplateItem - /// - /// - /// - /// - [HttpPut("{id}")] - public async Task PutWorkOrderTemplateItem([FromRoute] long id, [FromBody] WorkOrderTemplateItem inObj) - { - 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 - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - if (!await biz.PutAsync(o, inObj)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - - /// - /// Patch (update) WorkOrderTemplateItem - /// - /// - /// - /// - /// - [HttpPatch("{id}/{concurrencyToken}")] - public async Task PatchWorkOrderTemplateItem([FromRoute] long id, [FromRoute] uint concurrencyToken, [FromBody]JsonPatchDocument objectPatch) - { - //https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ - - 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 - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.GetBiz(ct, HttpContext); - - var o = await biz.GetAsync(id, false); - if (o == null) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - - if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - try - { - //patch and validate - if (!await biz.PatchAsync(o, objectPatch, concurrencyToken)) - return BadRequest(new ApiErrorResponse(biz.Errors)); - } - catch (DbUpdateConcurrencyException) - { - if (!await biz.ExistsAsync(id)) - return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - else - return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); - } - return Ok(ApiOkResponse.Response(new { ConcurrencyToken = o.ConcurrencyToken }, true)); - } - - - /// - /// Post WorkOrderTemplateItem - /// - /// - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost] - public async Task PostWorkOrderTemplateItem([FromBody] WorkOrderTemplateItem inObj, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - return BadRequest(new ApiErrorResponse(ModelState)); - - //Create and validate - WorkOrderTemplateItem o = await biz.CreateAsync(inObj); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(WorkOrderTemplateItemController.GetWorkOrderTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - - } - - /// - /// Duplicate WorkOrderTemplateItem - /// - /// Create a duplicate of this items id - /// Automatically filled from route path, no need to specify in body - /// - [HttpPost("duplicate/{id}")] - public async Task DuplicateWorkOrderTemplateItem([FromRoute] long id, ApiVersion apiVersion) - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - //Instantiate the business object handler - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.GetBiz(ct, HttpContext); - - //If a user has change roles - if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - if (!ModelState.IsValid) - 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 - WorkOrderTemplateItem o = await biz.DuplicateAsync(oSrc); - if (o == null) - return BadRequest(new ApiErrorResponse(biz.Errors)); - else - return CreatedAtAction(nameof(WorkOrderTemplateItemController.GetWorkOrderTemplateItem), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o)); - - } - - - - /// - /// Delete WorkOrderTemplateItem - /// - /// - /// Ok - [HttpDelete("{id}")] - public async Task DeleteWorkOrderTemplateItem([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 - WorkOrderTemplateItemBiz biz = WorkOrderTemplateItemBiz.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(); - } - - - - - - //------------ - - - }//eoc -}//eons \ No newline at end of file diff --git a/server/AyaNova/biz/PMItemBiz.cs b/server/AyaNova/biz/PMItemBiz.cs deleted file mode 100644 index 5d669a5f..00000000 --- a/server/AyaNova/biz/PMItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class PMItemBiz : BizObject, ISearchAbleObject - { - - internal PMItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.PMItem; - } - - internal static PMItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new PMItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new PMItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.PMItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.PMItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(PMItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with PMItem - PMItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.PMItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(PMItem dbObj) - { - - PMItem outObj = new PMItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.PMItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.PMItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(PMItem dbObj, PMItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - PMItem SnapshotOfOriginalDBObj = new PMItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(PMItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - PMItem SnapshotOfOriginalDBObj = new PMItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(PMItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.PMItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(PMItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.PMItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(PMItem proposedObj, PMItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.PMItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.PMItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(PMItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/biz/PMTemplateItemBiz.cs b/server/AyaNova/biz/PMTemplateItemBiz.cs deleted file mode 100644 index 3207061b..00000000 --- a/server/AyaNova/biz/PMTemplateItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class PMTemplateItemBiz : BizObject, ISearchAbleObject - { - - internal PMTemplateItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.PMTemplateItem; - } - - internal static PMTemplateItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new PMTemplateItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new PMTemplateItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.PMTemplateItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.PMTemplateItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(PMTemplateItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with PMTemplateItem - PMTemplateItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.PMTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(PMTemplateItem dbObj) - { - - PMTemplateItem outObj = new PMTemplateItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.PMTemplateItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.PMTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(PMTemplateItem dbObj, PMTemplateItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - PMTemplateItem SnapshotOfOriginalDBObj = new PMTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(PMTemplateItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - PMTemplateItem SnapshotOfOriginalDBObj = new PMTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(PMTemplateItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.PMTemplateItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(PMTemplateItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.PMTemplateItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(PMTemplateItem proposedObj, PMTemplateItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.PMTemplateItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.PMTemplateItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(PMTemplateItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/biz/QuoteItemBiz.cs b/server/AyaNova/biz/QuoteItemBiz.cs deleted file mode 100644 index 2bcf00b3..00000000 --- a/server/AyaNova/biz/QuoteItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class QuoteItemBiz : BizObject, ISearchAbleObject - { - - internal QuoteItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.QuoteItem; - } - - internal static QuoteItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new QuoteItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new QuoteItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.QuoteItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.QuoteItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(QuoteItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with QuoteItem - QuoteItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.QuoteItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(QuoteItem dbObj) - { - - QuoteItem outObj = new QuoteItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.QuoteItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.QuoteItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(QuoteItem dbObj, QuoteItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - QuoteItem SnapshotOfOriginalDBObj = new QuoteItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(QuoteItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - QuoteItem SnapshotOfOriginalDBObj = new QuoteItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(QuoteItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.QuoteItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(QuoteItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.QuoteItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(QuoteItem proposedObj, QuoteItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.QuoteItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.QuoteItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(QuoteItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/biz/QuoteTemplateItemBiz.cs b/server/AyaNova/biz/QuoteTemplateItemBiz.cs deleted file mode 100644 index 8049fda8..00000000 --- a/server/AyaNova/biz/QuoteTemplateItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class QuoteTemplateItemBiz : BizObject, ISearchAbleObject - { - - internal QuoteTemplateItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.QuoteTemplateItem; - } - - internal static QuoteTemplateItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new QuoteTemplateItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new QuoteTemplateItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.QuoteTemplateItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.QuoteTemplateItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(QuoteTemplateItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with QuoteTemplateItem - QuoteTemplateItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.QuoteTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(QuoteTemplateItem dbObj) - { - - QuoteTemplateItem outObj = new QuoteTemplateItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.QuoteTemplateItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.QuoteTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(QuoteTemplateItem dbObj, QuoteTemplateItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - QuoteTemplateItem SnapshotOfOriginalDBObj = new QuoteTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(QuoteTemplateItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - QuoteTemplateItem SnapshotOfOriginalDBObj = new QuoteTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(QuoteTemplateItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.QuoteTemplateItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(QuoteTemplateItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.QuoteTemplateItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(QuoteTemplateItem proposedObj, QuoteTemplateItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.QuoteTemplateItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.QuoteTemplateItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(QuoteTemplateItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/biz/WorkOrderItemBiz.cs b/server/AyaNova/biz/WorkOrderItemBiz.cs deleted file mode 100644 index 87223c28..00000000 --- a/server/AyaNova/biz/WorkOrderItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class WorkOrderItemBiz : BizObject, ISearchAbleObject - { - - internal WorkOrderItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.WorkOrderItem; - } - - internal static WorkOrderItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new WorkOrderItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new WorkOrderItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.WorkOrderItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.WorkOrderItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(WorkOrderItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with WorkOrderItem - WorkOrderItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.WorkOrderItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(WorkOrderItem dbObj) - { - - WorkOrderItem outObj = new WorkOrderItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.WorkOrderItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.WorkOrderItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(WorkOrderItem dbObj, WorkOrderItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - WorkOrderItem SnapshotOfOriginalDBObj = new WorkOrderItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(WorkOrderItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - WorkOrderItem SnapshotOfOriginalDBObj = new WorkOrderItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(WorkOrderItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.WorkOrderItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(WorkOrderItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.WorkOrderItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(WorkOrderItem proposedObj, WorkOrderItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.WorkOrderItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.WorkOrderItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(WorkOrderItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons - diff --git a/server/AyaNova/biz/WorkOrderTemplateItemBiz.cs b/server/AyaNova/biz/WorkOrderTemplateItemBiz.cs deleted file mode 100644 index ecf19337..00000000 --- a/server/AyaNova/biz/WorkOrderTemplateItemBiz.cs +++ /dev/null @@ -1,305 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.JsonPatch; -using AyaNova.Util; -using AyaNova.Api.ControllerHelpers; -using AyaNova.Models; - -namespace AyaNova.Biz -{ - - internal class WorkOrderTemplateItemBiz : BizObject, ISearchAbleObject - { - - internal WorkOrderTemplateItemBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) - { - ct = dbcontext; - UserId = currentUserId; - UserTranslationId = userTranslationId; - CurrentUserRoles = UserRoles; - BizType = AyaType.WorkOrderTemplateItem; - } - - internal static WorkOrderTemplateItemBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) - { - - if (httpContext != null) - return new WorkOrderTemplateItemBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); - else//when called internally for internal ops there will be no context so need to set default values for that - return new WorkOrderTemplateItemBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //EXISTS - internal async Task ExistsAsync(long id) - { - return await ct.WorkOrderTemplateItem.AnyAsync(e => e.Id == id); - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - /// - /// - - internal async Task GetAsync(long fetchId, bool logTheGetEvent = true) - { - //This is simple so nothing more here, but often will be copying to a different output object or some other ops - var ret = await ct.WorkOrderTemplateItem.SingleOrDefaultAsync(m => m.Id == fetchId); - if (logTheGetEvent && ret != null) - { - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, fetchId, BizType, AyaEvent.Retrieved), ct); - } - return ret; - } - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //CREATE - - //Called from route and also seeder - internal async Task CreateAsync(WorkOrderTemplateItem inObj) - { - await ValidateAsync(inObj, null); - if (HasErrors) - return null; - else - { - //do stuff with WorkOrderTemplateItem - WorkOrderTemplateItem outObj = inObj; - - outObj.Tags = TagUtil.NormalizeTags(outObj.Tags); - outObj.CustomFields = JsonUtil.CompactJson(outObj.CustomFields); - //Save to db - await ct.WorkOrderTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - - return outObj; - } - } - - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DUPLICATE - // - - internal async Task DuplicateAsync(WorkOrderTemplateItem dbObj) - { - - WorkOrderTemplateItem outObj = new WorkOrderTemplateItem(); - CopyObject.Copy(dbObj, outObj, "Wiki"); - // outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255); - //generate unique name - string newUniqueName = string.Empty; - bool NotUnique = true; - long l = 1; - do - { - newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObj.Name, l++, 255); - NotUnique = await ct.WorkOrderTemplateItem.AnyAsync(m => m.Name == newUniqueName); - } while (NotUnique); - - outObj.Name = newUniqueName; - - - outObj.Id = 0; - outObj.ConcurrencyToken = 0; - - await ct.WorkOrderTemplateItem.AddAsync(outObj); - await ct.SaveChangesAsync(); - - //Handle child and associated items: - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct); - await SearchIndexAsync(outObj, true); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, outObj.Tags, null); - return outObj; - - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //UPDATE - // - - //put - internal async Task PutAsync(WorkOrderTemplateItem dbObj, WorkOrderTemplateItem inObj) - { - - //make a snapshot of the original for validation but update the original to preserve workflow - WorkOrderTemplateItem SnapshotOfOriginalDBObj = new WorkOrderTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Replace the db object with the PUT object - CopyObject.Copy(inObj, dbObj, "Id"); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - //Set "original" value of concurrency token to input token - //this will allow EF to check it out - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = inObj.ConcurrencyToken; - - - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - //patch - internal async Task PatchAsync(WorkOrderTemplateItem dbObj, JsonPatchDocument objectPatch, uint concurrencyToken) - { - //Validate Patch is allowed - if (!ValidateJsonPatch.Validate(this, objectPatch)) return false; - - //make a snapshot of the original for validation but update the original to preserve workflow - WorkOrderTemplateItem SnapshotOfOriginalDBObj = new WorkOrderTemplateItem(); - CopyObject.Copy(dbObj, SnapshotOfOriginalDBObj); - - //Do the patching - objectPatch.ApplyTo(dbObj); - - dbObj.Tags = TagUtil.NormalizeTags(dbObj.Tags); - dbObj.CustomFields = JsonUtil.CompactJson(dbObj.CustomFields); - - ct.Entry(dbObj).OriginalValues["ConcurrencyToken"] = concurrencyToken; - await ValidateAsync(dbObj, SnapshotOfOriginalDBObj); - if (HasErrors) - return false; - - //Log event and save context - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, BizType, AyaEvent.Modified), ct); - await SearchIndexAsync(dbObj, false); - - await TagUtil.ProcessUpdateTagsInRepositoryAsync(ct, dbObj.Tags, SnapshotOfOriginalDBObj.Tags); - - return true; - } - - - private async Task SearchIndexAsync(WorkOrderTemplateItem obj, bool isNew) - { - //SEARCH INDEXING - var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType); - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - - if (isNew) - await Search.ProcessNewObjectKeywordsAsync(SearchParams); - else - await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams); - } - - public async Task GetSearchResultSummary(long id) - { - var obj = await ct.WorkOrderTemplateItem.SingleOrDefaultAsync(m => m.Id == id); - var SearchParams = new Search.SearchIndexProcessObjectParameters(); - if (obj != null) - SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields); - return SearchParams; - } - - //////////////////////////////////////////////////////////////////////////////////////////////// - //DELETE - // - internal async Task DeleteAsync(WorkOrderTemplateItem dbObj) - { - //Determine if the object can be deleted, do the deletion tentatively - //Probably also in here deal with tags and associated search text etc - - //NOT REQUIRED NOW BUT IF IN FUTURE ValidateCanDelete(dbObj); - if (HasErrors) - return false; - ct.WorkOrderTemplateItem.Remove(dbObj); - await ct.SaveChangesAsync(); - - //Log event - await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObj.Id, dbObj.Name, ct); - await Search.ProcessDeletedObjectKeywordsAsync(dbObj.Id, BizType); - await TagUtil.ProcessDeleteTagsInRepositoryAsync(ct, dbObj.Tags); - return true; - } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //VALIDATION - // - - //Can save or update? - private async Task ValidateAsync(WorkOrderTemplateItem proposedObj, WorkOrderTemplateItem currentObj) - { - - //run validation and biz rules - bool isNew = currentObj == null; - - //Name required - if (string.IsNullOrWhiteSpace(proposedObj.Name)) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); - - //Name must be less than 255 characters - if (proposedObj.Name.Length > 255) - AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max"); - - //If name is otherwise OK, check that name is unique - if (!PropertyHasErrors("Name")) - { - //Use Any command is efficient way to check existance, it doesn't return the record, just a true or false - if (await ct.WorkOrderTemplateItem.AnyAsync(m => m.Name == proposedObj.Name && m.Id != proposedObj.Id)) - { - AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name"); - } - } - - - //Any form customizations to validate? - var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.WorkOrderTemplateItem.ToString()); - if (FormCustomization != null) - { - //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required - - //validate users choices for required non custom fields - RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj); - - //validate custom fields - CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields); - } - - } - - - //Can delete? - // private void ValidateCanDelete(WorkOrderTemplateItem inObj) - // { - // //whatever needs to be check to delete this object - // } - - - - //////////////////////////////////////////////////////////////////////////////////////////////// - //JOB / OPERATIONS - // - - - //Other job handlers here... - - - ///////////////////////////////////////////////////////////////////// - - }//eoc - - -}//eons -