53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.Uom;
|
|
|
|
public class UomService(IUomRepository repo, IUnitOfWork uow)
|
|
{
|
|
public async Task<List<UomResponse>> GetAllAsync() =>
|
|
(await repo.GetAllAsync())
|
|
.Where(u => u.IsActive)
|
|
.Select(u => new UomResponse(u.Id, u.Name, u.IsActive, u.CreatedAt))
|
|
.ToList();
|
|
|
|
public async Task<UomResponse?> 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<UomResponse> 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<UomResponse?> 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<bool> 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;
|
|
}
|
|
}
|