2025-06-27 13:52:22 -03:00
|
|
|
|
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;
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
public async Task<PagedResult<ELSProductDivision>> GetAllAsync(int page = 1, int pageSize = 50)
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
|
|
|
|
|
var query = _context.PhLsmProductDivisions.AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
return new PagedResult<ELSProductDivision>
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
2025-06-30 16:15:08 -03:00
|
|
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhLsmProductDivision, ELSProductDivision>),
|
2025-06-27 13:52:22 -03:00
|
|
|
|
TotalItems = pagedEntities.TotalItems,
|
|
|
|
|
|
Page = pagedEntities.Page,
|
|
|
|
|
|
PageSize = pagedEntities.PageSize
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
public async Task<ELSProductDivision?> GetByIdAsync(int id)
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
|
|
|
|
|
var entity = await _context.PhLsmProductDivisions.FirstOrDefaultAsync(x => x.Id == id);
|
2025-06-30 16:15:08 -03:00
|
|
|
|
return entity is null ? null : EntityMapper.MapEntity<PhLsmProductDivision, ELSProductDivision>(entity);
|
2025-06-27 13:52:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
public async Task<PagedResult<ELSProductDivision>> SearchAsync(string? term, int page = 1, int pageSize = 50)
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
|
|
|
|
|
var query = _context.PhLsmProductDivisions.AsQueryable();
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(term))
|
|
|
|
|
|
{
|
|
|
|
|
|
term = term.ToLower();
|
2025-06-27 17:24:31 -03:00
|
|
|
|
query = query.Where(x =>
|
|
|
|
|
|
(x.Code != null && x.Code.ToLower().Contains(term)) ||
|
|
|
|
|
|
(x.Name != null && x.Name.ToLower().Contains(term)) ||
|
|
|
|
|
|
(x.Description != null && x.Description.ToLower().Contains(term))
|
|
|
|
|
|
);
|
2025-06-27 13:52:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
return new PagedResult<ELSProductDivision>
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
2025-06-30 16:15:08 -03:00
|
|
|
|
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhLsmProductDivision, ELSProductDivision>),
|
2025-06-27 13:52:22 -03:00
|
|
|
|
TotalItems = pagedEntities.TotalItems,
|
|
|
|
|
|
Page = pagedEntities.Page,
|
|
|
|
|
|
PageSize = pagedEntities.PageSize
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
public async Task<ELSProductDivision> CreateAsync(ELSProductDivision entity)
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
|
throw new ArgumentNullException(nameof(entity));
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-06-30 16:15:08 -03:00
|
|
|
|
var mapped = EntityMapper.MapEntity<ELSProductDivision, PhLsmProductDivision>(entity);
|
2025-06-27 13:52:22 -03:00
|
|
|
|
_context.PhLsmProductDivisions.Add(mapped);
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
2025-06-30 16:15:08 -03:00
|
|
|
|
return EntityMapper.MapEntity<PhLsmProductDivision, ELSProductDivision>(mapped);
|
2025-06-27 13:52:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-30 16:15:08 -03:00
|
|
|
|
public async Task<bool> UpdateAsync(ELSProductDivision entity)
|
2025-06-27 13:52:22 -03:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-07-14 16:16:05 -03:00
|
|
|
|
public async Task<List<string>> GetAllCodesAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _context.PhLsmProductDivisions
|
|
|
|
|
|
.Where(x => !string.IsNullOrEmpty(x.Code))
|
|
|
|
|
|
.Select(x => x.Code!)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ExistsByCodeAsync(string code)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _context.PhLsmProductDivisions
|
|
|
|
|
|
.AnyAsync(x => x.Code != null && x.Code == code);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-27 13:52:22 -03:00
|
|
|
|
}
|
|
|
|
|
|
}
|