phronCare/Models/Repositories/Stock/PhLSMUnitOfMeasureRepository.cs

96 lines
3.4 KiB
C#
Raw Permalink Normal View History

2025-07-14 16:16:05 -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 PhLSMUnitOfMeasureRepository(PhronCareOperationsHubContext context) : IPhLSMUnitOfMeasureRepository
{
private readonly PhronCareOperationsHubContext _context = context;
public async Task<PagedResult<ELSUnitOfMeasure>> GetAllAsync(int page = 1, int pageSize = 100)
{
var query = _context.PhLsmUnitOfMeasures.AsQueryable();
var paged = await query.ToPagedResultAsync(page, pageSize);
return new PagedResult<ELSUnitOfMeasure>
{
Items = paged.Items.Select(EntityMapper.MapEntity<PhLsmUnitOfMeasure, ELSUnitOfMeasure>),
TotalItems = paged.TotalItems,
Page = paged.Page,
PageSize = paged.PageSize
};
}
2025-07-15 12:42:30 -03:00
public async Task<ELSUnitOfMeasure?> GetByIdAsync(int id)
{
var entity = await _context.PhLsmUnitOfMeasures.FindAsync(id);
return entity != null
? EntityMapper.MapEntity<PhLsmUnitOfMeasure, ELSUnitOfMeasure>(entity)
: null;
}
2025-07-14 16:16:05 -03:00
public async Task<bool> ExistsByCodeAsync(string code)
{
if (string.IsNullOrWhiteSpace(code))
return false;
return await _context.PhLsmUnitOfMeasures
.AnyAsync(x => x.Code.ToLower() == code.ToLower());
}
public async Task<List<string>> GetAllCodesAsync()
{
return await _context.PhLsmUnitOfMeasures
.Select(x => x.Code)
.ToListAsync();
}
2025-07-15 12:42:30 -03:00
public async Task<int> AddAsync(ELSUnitOfMeasure model)
{
var entity = new PhLsmUnitOfMeasure
{
Name = model.Name,
Description = model.Description,
Code = model.Code // si lo tenés
};
_context.PhLsmUnitOfMeasures.Add(entity);
await _context.SaveChangesAsync();
return entity.Id;
}
public async Task UpdateAsync(ELSUnitOfMeasure model)
{
var entity = await _context.PhLsmUnitOfMeasures.FindAsync(model.Id);
if (entity == null) throw new Exception("Unidad no encontrada");
entity.Name = model.Name;
entity.Description = model.Description;
entity.Code = model.Code;
await _context.SaveChangesAsync();
}
public async Task<PagedResult<ELSUnitOfMeasure>> SearchAsync(string? text, int page = 1, int pageSize = 100)
{
var query = _context.PhLsmUnitOfMeasures.AsQueryable();
if (!string.IsNullOrWhiteSpace(text))
{
var lowered = text.ToLower();
query = query.Where(x =>
x.Name.ToLower().Contains(lowered) ||
x.Description.ToLower().Contains(lowered));
}
var paged = await query.ToPagedResultAsync(page, pageSize);
return new PagedResult<ELSUnitOfMeasure>
{
Items = paged.Items.Select(EntityMapper.MapEntity<PhLsmUnitOfMeasure, ELSUnitOfMeasure>),
TotalItems = paged.TotalItems,
Page = paged.Page,
PageSize = paged.PageSize
};
}
2025-07-14 16:16:05 -03:00
}
}