From e230d1eb9ab41a7dfed396975294dfa17f670d30 Mon Sep 17 00:00:00 2001 From: Mike Belcher Date: Sun, 1 Mar 2026 17:58:48 -0500 Subject: [PATCH] More updates --- .../Controllers/CustomerController.cs | 119 ++++---- MBDEVproAPI.API/Program.cs | 2 +- MBDEVproAPI.BLL/GlobalUsings.cs | 1 + .../Interfaces/ICustomerService.cs | 26 +- MBDEVproAPI.BLL/Services/CustomerService.cs | 257 +++++++++++++----- .../Interfaces/IBaseRepository.cs | 2 +- .../Interfaces/ICustomerRepository.cs | 29 +- .../Repositories/CustomerRepository.cs | 84 +++--- 8 files changed, 348 insertions(+), 172 deletions(-) diff --git a/MBDEVproAPI.API/Controllers/CustomerController.cs b/MBDEVproAPI.API/Controllers/CustomerController.cs index be036be..7792e26 100644 --- a/MBDEVproAPI.API/Controllers/CustomerController.cs +++ b/MBDEVproAPI.API/Controllers/CustomerController.cs @@ -1,6 +1,4 @@ - - -namespace MBDEVproAPI.API.Controllers +namespace MBDEVproAPI.API.Controllers { public class CustomerController : BaseController { @@ -20,9 +18,9 @@ public CustomerController(ICustomerService customerService) #endregion - #region Get All Customers | CustomerViewModel + #region Get All Customers /// - /// GET: Gets all customers for a business in a VM for web UI. + /// GET: Get All Customers | CustomerViewModel | Gets all customers for a business in a VM for web UI. /// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers/52466 | https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466 /// "CustomerControllerGetAllCustomersVMAsync": "Customer/GetAllCustomersVMAsync", /// @@ -50,12 +48,10 @@ public async Task> GetAllCustomersVMAsync(int Bu return BadRequest("Customer API error: " + ex.Message + " | " + ex.InnerException); } } - #endregion - #region Get All Customers | CustomerModel /// - /// GET: Gets all customers for a business. + /// GET: Get All Customers | CustomerModel | Gets all customers for a business. /// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466 /// "CustomerControllerGetAllCustomersAsync": "Customer/GetAllCustomersAsync", /// @@ -83,9 +79,9 @@ public async Task> GetAllCustomersAsync(int Business - #region Get Customer | Customer + #region Get Customer /// - /// GET: Gets a customer for a business. + /// GET: Get Customer | Customer | Gets a customer for a business. /// TEST URL: https://localhost:7092/api/Customer/GetCustomer/3 | https://localhost:7092/api/Customer/GetCustomer?CustomerID=3 /// "CustomerControllerGetCustomer": "Customer/GetCustomer", /// @@ -109,9 +105,10 @@ public async Task> GetCustomerAsync(int CustomerID) - #region Add Customer | CustomerViewModel + + #region Add Customer /// - /// Create a new customer for a business from client web application using a CustomerViewModel. + /// Add Customer | CustomerViewModel | Create a new customer for a business from client web application using a CustomerViewModel. /// /// /// @@ -120,13 +117,10 @@ public async Task CreateCustomerVMAsync([FromBody] CustomerViewMo { return Ok(_customerService.CreateCustomerVMAsync(vm)); } - #endregion - - #region Add Customer | Customer /// - /// Create a new customer for a business. + /// Add Customer | CustomerModel | Create a new customer for a business. /// /// /// @@ -141,50 +135,69 @@ public async Task CreateCustomerAsync([FromBody] CustomerModel mo - //// PUT - CREATE: api/Customer/5 - //// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 - //[HttpPut("{id}")] + #region Edit Customer + /// + /// EDIT: Edit a Customer | CustomerViewModel | edit a customer for a business in a VM for web UI. + /// TEST URL: | + /// "CustomerControllerEditCustomerVMAsync": "Customer/EditCustomerVMAsync", + /// + /// + /// CustomerViewModel + [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)); + //return Ok("UNDER CONTRUCTION | EditCustomer(int id, [FromBody] Customer model)"); + } - //[HttpPost] - //public async Task PutCustomer(int id, Customer customer) - //{ - // if (id != customer.CustomerID) - // { - // return BadRequest(); - // } - // _context.Entry(customer).State = EntityState.Modified; + /// + /// EDIT: Customer | CustomerModel | edit a customer for a business. + /// "CustomerControllerEditCustomer": "Customer/EditCustomer" + /// + /// + /// + [HttpPost] + public async Task EditCustomer(int id, [FromBody] CustomerModel model) + { + // if model is valid, then update, else return bad request with model state errors. + return Ok(_customerService.EditCustomer(model)); + } - // try - // { - // await _context.SaveChangesAsync(); - // } - // catch (DbUpdateConcurrencyException) - // { - // if (!CustomerExists(id)) - // { - // return NotFound(); - // } - // else - // { - // throw; - // } - // } + // CustomerModel to just return a CustomerModel instead of a SaveViewModel with the RefID. We could do this for the VM as well. + #endregion - // return NoContent(); - //} - //// POST: api/Customer - //// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 - //[HttpPost] - //public async Task> PostCustomer(Customer customer) - //{ - // _context.Customers.Add(customer); - // await _context.SaveChangesAsync(); - // return CreatedAtAction("GetCustomer", new { id = customer.CustomerID }, customer); - //} + #region Delete Customer + /// + /// DELET: Customer | CustomerModel | Delete a customer for a business. + /// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer" + /// + /// + /// + [HttpDelete("{id:int}")] + public async Task DeleteCustomer(int id) + { + return Ok(_customerService.DeleteCustomer(id)); + } + #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}")] diff --git a/MBDEVproAPI.API/Program.cs b/MBDEVproAPI.API/Program.cs index 605712f..ea158c6 100644 --- a/MBDEVproAPI.API/Program.cs +++ b/MBDEVproAPI.API/Program.cs @@ -41,7 +41,7 @@ }); }); - +//ADD: API security and authentication (OAuth2, OpenID Connect, JWT) var app = builder.Build(); diff --git a/MBDEVproAPI.BLL/GlobalUsings.cs b/MBDEVproAPI.BLL/GlobalUsings.cs index 34e3816..b00585a 100644 --- a/MBDEVproAPI.BLL/GlobalUsings.cs +++ b/MBDEVproAPI.BLL/GlobalUsings.cs @@ -20,3 +20,4 @@ global using MBDEVproAPI.DataModel; global using MBDEVproAPI.DataModel.Entities; global using MBDEVproAPI.Repository.Interfaces; +global using MBDEVproAPI.Repository.Repositories; diff --git a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs index b64b4a4..470b8d2 100644 --- a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs +++ b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs @@ -8,23 +8,43 @@ namespace MBDEVproAPI.BLL.Interfaces public interface ICustomerService : IBaseService { + + #region + #endregion + + + + #region Get All Customers Task GetAllCustomersVMAsync(int BusinessID); Task> GetAllCustomersAsync(int BusinessID); + #endregion + #region Get Customer Task GetCustomerAsync(int CustomerID); + #endregion + + #region Add Customer Task CreateCustomerVMAsync(CustomerViewModel vm); Task CreateCustomerAsync(CustomerModel model); + + SaveViewModel CreateCustomer(CustomerModel model); + #endregion - SaveViewModel CreateCustomer(CustomerModel model); + #region Edit Customer + Task EditCustomerVMAsync(CustomerViewModel vm); - SaveViewModel EditCustomer(int id, CustomerModel model); + Task EditCustomer(CustomerModel model); + #endregion - SaveViewModel DeleteCustomer(int id); + #region Delete Customer + Task DeleteCustomerVM(int CustomerID); + SaveViewModel DeleteCustomer(int CustomerID); + #endregion } } diff --git a/MBDEVproAPI.BLL/Services/CustomerService.cs b/MBDEVproAPI.BLL/Services/CustomerService.cs index 93c4c29..40d1692 100644 --- a/MBDEVproAPI.BLL/Services/CustomerService.cs +++ b/MBDEVproAPI.BLL/Services/CustomerService.cs @@ -35,9 +35,17 @@ public CustomerService(MBDEVproAPIDbContext context, ICustomerRepository custome #endregion - #region Get All Customers | CustomerViewModel + + #region + #endregion + + + + #region Get All Customers /// - /// GET: Gets all customers for a business in a VM for web UI. + /// GET: Get All Customers | CustomerViewModel | Gets all customers for a business in a VM for web UI. + /// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers/52466 | https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466 + /// "CustomerControllerGetAllCustomersVMAsync": "Customer/GetAllCustomersVMAsync", /// /// /// @@ -69,10 +77,14 @@ public async Task GetAllCustomersVMAsync(int BusinessID) return new CustomerViewModel(); } } - #endregion - - #region Get All Customers | CustomerModel + /// + /// GET: Get All Customers | CustomerModel | Gets all customers for a business. + /// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466 + /// "CustomerControllerGetAllCustomersAsync": "Customer/GetAllCustomersAsync", + /// + /// + /// public async Task> GetAllCustomersAsync(int BusinessID) { try @@ -104,35 +116,11 @@ public async Task> GetAllCustomersAsync(int BusinessI #endregion - //public IEnumerable GetAll() - //{ - // try - // { - // var entities = _projectRepository.GetAll().Select(O => Mapper.MapObject(O, new ProjectModel())).ToList(); - // if (entities == null) - // { - // throw new Exception("Licensing API: ProjectService(GetAll); (entities == null"); - // } - // else - // { - // return entities; - // } - // } - // catch (Exception ex) - // { - // ex.Data.Add("ErrorMessage", "Licensing API: ProjectService(GetAll)"); - // throw; - // } - //} - - - - - - #region Get Customer | Customer - //GetCustomerAsync + #region Get Customer /// - /// GET: Gets a customer for a business. + /// GET: Get Customer | Customer | Gets a customer for a business. + /// TEST URL: https://localhost:7092/api/Customer/GetCustomer/3 | https://localhost:7092/api/Customer/GetCustomer?CustomerID=3 + /// "CustomerControllerGetCustomer": "Customer/GetCustomer", /// /// /// @@ -167,12 +155,9 @@ public async Task> GetAllCustomersAsync(int BusinessI - - - - #region Add Customer | CustomerViewModel + #region Add Customer /// - /// Create a new customer for a business from client web application using a CustomerViewModel. + /// Add Customer | CustomerViewModel | Create a new customer for a business from client web application using a CustomerViewModel. /// /// /// @@ -218,13 +203,9 @@ public async Task CreateCustomerVMAsync(CustomerViewModel vm) { } } - #endregion - - #region Add Customer | Customer /// - /// CREATE: Customer - /// "CustomerControllerCreateCustomerAsync": "Customer/CreateCustomerAsync" + /// Add Customer | CustomerModel | Create a new customer for a business. /// /// /// @@ -239,7 +220,7 @@ public async Task CreateCustomerAsync(CustomerModel model) } else { - var entity = new DataModel.Entities.Customer(); + var entity = new DataModel.Entities.Customer(); if (entity == null) { Log.Error("Customer API: CustomerService(CreateCustomerAsync); (entity == null)"); @@ -269,23 +250,7 @@ public async Task CreateCustomerAsync(CustomerModel model) { } - - } - #endregion - - - - ///// - ///// GET: Customer - ///// "CustomerControllerGetCustomer": "Customer/GetCustomer" - ///// - ///// - ///// - //public CustomerModel GetCustomer(int id) - //{ - // throw new NotImplementedException(); - //} /// /// CREATE: Customer @@ -297,31 +262,189 @@ public SaveViewModel CreateCustomer(CustomerModel model) { throw new NotImplementedException(); } + #endregion + + + #region Edit Customer /// - /// EDIT: Customer + /// EDIT: Edit a Customer | CustomerViewModel | edit a customer for a business in a VM for web UI. + /// TEST URL: | + /// "CustomerControllerEditCustomerVMAsync": "Customer/EditCustomerVMAsync", + /// + /// + /// SaveViewModel + public async Task EditCustomerVMAsync(CustomerViewModel vm) + { + try + { + if (vm == null || vm.CustomerID == 0 || vm.BusinessID == 0) + { + Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (vm == null || vm.CustomerID == 0 || vm.BusinessID == 0)"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." }; + } + else + { + var entity = await _customerRepository.GetCustomerAsync(vm.CustomerID); + if (entity == null) + { + Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (entity == null)"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." }; + } + else + { + int? refID; + using (TransactionScope scope = new TransactionScope()) + { + Mapper.MapObject(vm, entity); + _customerRepository.SaveChanges(); + refID = entity.CustomerID; + scope.Complete(); + } + return new SaveViewModel(refID); + } + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(EditCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel(ex.Message); + } + finally + { + } + } + + /// + /// EDIT: Customer | CustomerModel | edit a customer for a business. /// "CustomerControllerEditCustomer": "Customer/EditCustomer" /// /// /// - public SaveViewModel EditCustomer(int id, CustomerModel model) + public async Task EditCustomer(CustomerModel model) { - throw new NotImplementedException(); + try + { + 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." }; + } + else + { + var entity = await _customerRepository.GetCustomerAsync(model.CustomerID); + if (entity == null) + { + Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (entity == null)"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Edit the Customer." }; + } + else + { + int? refID; + using (TransactionScope scope = new TransactionScope()) + { + Mapper.MapObject(model, entity); + _customerRepository.SaveChanges(); + refID = entity.CustomerID; + scope.Complete(); + } + return new SaveViewModel(refID); + } + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel(ex.Message); + } + finally + { + } } + // CustomerModel to just return a CustomerModel instead of a SaveViewModel with the RefID. We could do this for the VM as well. + #endregion + + + + #region Delete Customer /// - /// DELETE: Customer - /// "CustomerControllerDeleteCustomer": "Customer/DeleteCustomer" + /// DELETE: Customer | SaveViewModel | delete a customer for a business and return a SaveViewModel with the RefID of the deleted customer. + /// "CustomerControllerDeleteCustomerVM": "Customer/DeleteCustomerVM" /// /// - /// - public SaveViewModel DeleteCustomer(int id) + /// SaveViewModel + public async Task DeleteCustomerVM(int CustomerID) + { + try + { + int? refID = null; + if (CustomerID == 0) + { + Log.Error("Customer API: CustomerService(DeleteCustomerVM); (CustomerID == 0)"); + return new SaveViewModel("Please provide details to delete the Customer."); + } + else + { + var entity = await _customerRepository.GetCustomerAsync(CustomerID); + + if (entity == null) + { + Log.Error("Customer API: CustomerService(DeleteCustomerVM); (entity == null)"); + return new SaveViewModel("Please provide details to delete the Customer."); + } + else + { + using (TransactionScope scope = new TransactionScope()) + { + _customerRepository.Remove(entity); + _customerRepository.SaveChanges(); + scope.Complete(); + refID = entity.CustomerID; + } + return new SaveViewModel(refID); + } + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(EditCustomer) CustomerModel; (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel(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" + /// + /// + /// 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(); + } + #endregion + + + + + + + + #region Other + #endregion } } diff --git a/MBDEVproAPI.Repository/Interfaces/IBaseRepository.cs b/MBDEVproAPI.Repository/Interfaces/IBaseRepository.cs index c71309e..5113e2d 100644 --- a/MBDEVproAPI.Repository/Interfaces/IBaseRepository.cs +++ b/MBDEVproAPI.Repository/Interfaces/IBaseRepository.cs @@ -15,7 +15,7 @@ public interface IBaseRepository /// /// /// - void Remove(int BusinessID, T obj); + void Remove(T obj); /// /// diff --git a/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs b/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs index 13f9d7a..659c22b 100644 --- a/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs +++ b/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs @@ -5,12 +5,39 @@ namespace MBDEVproAPI.Repository.Interfaces public interface ICustomerRepository : IBaseRepository { + #region + #endregion + + + + #region Get All Customers Task> GetAllCustomersVMAsync(int BusinessID); Task> GetAllCustomersAsync(int BusinessID); + #endregion + + + #region Get Customer Task GetCustomerAsync(int CustomerID); + #endregion + + + + #region Add Customer + #endregion + + + + #region Edit Customer + #endregion + + + + #region Delete Customer + public void DeleteCustomerVM(Customer obj); + + #endregion - Task AddAsync(Customer customer); } } diff --git a/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs b/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs index 1ba6559..34cca35 100644 --- a/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs +++ b/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs @@ -29,26 +29,27 @@ public CustomerRepository(MBDEVproAPIDbContext context) + #region + #endregion - - - #region Get All Customers | CustomerViewModel + #region Get All Customers /// - /// GET: Get All Customers Async + /// GET: Get All Customers | CustomerViewModel | Gets all customers for a business in a VM for web UI. /// /// - /// + /// customers public async Task> GetAllCustomersVMAsync(int BusinessID) { var customers = await _context.Customers.Where(O => O.BusinessID == BusinessID).ToListAsync(); return customers; } - #endregion - - #region Get All Customers | CustomerModel + /// GET: Get All Customers | CustomerModel | Gets all customers for a business. + /// + /// + /// customer public async Task> GetAllCustomersAsync(int BusinessID) { var customers = await _context.Customers.Where(O => O.BusinessID == BusinessID).ToListAsync(); @@ -57,7 +58,13 @@ public async Task> GetAllCustomersAsync(int BusinessID) #endregion - #region Get All Customers | Customer + + #region Get Customer + /// + /// GET: Get Customer | Customer | Gets a customer for a business. + /// + /// + /// public async Task GetCustomerAsync(int CustomerID) { var customer = await _context.Customers.Where(O => O.CustomerID == CustomerID).FirstOrDefaultAsync(); @@ -75,69 +82,54 @@ public async Task GetCustomerAsync(int CustomerID) - #region Add Customer Async - public async Task AddAsync(Customer customer) + #region Add Customer + public void Add(Customer obj) { - await _context.Customers.AddAsync(customer); - await _context.SaveChangesAsync(); // Non-blocking save + _context.Customers.Add(obj); } #endregion + #region Edit Customer + #endregion - #region other - public IEnumerable GetAll(int BusinessID) + #region Delete Customer + //Task DeleteCustomerVM(int CustomerID); + public void DeleteCustomerVM(Customer obj) { - var customers = _context.Customers.Where(O => O.BusinessID == BusinessID).ToList(); - - return customers; + _context.Customers.Remove(obj); } - ///// - ///// Get a single record by ID - ///// - ///// - ///// - ///// - public Customer GetByID(int BusinessID, int? id) + public void Remove(Customer obj) { - var customer = _context.Customers.Where(O => O.BusinessID == BusinessID && O.CustomerID == id).FirstOrDefault(); - - if (customer == null) - { - throw new Exception("Customer not found"); - } - else - { - return customer; - } + _context.Customers.Remove(obj); } + #endregion - public void Add(Customer obj) + #region Save Customer + public void SaveChanges() { - _context.Customers.Add(obj); + _context.SaveChanges(); } + #endregion + - public void Remove(int BusinessID, Customer obj) + + #region Other + public Customer GetByID(int BusinessID, int? id) { throw new NotImplementedException(); } - public void SaveChanges() + public IEnumerable GetAll(int BusinessID) { - _context.SaveChanges(); + throw new NotImplementedException(); } - - - - #endregion - - } }