Add CustomerService in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m58s
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m58s
This commit is contained in:
parent
ee3a76ce3f
commit
f15b3b159c
20
Core/Interfaces/ICustomerDom.cs
Normal file
20
Core/Interfaces/ICustomerDom.cs
Normal file
@ -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<ECustomer> AddAsync(ECustomer entity);
|
||||
Task<bool> DeleteAsync(int id);
|
||||
Task<IEnumerable<ECustomer>> GetAllAsync();
|
||||
Task<ECustomer?> GetByIdAsync(int id);
|
||||
Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? email, string? document);
|
||||
Task<bool> UpdateAsync(ECustomer entity);
|
||||
}
|
||||
}
|
||||
|
||||
58
Core/Services/CustomerService.cs
Normal file
58
Core/Services/CustomerService.cs
Normal file
@ -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<ECustomer> AddAsync(ECustomer entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> DeleteAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ECustomer>> 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<ECustomer?> GetByIdAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? email, string? document)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> UpdateAsync(ECustomer entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Models/Interfaces/IPhSCustomerRepository.cs
Normal file
20
Models/Interfaces/IPhSCustomerRepository.cs
Normal file
@ -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<ECustomer> AddAsync(ECustomer entity);
|
||||
Task<bool> DeleteAsync(int id);
|
||||
Task<IEnumerable<ECustomer>> GetAllAsync();
|
||||
Task<ECustomer?> GetByIdAsync(int id);
|
||||
Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? email, string? document);
|
||||
Task<bool> UpdateAsync(ECustomer entity);
|
||||
}
|
||||
}
|
||||
@ -25,5 +25,4 @@ namespace Models.Repositories
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
79
Models/Repositories/PhSCustomerRepository.cs
Normal file
79
Models/Repositories/PhSCustomerRepository.cs
Normal file
@ -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<IEnumerable<ECustomer>> GetAllAsync()
|
||||
{
|
||||
var customers = await _context.PhSCustomers
|
||||
.Include(c => c.Accounttypes)
|
||||
.ToListAsync();
|
||||
return customers.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>);
|
||||
}
|
||||
public async Task<ECustomer?> 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<PhSCustomer, ECustomer>(customer) : null;
|
||||
}
|
||||
public async Task<IEnumerable<ECustomer>> 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<PhSCustomer, ECustomer>);
|
||||
}
|
||||
public async Task<ECustomer> AddAsync(ECustomer entity)
|
||||
{
|
||||
var customer = EntityMapper.MapEntity<ECustomer, PhSCustomer>(entity);
|
||||
await _context.PhSCustomers.AddAsync(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
return EntityMapper.MapEntity<PhSCustomer, ECustomer>(customer);
|
||||
}
|
||||
public async Task<bool> 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<bool> DeleteAsync(int id)
|
||||
{
|
||||
var customer = await _context.PhSCustomers.FindAsync(id);
|
||||
if (customer == null) return false;
|
||||
|
||||
_context.PhSCustomers.Remove(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,5 @@ namespace Models.Repositories
|
||||
return documentType != null ? EntityMapper.MapEntity<PhSDocumentType, EDocumentType>(documentType) : null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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));
|
||||
|
||||
30
phronCare.API/Controllers/Sales/CustomerController.cs
Normal file
30
phronCare.API/Controllers/Sales/CustomerController.cs
Normal file
@ -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<IActionResult> GetAll()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _customerService.GetAllAsync();
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,13 +31,14 @@ builder.Services.AddDbContext<PhronCareOperationsHubContext>(options =>
|
||||
#endregion
|
||||
|
||||
#region Repositorios y Servicios
|
||||
builder.Services.AddScoped<ITicketRepository, TicketRepository>();
|
||||
builder.Services.AddScoped<ITicketDom, TicketService>();
|
||||
builder.Services.AddScoped<IPhSAccountTypeRepository, PhSAccountTypeRepository>();
|
||||
builder.Services.AddScoped<ITicketRepository, TicketRepository>();
|
||||
builder.Services.AddScoped<IAccountTypeDom, AccountTypeService>();
|
||||
builder.Services.AddScoped<IPhSDocumentTypeRepository, PhSDocumentTypeRepository>();
|
||||
builder.Services.AddScoped<IPhSAccountTypeRepository, PhSAccountTypeRepository>();
|
||||
builder.Services.AddScoped<IDocumentTypeDom, DocumentTypeService>();
|
||||
|
||||
builder.Services.AddScoped<IPhSDocumentTypeRepository, PhSDocumentTypeRepository>();
|
||||
builder.Services.AddScoped<ICustomerDom, CustomerService>();
|
||||
builder.Services.AddScoped<IPhSCustomerRepository, PhSCustomerRepository>();
|
||||
#endregion
|
||||
|
||||
#region Require Confirmed Email
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user