using Distribution.Infra.UoW; namespace Distribution.Features.Uom; public class UomService(IUomRepository repo, IUnitOfWork uow) { public async Task> GetAllAsync() => (await repo.GetAllAsync()) .Where(u => u.IsActive) .Select(u => new UomResponse(u.Id, u.Name, u.IsActive, u.CreatedAt)) .ToList(); public async Task GetByIdAsync(Guid id) { var uom = await repo.GetByIdAsync(id); if (uom is null || !uom.IsActive) return null; return new UomResponse(uom.Id, uom.Name, uom.IsActive, uom.CreatedAt); } public async Task CreateAsync(CreateUomRequest request, CancellationToken ct = default) { var uom = new Models.Uom { Name = request.Name }; await uow.BeginAsync(ct); try { await repo.AddAsync(uom); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new UomResponse(uom.Id, uom.Name, uom.IsActive, uom.CreatedAt); } public async Task UpdateAsync(Guid id, UpdateUomRequest request, CancellationToken ct = default) { var uom = await repo.GetByIdAsync(id); if (uom is null || !uom.IsActive) return null; if (uom.ExternalErpId.HasValue) return null; uom.Name = request.Name; await uow.BeginAsync(ct); try { repo.Update(uom); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return new UomResponse(uom.Id, uom.Name, uom.IsActive, uom.CreatedAt); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var uom = await repo.GetByIdAsync(id); if (uom is null || !uom.IsActive) return false; if (uom.ExternalErpId.HasValue) return false; if (await repo.IsUsedByItemsAsync(id)) return false; await uow.BeginAsync(ct); try { repo.Remove(uom); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return true; } }