using Distribution.Data; using Distribution.Models; using Microsoft.EntityFrameworkCore; namespace Distribution.Features.Po; public class PoRepository(AppDBContext context) : IPoRepository { public async Task GetByIdAsync(Guid id) => await context.PurchaseOrders .Include(po => po.Supplier) .Include(po => po.Items).ThenInclude(i => i.Item) .FirstOrDefaultAsync(po => po.Id == id); public async Task> GetAllAsync(Guid? supplierId, PurchaseOrderStatus? status) { var query = context.PurchaseOrders .Include(po => po.Supplier) .Include(po => po.Items).ThenInclude(i => i.Item) .Where(po => po.IsActive) .AsQueryable(); if (supplierId.HasValue) query = query.Where(po => po.FK_SupplierId == supplierId.Value); if (status.HasValue) query = query.Where(po => po.Status == status.Value); return await query.OrderByDescending(po => po.CreatedAt).ToListAsync(); } public async Task AddAsync(Models.PurchaseOrder entity) => await context.PurchaseOrders.AddAsync(entity); public void Update(Models.PurchaseOrder entity) => context.PurchaseOrders.Update(entity); public async Task AddItemAsync(PurchaseOrderItem entity) => await context.PurchaseOrderItems.AddAsync(entity); public async Task HasActiveForSupplierAsync(Guid supplierId) => await context.PurchaseOrders.AnyAsync(po => po.FK_SupplierId == supplierId && po.IsActive && po.Status != PurchaseOrderStatus.Cancelled); public async Task> GetBySupplierAsync(Guid supplierId) => await context.PurchaseOrders .Include(po => po.Items) .Where(po => po.FK_SupplierId == supplierId && po.IsActive) .ToListAsync(); }