phronCare/Models/Repositories/Stock/PhLSMProductRepository.cs

102 lines
3.8 KiB
C#
Raw Normal View History

2025-06-30 16:15:08 -03:00
using Domain.Entities;
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces;
using Models.Models;
namespace Models.Repositories
{
public class PhLSMProductRepository(PhronCareOperationsHubContext context) : IPhLSMProductRepository
{
private readonly PhronCareOperationsHubContext _context = context;
public async Task<PagedResult<ELSProduct>> SearchAsync(LSProductSearchParams searchParams)
{
var query = _context.PhLsmProducts
.Include(p => p.Division)
.Include(p => p.Unit)
.AsQueryable();
if (!string.IsNullOrWhiteSpace(searchParams.Code))
{
var lowered = searchParams.Code.ToLower();
query = query.Where(p =>
(!string.IsNullOrEmpty(p.FactoryCode) && p.FactoryCode.ToLower().Contains(lowered)) ||
(!string.IsNullOrEmpty(p.ExternalCode) && p.ExternalCode.ToLower().Contains(lowered)));
}
if (!string.IsNullOrWhiteSpace(searchParams.Description))
{
var lowered = searchParams.Description.ToLower();
query = query.Where(p =>
(!string.IsNullOrEmpty(p.Name) && p.Name.ToLower().Contains(lowered)) ||
(!string.IsNullOrEmpty(p.Descripcion) && p.Descripcion.ToLower().Contains(lowered)));
}
if (searchParams.ProductType.HasValue)
query = query.Where(p => p.ProductType == searchParams.ProductType);
if (searchParams.TraceabilityType.HasValue)
query = query.Where(p => p.TraceabilityType == searchParams.TraceabilityType);
if (searchParams.DivisionId.HasValue)
query = query.Where(p => p.DivisionId == searchParams.DivisionId);
if (searchParams.UnitId.HasValue)
query = query.Where(p => p.UnitId == searchParams.UnitId);
if (searchParams.PlusProcess.HasValue)
query = query.Where(p => p.PlusProcess == searchParams.PlusProcess);
var paged = await query.ToPagedResultAsync(searchParams.Page, searchParams.PageSize);
return new PagedResult<ELSProduct>
{
Items = paged.Items.Select(EntityMapper.MapEntity<PhLsmProduct, ELSProduct>),
TotalItems = paged.TotalItems,
Page = paged.Page,
PageSize = paged.PageSize
};
}
public async Task<ELSProduct?> GetByIdAsync(int id)
{
var entity = await _context.PhLsmProducts
.Include(p => p.Division)
.Include(p => p.Unit)
.FirstOrDefaultAsync(p => p.Id == id);
return entity != null ? EntityMapper.MapEntity<PhLsmProduct, ELSProduct>(entity) : null;
}
public async Task<ELSProduct> CreateAsync(ELSProduct entity)
{
var mapped = EntityMapper.MapEntity<ELSProduct, PhLsmProduct>(entity);
_context.PhLsmProducts.Add(mapped);
await _context.SaveChangesAsync();
return EntityMapper.MapEntity<PhLsmProduct, ELSProduct>(mapped);
}
public async Task<bool> UpdateAsync(ELSProduct entity)
{
var existing = await _context.PhLsmProducts.FindAsync(entity.Id);
if (existing == null) return false;
EntityMapper.MapEntityToExisting(entity, existing);
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteAsync(int id)
{
var entity = await _context.PhLsmProducts.FindAsync(id);
if (entity == null) return false;
_context.PhLsmProducts.Remove(entity);
await _context.SaveChangesAsync();
return true;
}
}
}