using Distribution.Infra.UoW; namespace Distribution.Features.Supplier; public class SupplierService(ISupplierRepository repo, IUnitOfWork uow) { private static SupplierResponse Map(Models.Supplier s) => new(s.Id, s.Name, s.ContactInfo, s.PerformanceScore, s.IsActive, s.CreatedAt); public async Task> GetAllAsync() => (await repo.GetAllAsync()).Select(Map).ToList(); public async Task GetByIdAsync(Guid id) { var supplier = await repo.GetByIdAsync(id); if (supplier is null || !supplier.IsActive) return null; return Map(supplier); } public async Task CreateAsync(CreateSupplierRequest request, CancellationToken ct = default) { var supplier = new Models.Supplier { Name = request.Name, ContactInfo = request.ContactInfo }; await uow.BeginAsync(ct); try { await repo.AddAsync(supplier); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return Map(supplier); } public async Task UpdateAsync(Guid id, UpdateSupplierRequest request, CancellationToken ct = default) { var supplier = await repo.GetByIdAsync(id); if (supplier is null || !supplier.IsActive) return null; supplier.Name = request.Name; supplier.ContactInfo = request.ContactInfo; await uow.BeginAsync(ct); try { repo.Update(supplier); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return Map(supplier); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var supplier = await repo.GetByIdAsync(id); if (supplier is null || !supplier.IsActive) return false; await uow.BeginAsync(ct); try { repo.Remove(supplier); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return true; } }