phronCare/Core/Services/PatientService.cs

130 lines
4.8 KiB
C#
Raw Normal View History

using Core.Interfaces;
using Domain.Entities;
using Domain.Generics;
using Domain.SearchParams;
using Models.Interfaces;
using System.Reflection;
using Transversal.Services;
2025-05-08 15:46:04 -03:00
namespace Core.Services
{
2025-05-08 15:46:04 -03:00
public class PatientService : IPatientDom
{
2025-05-08 15:46:04 -03:00
#region Declaraciones y Constructor
private readonly IPhSPatientRepository _repository;
public PatientService(IPhSPatientRepository patientRepository)
{
2025-05-08 15:46:04 -03:00
_repository = patientRepository ?? throw new ArgumentNullException(nameof(patientRepository));
}
2025-05-08 15:46:04 -03:00
#endregion
#region Métodos de servicio
public async Task<PagedResult<EPatient>> GetAllAsync(int page = 1, int pageSize = 50)
{
2025-05-08 15:46:04 -03:00
try
{
return await _repository.GetAllAsync(page, pageSize);
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} Message: {ex.Message}", ex);
}
}
2025-05-08 15:46:04 -03:00
public async Task<EPatient?> GetByIdAsync(int id)
{
2025-05-08 15:46:04 -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-05-08 15:46:04 -03:00
public async Task<EPatient> CreateAsync(EPatient entity)
{
2025-05-08 15:46:04 -03:00
if (entity is null)
throw new ArgumentNullException(nameof(entity), "El paciente no puede ser nulo.");
2025-05-08 15:46:04 -03:00
if (string.IsNullOrWhiteSpace(entity.Firstname) || string.IsNullOrWhiteSpace(entity.Lastname))
throw new ArgumentException("Debe completar el nombre y apellido del paciente.");
2025-05-08 15:46:04 -03:00
if (string.IsNullOrWhiteSpace(entity.DocumentNumber))
throw new ArgumentException("Debe completar el número de documento del paciente.");
2025-05-08 15:46:04 -03:00
return await _repository.CreateAsync(entity);
}
public async Task<bool> UpdateAsync(EPatient entity)
{
if (entity is null)
throw new ArgumentNullException(nameof(entity), "El paciente no puede ser nulo.");
2025-05-08 15:46:04 -03:00
if (string.IsNullOrWhiteSpace(entity.Firstname) || string.IsNullOrWhiteSpace(entity.Lastname))
throw new ArgumentException("Debe completar el nombre y apellido del paciente.");
2025-05-08 15:46:04 -03:00
if (string.IsNullOrWhiteSpace(entity.DocumentNumber))
throw new ArgumentException("Debe completar el número de documento del paciente.");
2025-05-08 15:46:04 -03:00
return await _repository.UpdateAsync(entity);
}
public async Task<PagedResult<EPatient>> SearchAsync(string? name, string? document, int page = 1, int pageSize = 50)
{
2025-05-08 15:46:04 -03:00
try
{
return await _repository.SearchAsync(name, document, page, pageSize);
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} Message: {ex.Message}", ex);
}
}
2025-05-08 15:46:04 -03:00
public Task<bool> DeleteAsync(int id)
{
2025-05-08 15:46:04 -03:00
// Implementar según políticas del sistema (soft delete, etc.)
throw new NotImplementedException();
}
2025-05-08 15:46:04 -03:00
public async Task<byte[]> ExportFilteredPatientsToExcelAsync(PatientSearchParams searchParams)
{
2025-05-08 15:46:04 -03:00
try
{
2025-05-08 15:46:04 -03:00
var searchResult = await SearchAsync(
searchParams.Name,
searchParams.Document,
searchParams.Page,
searchParams.PageSize
);
2025-05-08 15:46:04 -03:00
if (searchResult?.Items is null || !searchResult.Items.Any())
{
throw new Exception("No se encontraron pacientes para exportar.");
}
2025-05-08 15:46:04 -03:00
var stream = new XLSXExportBase();
2025-05-08 15:46:04 -03:00
var patientData = searchResult.Items.Select(p => new
{
p.Id,
p.Firstname,
p.Lastname,
p.DocumentNumber,
p.AffiliateNumber,
p.Email,
p.Phone,
p.Birthdate,
p.Gender
}).ToList();
var excelFile = stream.ExportExcel(patientData);
return excelFile;
}
catch (Exception ex)
{
var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
throw new Exception($"{methodName} - Error al exportar pacientes: {ex.Message}", ex);
}
}
2025-05-08 15:46:04 -03:00
#endregion
}
2025-05-08 15:46:04 -03:00
}