From f15b3b159c934067c88f221fcfc458a2d364bb44 Mon Sep 17 00:00:00 2001 From: Leandro Hernan Rojas Date: Fri, 4 Apr 2025 14:58:44 -0300 Subject: [PATCH] Add CustomerService in API --- Core/Interfaces/ICustomerDom.cs | 20 +++++ Core/Services/CustomerService.cs | 58 ++++++++++++++ Models/Interfaces/IPhSCustomerRepository.cs | 20 +++++ .../Repositories/PhSAccountTypeRepository.cs | 1 - Models/Repositories/PhSCustomerRepository.cs | 79 +++++++++++++++++++ .../Repositories/PhSDocumentTypeRepository.cs | 1 - .../Sales/AccountTypeController.cs | 2 - .../Controllers/Sales/CustomerController.cs | 30 +++++++ .../Sales/DocumentTypeController.cs | 2 - phronCare.API/Program.cs | 9 ++- 10 files changed, 212 insertions(+), 10 deletions(-) create mode 100644 Core/Interfaces/ICustomerDom.cs create mode 100644 Core/Services/CustomerService.cs create mode 100644 Models/Interfaces/IPhSCustomerRepository.cs create mode 100644 Models/Repositories/PhSCustomerRepository.cs create mode 100644 phronCare.API/Controllers/Sales/CustomerController.cs diff --git a/Core/Interfaces/ICustomerDom.cs b/Core/Interfaces/ICustomerDom.cs new file mode 100644 index 0000000..3f8520c --- /dev/null +++ b/Core/Interfaces/ICustomerDom.cs @@ -0,0 +1,20 @@ +using Domain.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Core.Interfaces +{ + public interface ICustomerDom + { + Task AddAsync(ECustomer entity); + Task DeleteAsync(int id); + Task> GetAllAsync(); + Task GetByIdAsync(int id); + Task> SearchAsync(string? name, string? email, string? document); + Task UpdateAsync(ECustomer entity); + } +} + diff --git a/Core/Services/CustomerService.cs b/Core/Services/CustomerService.cs new file mode 100644 index 0000000..c1f18ad --- /dev/null +++ b/Core/Services/CustomerService.cs @@ -0,0 +1,58 @@ +using System.Reflection; +using Core.Interfaces; +using Domain.Entities; +using Models.Helpers; +using Models.Interfaces; +using Models.Models; + +namespace Core.Services +{ + public class CustomerService: ICustomerDom + { + #region Declaraciones y Constructor + private readonly IPhSCustomerRepository _repository; + + public CustomerService(IPhSCustomerRepository customerRepository) + { + _repository = customerRepository ?? throw new ArgumentNullException(nameof(customerRepository)); + } + #endregion + public Task AddAsync(ECustomer entity) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetAllAsync() + { + try + { + return await _repository.GetAllAsync(); + } + catch (Exception ex) + { + var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + throw new Exception($"{methodName} Message: {ex.Message}", ex); + } + } + + public Task GetByIdAsync(int id) + { + throw new NotImplementedException(); + } + + public Task> SearchAsync(string? name, string? email, string? document) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(ECustomer entity) + { + throw new NotImplementedException(); + } + } +} diff --git a/Models/Interfaces/IPhSCustomerRepository.cs b/Models/Interfaces/IPhSCustomerRepository.cs new file mode 100644 index 0000000..f0dba74 --- /dev/null +++ b/Models/Interfaces/IPhSCustomerRepository.cs @@ -0,0 +1,20 @@ +using Domain.Entities; +using Models.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Models.Interfaces +{ + public interface IPhSCustomerRepository + { + Task AddAsync(ECustomer entity); + Task DeleteAsync(int id); + Task> GetAllAsync(); + Task GetByIdAsync(int id); + Task> SearchAsync(string? name, string? email, string? document); + Task UpdateAsync(ECustomer entity); + } +} diff --git a/Models/Repositories/PhSAccountTypeRepository.cs b/Models/Repositories/PhSAccountTypeRepository.cs index 09c6e34..0c7fb6f 100644 --- a/Models/Repositories/PhSAccountTypeRepository.cs +++ b/Models/Repositories/PhSAccountTypeRepository.cs @@ -25,5 +25,4 @@ namespace Models.Repositories } #endregion } - } diff --git a/Models/Repositories/PhSCustomerRepository.cs b/Models/Repositories/PhSCustomerRepository.cs new file mode 100644 index 0000000..0fb6062 --- /dev/null +++ b/Models/Repositories/PhSCustomerRepository.cs @@ -0,0 +1,79 @@ +using Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Models.Helpers; +using Models.Interfaces; +using Models.Models; +using System.Xml.Linq; + +namespace Models.Repositories +{ + public class PhSCustomerRepository(PhronCareOperationsHubContext context) : IPhSCustomerRepository + { + #region Declaraciones y Constructor + private readonly PhronCareOperationsHubContext _context = context; + #endregion + public async Task> GetAllAsync() + { + var customers = await _context.PhSCustomers + .Include(c => c.Accounttypes) + .ToListAsync(); + return customers.Select(EntityMapper.MapEntity); + } + public async Task GetByIdAsync(int id) + { + var customer = await _context.PhSCustomers + .Include(c => c.Accounttypes) + .Include(c => c.PhSCustomerAddresses) + .Include(c => c.PhSCustomerDocuments) + .Include(c => c.PhSQuoteHeaders) + .FirstOrDefaultAsync(c => c.Id == id); + + return customer != null ? EntityMapper.MapEntity(customer) : null; + } + public async Task> SearchAsync(string? name, string? document, string? email) + { + var query = _context.PhSCustomers + .Include(c => c.Accounttypes) + .Include(c => c.PhSCustomerDocuments) + .Include(c => c.PhSCustomerAddresses) + .AsQueryable(); + + if (!string.IsNullOrEmpty(name)) + query = query.Where(c => c.Name.Contains(name) || c.BusinessName.Contains(name)); + + if (!string.IsNullOrEmpty(document)) + query = query.Where(c => c.PhSCustomerDocuments.Any(d => d.DocumentNumber == document)); + + if (!string.IsNullOrEmpty(email)) + query = query.Where(c => c.PhSCustomerAddresses.Any(a => a.Email == email)); + + var customers = await query.ToListAsync(); + return customers.Select(EntityMapper.MapEntity); + } + public async Task AddAsync(ECustomer entity) + { + var customer = EntityMapper.MapEntity(entity); + await _context.PhSCustomers.AddAsync(customer); + await _context.SaveChangesAsync(); + return EntityMapper.MapEntity(customer); + } + public async Task UpdateAsync(ECustomer entity) + { + var customer = await _context.PhSCustomers.FindAsync(entity.Id); + if (customer == null) return false; + + _context.Entry(customer).CurrentValues.SetValues(entity); + await _context.SaveChangesAsync(); + return true; + } + public async Task DeleteAsync(int id) + { + var customer = await _context.PhSCustomers.FindAsync(id); + if (customer == null) return false; + + _context.PhSCustomers.Remove(customer); + await _context.SaveChangesAsync(); + return true; + } + } +} diff --git a/Models/Repositories/PhSDocumentTypeRepository.cs b/Models/Repositories/PhSDocumentTypeRepository.cs index 1564617..2f0eb26 100644 --- a/Models/Repositories/PhSDocumentTypeRepository.cs +++ b/Models/Repositories/PhSDocumentTypeRepository.cs @@ -23,6 +23,5 @@ namespace Models.Repositories return documentType != null ? EntityMapper.MapEntity(documentType) : null; } #endregion - } } diff --git a/phronCare.API/Controllers/Sales/AccountTypeController.cs b/phronCare.API/Controllers/Sales/AccountTypeController.cs index 60bca94..7408087 100644 --- a/phronCare.API/Controllers/Sales/AccountTypeController.cs +++ b/phronCare.API/Controllers/Sales/AccountTypeController.cs @@ -1,7 +1,6 @@ using Core.Interfaces; using Microsoft.AspNetCore.Mvc; - namespace phronCare.API.Controllers.Sales { [Route("api/[controller]")] @@ -9,7 +8,6 @@ namespace phronCare.API.Controllers.Sales public class AccountTypeController : ControllerBase { private readonly IAccountTypeDom _accountTypeService; - public AccountTypeController(IAccountTypeDom accountTypeService) { _accountTypeService = accountTypeService ?? throw new ArgumentNullException(nameof(accountTypeService)); diff --git a/phronCare.API/Controllers/Sales/CustomerController.cs b/phronCare.API/Controllers/Sales/CustomerController.cs new file mode 100644 index 0000000..425c877 --- /dev/null +++ b/phronCare.API/Controllers/Sales/CustomerController.cs @@ -0,0 +1,30 @@ +using Core.Interfaces; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace phronCare.API.Controllers.Sales +{ + [Route("api/[controller]")] + [ApiController] + public class CustomerController : ControllerBase + { + private readonly ICustomerDom _customerService; + public CustomerController(ICustomerDom customerService) + { + _customerService = customerService ?? throw new ArgumentNullException(nameof(customerService)); + } + [HttpGet("GetAll")] + public async Task GetAll() + { + try + { + var result = await _customerService.GetAllAsync(); + return Ok(result); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + } +} diff --git a/phronCare.API/Controllers/Sales/DocumentTypeController.cs b/phronCare.API/Controllers/Sales/DocumentTypeController.cs index a427e28..18dc9ea 100644 --- a/phronCare.API/Controllers/Sales/DocumentTypeController.cs +++ b/phronCare.API/Controllers/Sales/DocumentTypeController.cs @@ -8,7 +8,6 @@ namespace phronCare.API.Controllers.Sales public class DocumentTypeController : ControllerBase { private readonly IDocumentTypeDom _documentTypeService; - public DocumentTypeController(IDocumentTypeDom documentTypeService) { _documentTypeService = documentTypeService ?? throw new ArgumentNullException(nameof(documentTypeService)); @@ -35,6 +34,5 @@ namespace phronCare.API.Controllers.Sales return Ok(result); } - } } diff --git a/phronCare.API/Program.cs b/phronCare.API/Program.cs index 177c0cc..d9214ed 100644 --- a/phronCare.API/Program.cs +++ b/phronCare.API/Program.cs @@ -31,13 +31,14 @@ builder.Services.AddDbContext(options => #endregion #region Repositorios y Servicios -builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); - +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); #endregion #region Require Confirmed Email