item mng with sync
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
namespace Distribution.Features.Uom;
|
||||
|
||||
public interface IUomRepository
|
||||
{
|
||||
Task<Models.Uom?> GetByIdAsync(Guid id);
|
||||
Task<List<Models.Uom>> GetAllAsync();
|
||||
Task<Models.Uom?> GetByExternalErpIdAsync(int erpId);
|
||||
Task<bool> IsUsedByItemsAsync(Guid id);
|
||||
Task AddAsync(Models.Uom entity);
|
||||
void Update(Models.Uom entity);
|
||||
void Remove(Models.Uom entity);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Distribution.Features.Uom;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/uoms")]
|
||||
[Authorize]
|
||||
public class UomController(UomService service) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[Authorize(Policy = "items.view")]
|
||||
public async Task<IActionResult> GetAll() =>
|
||||
Ok(await service.GetAllAsync());
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
[Authorize(Policy = "items.view")]
|
||||
public async Task<IActionResult> GetById(Guid id)
|
||||
{
|
||||
var result = await service.GetByIdAsync(id);
|
||||
return result is null ? NotFound() : Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = "items.create")]
|
||||
public async Task<IActionResult> Create(CreateUomRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await service.CreateAsync(request, ct);
|
||||
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
[Authorize(Policy = "items.update")]
|
||||
public async Task<IActionResult> Update(Guid id, UpdateUomRequest request, CancellationToken ct)
|
||||
{
|
||||
var result = await service.UpdateAsync(id, request, ct);
|
||||
return result is null ? NotFound() : Ok(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize(Policy = "items.delete")]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||||
{
|
||||
var deleted = await service.DeleteAsync(id, ct);
|
||||
return deleted ? NoContent() : NotFound();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Distribution.Features.Uom;
|
||||
|
||||
public record CreateUomRequest(string Name);
|
||||
public record UpdateUomRequest(string Name);
|
||||
public record UomResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
|
||||
@@ -0,0 +1,32 @@
|
||||
using Distribution.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Distribution.Features.Uom;
|
||||
|
||||
public class UomRepository(AppDBContext context) : IUomRepository
|
||||
{
|
||||
public async Task<Models.Uom?> GetByIdAsync(Guid id) =>
|
||||
await context.Uoms.FindAsync(id);
|
||||
|
||||
public async Task<List<Models.Uom>> GetAllAsync() =>
|
||||
await context.Uoms.ToListAsync();
|
||||
|
||||
public async Task<Models.Uom?> GetByExternalErpIdAsync(int erpId) =>
|
||||
await context.Uoms.FirstOrDefaultAsync(u => u.ExternalErpId == erpId);
|
||||
|
||||
public async Task<bool> IsUsedByItemsAsync(Guid id) =>
|
||||
await context.Items.AnyAsync(i => i.FK_BaseUomId == id && i.IsActive) ||
|
||||
await context.UomConversions.AnyAsync(c => c.FK_FromUomId == id || c.FK_ToUomId == id);
|
||||
|
||||
public async Task AddAsync(Models.Uom entity) =>
|
||||
await context.Uoms.AddAsync(entity);
|
||||
|
||||
public void Update(Models.Uom entity) =>
|
||||
context.Uoms.Update(entity);
|
||||
|
||||
public void Remove(Models.Uom entity)
|
||||
{
|
||||
entity.IsActive = false;
|
||||
context.Uoms.Update(entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user