97 lines
3.8 KiB
C#
97 lines
3.8 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Models.Interfaces;
|
|||
|
|
using Domain.Entities;
|
|||
|
|
using Models.Helpers;
|
|||
|
|
using Models.Models;
|
|||
|
|
|
|||
|
|
namespace Models.Repositories
|
|||
|
|
{
|
|||
|
|
public class PhSBusinessUnitRepository(PhronCareOperationsHubContext context) : IPhSBusinessUnitRepository
|
|||
|
|
{
|
|||
|
|
#region Declaraciones y Constructor
|
|||
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|||
|
|
#endregion
|
|||
|
|
#region Metodos de clase
|
|||
|
|
public async Task<IEnumerable<EBusinessUnit>> GetAllAsync()
|
|||
|
|
{
|
|||
|
|
var businessUnits = await _context.PhSBusinessUnits.ToListAsync();
|
|||
|
|
return businessUnits.Select(EntityMapper.MapEntity<PhSBusinessUnit, EBusinessUnit>);
|
|||
|
|
}
|
|||
|
|
public async Task<EBusinessUnit?> GetByIdAsync(int id)
|
|||
|
|
{
|
|||
|
|
var entity = await _context.PhSBusinessUnits.FindAsync(id);
|
|||
|
|
return entity is null ? null : EntityMapper.MapEntity<PhSBusinessUnit, EBusinessUnit>(entity);
|
|||
|
|
}
|
|||
|
|
public async Task<IEnumerable<EBusinessUnit>> SearchAsync(string term)
|
|||
|
|
{
|
|||
|
|
var query = _context.PhSBusinessUnits.AsQueryable();
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrWhiteSpace(term))
|
|||
|
|
{
|
|||
|
|
term = term.ToLower();
|
|||
|
|
query = query.Where(x =>
|
|||
|
|
x.Description.ToLower().Contains(term) ||
|
|||
|
|
x.Code.ToLower().Contains(term) ||
|
|||
|
|
x.Manager.ToLower().Contains(term));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var result = await query.ToListAsync();
|
|||
|
|
return result.Select(EntityMapper.MapEntity<PhSBusinessUnit, EBusinessUnit>);
|
|||
|
|
}
|
|||
|
|
public async Task<EBusinessUnit> CreateAsync(EBusinessUnit unit)
|
|||
|
|
{
|
|||
|
|
if (unit == null) throw new ArgumentNullException(nameof(unit), "La unidad de negocio no puede ser nulo.");
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var businessUnit = EntityMapper.MapEntity<EBusinessUnit, PhSBusinessUnit>(unit);
|
|||
|
|
await _context.PhSBusinessUnits.AddAsync(businessUnit);
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return EntityMapper.MapEntity<PhSBusinessUnit, EBusinessUnit>(businessUnit);
|
|||
|
|
}
|
|||
|
|
catch (DbUpdateException dbEx)
|
|||
|
|
{
|
|||
|
|
// Error relacionado con la base de datos (como violación de integridad referencial)
|
|||
|
|
throw new Exception("Error al guardar la unidad de negocio en la base de datos. Es posible que haya un problema con la integridad de los datos.", dbEx);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
// Captura cualquier otro tipo de excepción
|
|||
|
|
throw new Exception("Error inesperado al crear la unidad de negocio: " + ex.Message, ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<bool> UpdateAsync(EBusinessUnit unit)
|
|||
|
|
{
|
|||
|
|
if (unit == null) throw new ArgumentNullException(nameof(unit), "La unidad de negocio no puede ser nula.");
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var existingUnit = await _context.PhSBusinessUnits
|
|||
|
|
.FirstOrDefaultAsync(c => c.Id == unit.Id);
|
|||
|
|
|
|||
|
|
if (existingUnit == null)
|
|||
|
|
return false;
|
|||
|
|
|
|||
|
|
// Mapea los cambios del modelo EBusinessUnit a la entidad trackeada por EF
|
|||
|
|
EntityMapper.MapEntityToExisting(unit, existingUnit);
|
|||
|
|
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<bool> DeleteAsync(int id)
|
|||
|
|
{
|
|||
|
|
var entity = await _context.PhSBusinessUnits.FindAsync(id);
|
|||
|
|
if (entity != null)
|
|||
|
|
{
|
|||
|
|
_context.PhSBusinessUnits.Remove(entity);
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|