diff --git a/MBDEVproAPI.API/Controllers/CustomerController.cs b/MBDEVproAPI.API/Controllers/CustomerController.cs index 7792e26..5f56de9 100644 --- a/MBDEVproAPI.API/Controllers/CustomerController.cs +++ b/MBDEVproAPI.API/Controllers/CustomerController.cs @@ -106,7 +106,7 @@ public async Task> GetCustomerAsync(int CustomerID) - #region Add Customer + #region Create Customer /// /// Add Customer | CustomerViewModel | Create a new customer for a business from client web application using a CustomerViewModel. /// @@ -115,19 +115,22 @@ public async Task> GetCustomerAsync(int CustomerID) [HttpPost] public async Task CreateCustomerVMAsync([FromBody] CustomerViewModel vm) { - return Ok(_customerService.CreateCustomerVMAsync(vm)); + var result = await _customerService.CreateCustomerVMAsync(vm); + return Ok(result); } /// /// Add Customer | CustomerModel | Create a new customer for a business. + /// TEST URL: https://localhost:7092/api/Customer/CreateCustomer /// /// /// [HttpPost] public async Task CreateCustomerAsync([FromBody] CustomerModel model) { - return Ok(_customerService.CreateCustomerAsync(model)); + var result = await _customerService.CreateCustomerAsync(model); + return Ok(result); //return Ok("UNDER CONTRUCTION | CreateCustomerAsync([FromBody] Customer model)"); } #endregion @@ -142,27 +145,30 @@ public async Task CreateCustomerAsync([FromBody] CustomerModel mo /// "CustomerControllerEditCustomerVMAsync": "Customer/EditCustomerVMAsync", /// /// - /// CustomerViewModel + /// SaveViewModel [HttpPost] public async Task EditCustomerVMAsync([FromBody] CustomerViewModel vm) { // if model is valid, then update, else return bad request with model state errors. - return Ok(_customerService.EditCustomerVMAsync(vm)); + var result = await _customerService.EditCustomerVMAsync(vm); + return Ok(result); //return Ok("UNDER CONTRUCTION | EditCustomer(int id, [FromBody] Customer model)"); } /// /// EDIT: Customer | CustomerModel | edit a customer for a business. + /// TEST URL: https://localhost:7092/api/Customer/EditCustomer?id=5 /// "CustomerControllerEditCustomer": "Customer/EditCustomer" /// /// /// [HttpPost] - public async Task EditCustomer(int id, [FromBody] CustomerModel model) + public async Task EditCustomer([FromBody] CustomerModel model) { // if model is valid, then update, else return bad request with model state errors. - return Ok(_customerService.EditCustomer(model)); + var result = await _customerService.EditCustomer(model); + return Ok(result); } // CustomerModel to just return a CustomerModel instead of a SaveViewModel with the RefID. We could do this for the VM as well. @@ -172,56 +178,19 @@ public async Task EditCustomer(int id, [FromBody] CustomerModel m #region Delete Customer /// - /// DELET: Customer | CustomerModel | Delete a customer for a business. - /// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer" + /// DELETE: Customer | SaveViewModel | delete a customer for a business and return a SaveViewModel with the RefID of the deleted customer. + /// "CustomerControllerDeleteCustomerVMAsync": "Customer/DeleteCustomerVMAsync" /// /// - /// - [HttpDelete("{id:int}")] - public async Task DeleteCustomer(int id) + /// SaveViewModel + [HttpDelete("{CustomerID:int}")] + public async Task DeleteCustomerVMAsync(int CustomerID) { - return Ok(_customerService.DeleteCustomer(id)); - } + var result = await _customerService.DeleteCustomerVMAsync(CustomerID); + return result; + } #endregion - - - //#region DELETE: Citation Type - ///// - ///// DELETE: Citation Type - ///// "CitationTypeControllerDeleteCitationType": "CitationType/DeleteCitationType" - ///// - ///// - ///// - //[HttpDelete("{CitationTypeID:int}")] - //public IActionResult DeleteCitationType(int CitationTypeID) - //{ - // return Ok(_citationTypeService.DeleteCitationType(CitationTypeID)); - //} - //#endregion - - //// DELETE: api/Customer/5 - //[HttpDelete("{id}")] - //public async Task DeleteCustomer(int id) - //{ - // var customer = await _context.Customers.FindAsync(id); - // if (customer == null) - // { - // return NotFound(); - // } - - // _context.Customers.Remove(customer); - // await _context.SaveChangesAsync(); - - // return NoContent(); - //} - - //private bool CustomerExists(int id) - //{ - // return _context.Customers.Any(e => e.CustomerID == id); - //} - } - } diff --git a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs index 470b8d2..f8a90a1 100644 --- a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs +++ b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs @@ -43,8 +43,8 @@ public interface ICustomerService : IBaseService #region Delete Customer - Task DeleteCustomerVM(int CustomerID); - SaveViewModel DeleteCustomer(int CustomerID); + Task DeleteCustomerVMAsync(int CustomerID); + Task DeleteCustomer(int CustomerID); #endregion } } diff --git a/MBDEVproAPI.BLL/Services/CustomerService.cs b/MBDEVproAPI.BLL/Services/CustomerService.cs index 40d1692..74028ca 100644 --- a/MBDEVproAPI.BLL/Services/CustomerService.cs +++ b/MBDEVproAPI.BLL/Services/CustomerService.cs @@ -289,7 +289,7 @@ public async Task EditCustomerVMAsync(CustomerViewModel vm) if (entity == null) { Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (entity == null)"); - return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." }; + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer. (entity == null)" }; } else { @@ -328,7 +328,7 @@ public async Task EditCustomer(CustomerModel model) if (model == null || model.CustomerID == 0 || model.BusinessID == 0) { Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (model == null || model.CustomerID == 0 || model.BusinessID == 0)"); - return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." }; + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer. Null Values" }; } else { @@ -341,11 +341,12 @@ public async Task EditCustomer(CustomerModel model) else { int? refID; + refID = entity.CustomerID; using (TransactionScope scope = new TransactionScope()) { Mapper.MapObject(model, entity); _customerRepository.SaveChanges(); - refID = entity.CustomerID; + //refID = entity.CustomerID; scope.Complete(); } return new SaveViewModel(refID); @@ -374,14 +375,14 @@ public async Task EditCustomer(CustomerModel model) /// /// /// SaveViewModel - public async Task DeleteCustomerVM(int CustomerID) + public async Task DeleteCustomerVMAsync(int CustomerID) { try { int? refID = null; if (CustomerID == 0) { - Log.Error("Customer API: CustomerService(DeleteCustomerVM); (CustomerID == 0)"); + Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (CustomerID == 0)"); return new SaveViewModel("Please provide details to delete the Customer."); } else @@ -390,7 +391,7 @@ public async Task DeleteCustomerVM(int CustomerID) if (entity == null) { - Log.Error("Customer API: CustomerService(DeleteCustomerVM); (entity == null)"); + Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (entity == null)"); return new SaveViewModel("Please provide details to delete the Customer."); } else @@ -408,30 +409,47 @@ public async Task DeleteCustomerVM(int CustomerID) } catch (Exception ex) { - Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (" + ex + ")" + " (" + ex.InnerException + ")"); - return new SaveViewModel(ex.Message); + Log.Error("Customer API: CustomerService(DeleteCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel("Customer API: CustomerService(DeleteCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ") Message: " + ex.Message); } finally { } - return new SaveViewModel("Customer API: CustomerService(DeleteCustomer); (SaveViewModel) Not Yet Implemented."); } /// - /// DELETE: Customer | CustomerModel | delete a customer for a business and return a SaveViewModel with the RefID of the deleted customer. - /// "CustomerControllerDeleteCustomerVM": "Customer/DeleteCustomerVM" + /// DELETE: Customer | CustomerModel | delete a customer for a business. + /// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer" /// /// /// SaveViewModel - public CustomerModel DeleteCustomer(int CustomerID) - { - throw new NotImplementedException(); - // return new SaveViewModel("Customer API: CustomerService(DeleteCustomer); (SaveViewModel) Not Yet Implemented."); - } - - SaveViewModel ICustomerService.DeleteCustomer(int CustomerID) - { - throw new NotImplementedException(); + public async Task DeleteCustomer(int CustomerID) + { // More to do here + if (CustomerID == 0) + { + Log.Error("Customer API: CustomerService(DeleteCustomer); (CustomerID == 0)"); + return new CustomerModel(); + } + else { + var entity = await _customerRepository.GetCustomerAsync(CustomerID); + if (entity == null) + { + Log.Error("Customer API: CustomerService(DeleteCustomer); (entity == null)"); + return new CustomerModel(); + } + else + { + int? refID = null; + using (TransactionScope scope = new TransactionScope()) + { + _customerRepository.Remove(entity); + _customerRepository.SaveChanges(); + scope.Complete(); + refID = entity.CustomerID; + } + return Mapper.MapObject(entity, new CustomerModel()); + } + } } #endregion