All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 7m45s
109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using Domain.Entities;
|
|
using Domain.Generics;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Models.Helpers;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
|
|
namespace Models.Repositories.Stock
|
|
{
|
|
public class PhLSMProductDivisionRepository(PhronCareOperationsHubContext context) : IPhLSMProductDivisionRepository
|
|
{
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
|
|
public async Task<PagedResult<EProductDivision>> GetAllAsync(int page = 1, int pageSize = 50)
|
|
{
|
|
var query = _context.PhLsmProductDivisions.AsQueryable();
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
return new PagedResult<EProductDivision>
|
|
{
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhLsmProductDivision, EProductDivision>),
|
|
TotalItems = pagedEntities.TotalItems,
|
|
Page = pagedEntities.Page,
|
|
PageSize = pagedEntities.PageSize
|
|
};
|
|
}
|
|
|
|
public async Task<EProductDivision?> GetByIdAsync(int id)
|
|
{
|
|
var entity = await _context.PhLsmProductDivisions.FirstOrDefaultAsync(x => x.Id == id);
|
|
return entity is null ? null : EntityMapper.MapEntity<PhLsmProductDivision, EProductDivision>(entity);
|
|
}
|
|
|
|
public async Task<PagedResult<EProductDivision>> SearchAsync(string? term, int page = 1, int pageSize = 50)
|
|
{
|
|
var query = _context.PhLsmProductDivisions.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(term))
|
|
{
|
|
term = term.ToLower();
|
|
query = query.Where(x => x.Description.ToLower().Contains(term));
|
|
}
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
return new PagedResult<EProductDivision>
|
|
{
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhLsmProductDivision, EProductDivision>),
|
|
TotalItems = pagedEntities.TotalItems,
|
|
Page = pagedEntities.Page,
|
|
PageSize = pagedEntities.PageSize
|
|
};
|
|
}
|
|
|
|
public async Task<EProductDivision> CreateAsync(EProductDivision entity)
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException(nameof(entity));
|
|
|
|
try
|
|
{
|
|
var mapped = EntityMapper.MapEntity<EProductDivision, PhLsmProductDivision>(entity);
|
|
_context.PhLsmProductDivisions.Add(mapped);
|
|
await _context.SaveChangesAsync();
|
|
return EntityMapper.MapEntity<PhLsmProductDivision, EProductDivision>(mapped);
|
|
}
|
|
catch (DbUpdateException dbEx)
|
|
{
|
|
throw new Exception("Error al guardar la división de producto. Verificá integridad de datos.", dbEx);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error inesperado al crear la división: " + ex.Message, ex);
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(EProductDivision entity)
|
|
{
|
|
if (entity == null)
|
|
throw new ArgumentNullException(nameof(entity));
|
|
|
|
try
|
|
{
|
|
var existing = await _context.PhLsmProductDivisions.FirstOrDefaultAsync(x => x.Id == entity.Id);
|
|
if (existing == null) return false;
|
|
|
|
EntityMapper.MapEntityToExisting(entity, existing);
|
|
await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id)
|
|
{
|
|
var existing = await _context.PhLsmProductDivisions.FindAsync(id);
|
|
if (existing == null) return false;
|
|
|
|
_context.PhLsmProductDivisions.Remove(existing);
|
|
await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
}
|
|
}
|