phronCare/Core/Services/CustomerService.cs
Leandro Hernan Rojas 0177366fc9
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m4s
Update Customers Update Create and List
2025-04-12 18:51:25 -03:00

95 lines
3.5 KiB
C#

using System.Drawing.Printing;
using System.Reflection;
using Azure;
using System.Reflection.Metadata;
using Core.Interfaces;
using Domain.Entities;
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
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 async Task<PagedResult<ECustomer>> GetAllAsync(int page = 1, int pageSize = 50)
{
try
{
return await _repository.GetAllAsync(page, pageSize);
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} Message: {ex.Message}", ex);
}
}
public async Task<ECustomer?> GetByIdAsync(int id)
{
try
{
return await _repository.GetByIdAsync(id);
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} Message: {ex.Message}", ex);
}
}
public async Task<ECustomer> CreateAsync(ECustomer entity)
{
if (entity is null)
throw new ArgumentNullException(nameof(entity), "El cliente no puede ser nulo.");
if (!entity.AccounttypesId.HasValue)
throw new ArgumentException("Debe seleccionar un tipo de cuenta.", nameof(entity.AccounttypesId));
if (entity.PhSCustomerDocuments == null || !entity.PhSCustomerDocuments.Any())
throw new ArgumentException("El cliente debe tener al menos un documento (por ejemplo, CUIT).", nameof(entity.PhSCustomerDocuments));
return await _repository.CreateAsync(entity);
}
public async Task<PagedResult<ECustomer>> SearchAsync(
string? name, string? email, string? document,
int page = 1, int pageSize = 50)
{
try
{
return await _repository.SearchAsync(name, email, document, page, pageSize);
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} Message: {ex.Message}", ex);
}
}
public async Task<bool> UpdateAsync(ECustomer entity)
{
if (entity is null)
throw new ArgumentNullException(nameof(entity), "El cliente no puede ser nulo.");
if (!entity.AccounttypesId.HasValue)
throw new ArgumentException("Debe seleccionar un tipo de cuenta.", nameof(entity.AccounttypesId));
if (entity.PhSCustomerDocuments == null || !entity.PhSCustomerDocuments.Any())
throw new ArgumentException("El cliente debe tener al menos un documento (por ejemplo, CUIT).", nameof(entity.PhSCustomerDocuments));
return await _repository.UpdateAsync(entity);
}
public Task<bool> DeleteAsync(int id)
{
throw new NotImplementedException();
}
}
}