diff --git a/MBDEVproAPI.API/Controllers/CustomerController.cs b/MBDEVproAPI.API/Controllers/CustomerController.cs index 6733c3a..be036be 100644 --- a/MBDEVproAPI.API/Controllers/CustomerController.cs +++ b/MBDEVproAPI.API/Controllers/CustomerController.cs @@ -20,23 +20,23 @@ public CustomerController(ICustomerService customerService) #endregion - #region CustomerViewModel + #region Get All Customers | CustomerViewModel /// - /// GET: Gets all customers for a business. + /// GET: 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 - /// "CustomerControllerGetAllCustomers": "Customer/GetAllCustomers", + /// "CustomerControllerGetAllCustomersVMAsync": "Customer/GetAllCustomersVMAsync", /// /// /// //[Route("GetAllCustomers")] //[HttpGet("{BusinessID}")] [HttpGet] - public async Task> GetAllCustomersAsync(int BusinessID) + public async Task> GetAllCustomersVMAsync(int BusinessID) { BusinessID = 52466; // temp hard code for testing, can remove later. try { - var customers = await _customerService.GetAllCustomersAsync(BusinessID); + var customers = await _customerService.GetAllCustomersVMAsync(BusinessID); if (customers == null) { @@ -53,8 +53,37 @@ public async Task> GetAllCustomersAsync(int Busi #endregion + #region Get All Customers | CustomerModel + /// + /// GET: Gets all customers for a business. + /// TEST URL: https://localhost:7092/api/Customer/GetAllCustomers?BusinessID=52466 + /// "CustomerControllerGetAllCustomersAsync": "Customer/GetAllCustomersAsync", + /// + /// + /// + [HttpGet] + public async Task> GetAllCustomersAsync(int BusinessID) + { + BusinessID = 52466; // temp hard code for testing, can remove later. + try + { + var customers = await _customerService.GetAllCustomersAsync(BusinessID); + if (customers == null) + { + return NotFound(); + } + return Ok(customers); + } + catch (Exception ex) + { + return BadRequest("Customer API error: " + ex.Message + " | " + ex.InnerException); + } + } + #endregion + + - #region Get Customer + #region Get Customer | Customer /// /// GET: Gets a customer for a business. /// TEST URL: https://localhost:7092/api/Customer/GetCustomer/3 | https://localhost:7092/api/Customer/GetCustomer?CustomerID=3 @@ -79,6 +108,39 @@ public async Task> GetCustomerAsync(int CustomerID) #endregion + + #region Add Customer | CustomerViewModel + /// + /// Create a new customer for a business from client web application using a CustomerViewModel. + /// + /// + /// + [HttpPost] + public async Task CreateCustomerVMAsync([FromBody] CustomerViewModel vm) + { + return Ok(_customerService.CreateCustomerVMAsync(vm)); + } + #endregion + + + + #region Add Customer | Customer + /// + /// Create a new customer for a business. + /// + /// + /// + [HttpPost] + public async Task CreateCustomerAsync([FromBody] CustomerModel model) + { + return Ok(_customerService.CreateCustomerAsync(model)); + //return Ok("UNDER CONTRUCTION | CreateCustomerAsync([FromBody] Customer model)"); + } + #endregion + + + + //// PUT - CREATE: api/Customer/5 //// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 //[HttpPut("{id}")] @@ -123,6 +185,7 @@ public async Task> GetCustomerAsync(int CustomerID) // return CreatedAtAction("GetCustomer", new { id = customer.CustomerID }, customer); //} + //// DELETE: api/Customer/5 //[HttpDelete("{id}")] //public async Task DeleteCustomer(int id) diff --git a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs index 4767f1a..b64b4a4 100644 --- a/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs +++ b/MBDEVproAPI.BLL/Interfaces/ICustomerService.cs @@ -8,15 +8,17 @@ namespace MBDEVproAPI.BLL.Interfaces public interface ICustomerService : IBaseService { - //CustomerViewModel GetAllCustomers(int BusinessID); + Task GetAllCustomersVMAsync(int BusinessID); - Task GetAllCustomersAsync(int BusinessID); + Task> GetAllCustomersAsync(int BusinessID); Task GetCustomerAsync(int CustomerID); + Task CreateCustomerVMAsync(CustomerViewModel vm); + + Task CreateCustomerAsync(CustomerModel model); - //CustomerModel GetCustomer(int id); SaveViewModel CreateCustomer(CustomerModel model); diff --git a/MBDEVproAPI.BLL/Services/CustomerService.cs b/MBDEVproAPI.BLL/Services/CustomerService.cs index 3e3af84..93c4c29 100644 --- a/MBDEVproAPI.BLL/Services/CustomerService.cs +++ b/MBDEVproAPI.BLL/Services/CustomerService.cs @@ -35,51 +35,13 @@ public CustomerService(MBDEVproAPIDbContext context, ICustomerRepository custome #endregion - #region CustomerViewModel - ///// - ///// GET: Customers - ///// - ///// - ///// - //public CustomerViewModel GetAllCustomers(int BusinessID) - //{ - // try - // { - // if (BusinessID == 0) - // { - // Log.Error("Customer API: CustomerService(GetAllCustomers); (BusinessID == 0)"); - // return null; - // } - // else - // { - // CustomerViewModel model = new CustomerViewModel(); - // model.CustomerList = _customerRepository.GetAllCustomers(BusinessID).Select(O => Mapper.MapObject(O, new CustomerModel())).ToList(); - // //model.BusinessID = BusinessID; - // if (model == null) - // { - // Log.Error("Customer API: CustomerService(GetAllCustomers); (model == null)"); - // return null; - // } - // else - // { - // return model; - // } - // } - // } - // catch (Exception ex) - // { - // Log.Error("Customer API: CustomerService(GetAllCustomers); (" + ex + ")" + " (" + ex.InnerException + ")"); - // return null; - // } - //} - - + #region Get All Customers | CustomerViewModel /// - /// GET: Get All Customers Async + /// GET: Gets all customers for a business in a VM for web UI. /// /// /// - public async Task GetAllCustomersAsync(int BusinessID) + public async Task GetAllCustomersVMAsync(int BusinessID) { try { @@ -91,14 +53,12 @@ public async Task GetAllCustomersAsync(int BusinessID) else { CustomerViewModel model = new CustomerViewModel(); - var customers = await _customerRepository.GetAllCustomersAsync(BusinessID); - + var customers = await _customerRepository.GetAllCustomersVMAsync(BusinessID); if (customers == null) { Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (customers == null)"); return new CustomerViewModel(); } - model.CustomerList = customers.Select(o => Mapper.MapObject(o, new CustomerModel())).ToList(); return model; } @@ -109,15 +69,74 @@ public async Task GetAllCustomersAsync(int BusinessID) return new CustomerViewModel(); } } + #endregion + #region Get All Customers | CustomerModel + public async Task> GetAllCustomersAsync(int BusinessID) + { + try + { + if (BusinessID == 0) + { + Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (BusinessID == 0)"); + return new List(); + } + else + { + // Await the repository task first, then project the results. + var customers = await _customerRepository.GetAllCustomersAsync(BusinessID); + if (customers == null) + { + Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (entities == null)"); + return new List(); + } + var entities = customers.Select(o => Mapper.MapObject(o, new CustomerModel())).ToList(); + return entities; + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(GetAllCustomersAsync); (" + ex + ")" + " (" + ex.InnerException + ")"); + return new List(); + } + } + #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 /// - /// GET: Get Customer by ID Async + /// GET: Gets a customer for a business. /// /// /// - + public async Task GetCustomerAsync(int CustomerID) // can jsut do Customer here instead of CustomerModel { try @@ -144,64 +163,115 @@ public async Task GetAllCustomersAsync(int BusinessID) return new Customer(); } } + #endregion - //public IncidentViewModel GetIncidentsByProjectID(int ProjectID) - //{ - // IncidentViewModel model = new IncidentViewModel(); - // try - // { - // if (ProjectID == 0) - // { - // Log.Error("Incidents API: IncidentService(GetIncidentsByProjectID); (ProjectID == 0)"); - // return model; - // } - // else - // { - // model = Mapper.MapObject(_incidentRepository.GetIncidentsByProjectID(ProjectID), new IncidentViewModel()); - - // if (model == null) - // { - // Log.Error("Incidents API: IncidentService(GetIncidentsByProjectID); (model == null)"); - // return model; - // } - // else - // { - // // INCIDENTS - // model.IncidentList = (List)GetIncidents(ProjectID); - // return model; - // } - // } - // } - // catch (Exception ex) - // { - // Log.Error("Incidents API: IncidentService(GetIncidentsByProjectID); (" + ex + ")" + " (" + ex.InnerException + ")"); - // return model; - // } - // finally - // { - // } - //} + #region Add Customer | CustomerViewModel + /// + /// Create a new customer for a business from client web application using a CustomerViewModel. + /// + /// + /// + [HttpPost] + public async Task CreateCustomerVMAsync(CustomerViewModel vm) + { + try + { + if (vm == null || vm.CustomerID != 0 || vm.BusinessID == 0)// we could check each condition here and log which is the issue. + { + Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (vm == null || vm.CustomerID != 0 || vm.BusinessID == 0"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." }; + } + else + { + var entity = new DataModel.Entities.Customer(); + if (entity == null) + { + Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (entity == null)"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." }; + } + else + { + int? refID; + using (TransactionScope scope = new TransactionScope()) + { + Mapper.MapObject(vm, entity); + _customerRepository.Add(entity); + _customerRepository.SaveChanges(); + refID = entity.CustomerID; + scope.Complete(); + } + return new SaveViewModel(refID); + } + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(CreateCustomerVMAsync); (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel(ex.Message); + } + finally + { + } + } + #endregion - //BusinessID = 52466; // temp hard coded for testing; need to get from token or pass in as parameter - // var customers = _customerRepository.GetAll(BusinessID).ToList(); + #region Add Customer | Customer + /// + /// CREATE: Customer + /// "CustomerControllerCreateCustomerAsync": "Customer/CreateCustomerAsync" + /// + /// + /// + public async Task CreateCustomerAsync(CustomerModel model) + { + try + { + if (model == null || model.CustomerID != 0 || model.BusinessID == 0)// we could check each condition here and log which is the issue. + { + Log.Error("Customer API: CustomerService(CreateCustomerAsync); (model == null || model.CustomerID != 0 || model.BusinessID == 0"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." }; + } + else + { + var entity = new DataModel.Entities.Customer(); + if (entity == null) + { + Log.Error("Customer API: CustomerService(CreateCustomerAsync); (entity == null)"); + return new SaveViewModel { IsSaved = false, ErrorMessage = "Please provide details to Create the Customer." }; + } + else + { + int? refID; + using (TransactionScope scope = new TransactionScope()) + { + Mapper.MapObject(model, entity); + _customerRepository.Add(entity); + _customerRepository.SaveChanges(); + refID = entity.CustomerID; + scope.Complete(); + } + return new SaveViewModel(refID); + } + } + } + catch (Exception ex) + { + Log.Error("Customer API: CustomerService(CreateCustomerAsync); (" + ex + ")" + " (" + ex.InnerException + ")"); + return new SaveViewModel(ex.Message); + } + finally + { + + } - // if (customers == null) - // { - // Log.Error("Customers API: CustomerService(GetAllCustomers); (customers == null"); - // return null; // return empty model? - // } - // else - // { - // return customers.ToList(); - // } - //} + } #endregion @@ -268,6 +338,22 @@ public SaveViewModel DeleteCustomer(int id) + + + + + + + + + + + + + + + + diff --git a/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs b/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs index d05e87e..13f9d7a 100644 --- a/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs +++ b/MBDEVproAPI.Repository/Interfaces/ICustomerRepository.cs @@ -5,8 +5,12 @@ namespace MBDEVproAPI.Repository.Interfaces public interface ICustomerRepository : IBaseRepository { + Task> GetAllCustomersVMAsync(int BusinessID); + Task> GetAllCustomersAsync(int BusinessID); Task GetCustomerAsync(int CustomerID); + + Task AddAsync(Customer customer); } } diff --git a/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs b/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs index 7e14af1..1ba6559 100644 --- a/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs +++ b/MBDEVproAPI.Repository/Repositories/CustomerRepository.cs @@ -34,12 +34,21 @@ public CustomerRepository(MBDEVproAPIDbContext context) - #region Get All Customers Async + #region Get All Customers | CustomerViewModel /// /// GET: Get All Customers Async /// /// /// + 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 public async Task> GetAllCustomersAsync(int BusinessID) { var customers = await _context.Customers.Where(O => O.BusinessID == BusinessID).ToListAsync(); @@ -48,7 +57,7 @@ public async Task> GetAllCustomersAsync(int BusinessID) #endregion - #region Get Customer by CustomerID Async + #region Get All Customers | Customer public async Task GetCustomerAsync(int CustomerID) { var customer = await _context.Customers.Where(O => O.CustomerID == CustomerID).FirstOrDefaultAsync(); @@ -66,7 +75,13 @@ public async Task GetCustomerAsync(int CustomerID) - + #region Add Customer Async + public async Task AddAsync(Customer customer) + { + await _context.Customers.AddAsync(customer); + await _context.SaveChangesAsync(); // Non-blocking save + } + #endregion @@ -104,7 +119,7 @@ public Customer GetByID(int BusinessID, int? id) public void Add(Customer obj) { - throw new NotImplementedException(); + _context.Customers.Add(obj); } public void Remove(int BusinessID, Customer obj) @@ -114,9 +129,11 @@ public void Remove(int BusinessID, Customer obj) public void SaveChanges() { - throw new NotImplementedException(); + _context.SaveChanges(); } + + #endregion