phronCare/Core/Services/CustomerService.cs

95 lines
3.5 KiB
C#
Raw Normal View History

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