Application layer: - PagedRequest/PagedResult<T> with sane defaults (pageSize 50, max 500) - CatalogDtos: read DTOs with joined names + input DTOs for upsert - Product input supports nested Prices[] and Barcodes[] for atomic save API controllers (api/catalog/…): - countries, currencies (global, write requires SuperAdmin) - vat-rates, units-of-measure, price-types, stores (write: Admin/Manager) - retail-points (references Store, Admin/Manager write) - product-groups: hierarchy with auto-computed Path, delete guarded against children/products - counterparties: filter by kind (Supplier/Customer/Both), full join with Country - products: includes joined lookups, filter by group/isService/isWeighed/isActive, search by name/article/barcode, write replaces Prices+Barcodes atomically Role semantics: - SuperAdmin: mutates global references only - Admin: mutates/deletes tenant references - Manager: mutates tenant references (no delete on some) - Storekeeper: can manage counterparties and products (but not delete) All endpoints guarded by [Authorize]. Multi-tenant isolation via EF query filter. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
99 lines
3.8 KiB
C#
99 lines
3.8 KiB
C#
using foodmarket.Application.Catalog;
|
|
using foodmarket.Application.Common;
|
|
using foodmarket.Domain.Catalog;
|
|
using foodmarket.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace foodmarket.Api.Controllers.Catalog;
|
|
|
|
[ApiController]
|
|
[Authorize]
|
|
[Route("api/catalog/units-of-measure")]
|
|
public class UnitsOfMeasureController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _db;
|
|
|
|
public UnitsOfMeasureController(AppDbContext db) => _db = db;
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<PagedResult<UnitOfMeasureDto>>> List([FromQuery] PagedRequest req, CancellationToken ct)
|
|
{
|
|
var q = _db.UnitsOfMeasure.AsNoTracking().AsQueryable();
|
|
if (!string.IsNullOrWhiteSpace(req.Search))
|
|
{
|
|
var s = req.Search.Trim().ToLower();
|
|
q = q.Where(u => u.Name.ToLower().Contains(s) || u.Symbol.ToLower().Contains(s) || u.Code.ToLower().Contains(s));
|
|
}
|
|
var total = await q.CountAsync(ct);
|
|
var items = await q
|
|
.OrderByDescending(u => u.IsBase).ThenBy(u => u.Name)
|
|
.Skip(req.Skip).Take(req.Take)
|
|
.Select(u => new UnitOfMeasureDto(u.Id, u.Code, u.Symbol, u.Name, u.DecimalPlaces, u.IsBase, u.IsActive))
|
|
.ToListAsync(ct);
|
|
return new PagedResult<UnitOfMeasureDto> { Items = items, Total = total, Page = req.Page, PageSize = req.Take };
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<UnitOfMeasureDto>> Get(Guid id, CancellationToken ct)
|
|
{
|
|
var u = await _db.UnitsOfMeasure.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
return u is null ? NotFound() : new UnitOfMeasureDto(u.Id, u.Code, u.Symbol, u.Name, u.DecimalPlaces, u.IsBase, u.IsActive);
|
|
}
|
|
|
|
[HttpPost, Authorize(Roles = "Admin,Manager")]
|
|
public async Task<ActionResult<UnitOfMeasureDto>> Create([FromBody] UnitOfMeasureInput input, CancellationToken ct)
|
|
{
|
|
if (input.IsBase)
|
|
{
|
|
await _db.UnitsOfMeasure.Where(u => u.IsBase).ExecuteUpdateAsync(s => s.SetProperty(u => u.IsBase, false), ct);
|
|
}
|
|
|
|
var e = new UnitOfMeasure
|
|
{
|
|
Code = input.Code,
|
|
Symbol = input.Symbol,
|
|
Name = input.Name,
|
|
DecimalPlaces = input.DecimalPlaces,
|
|
IsBase = input.IsBase,
|
|
IsActive = input.IsActive,
|
|
};
|
|
_db.UnitsOfMeasure.Add(e);
|
|
await _db.SaveChangesAsync(ct);
|
|
return CreatedAtAction(nameof(Get), new { id = e.Id },
|
|
new UnitOfMeasureDto(e.Id, e.Code, e.Symbol, e.Name, e.DecimalPlaces, e.IsBase, e.IsActive));
|
|
}
|
|
|
|
[HttpPut("{id:guid}"), Authorize(Roles = "Admin,Manager")]
|
|
public async Task<IActionResult> Update(Guid id, [FromBody] UnitOfMeasureInput input, CancellationToken ct)
|
|
{
|
|
var e = await _db.UnitsOfMeasure.FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
if (e is null) return NotFound();
|
|
|
|
if (input.IsBase && !e.IsBase)
|
|
{
|
|
await _db.UnitsOfMeasure.Where(u => u.IsBase && u.Id != id).ExecuteUpdateAsync(s => s.SetProperty(u => u.IsBase, false), ct);
|
|
}
|
|
|
|
e.Code = input.Code;
|
|
e.Symbol = input.Symbol;
|
|
e.Name = input.Name;
|
|
e.DecimalPlaces = input.DecimalPlaces;
|
|
e.IsBase = input.IsBase;
|
|
e.IsActive = input.IsActive;
|
|
await _db.SaveChangesAsync(ct);
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id:guid}"), Authorize(Roles = "Admin")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var e = await _db.UnitsOfMeasure.FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
if (e is null) return NotFound();
|
|
_db.UnitsOfMeasure.Remove(e);
|
|
await _db.SaveChangesAsync(ct);
|
|
return NoContent();
|
|
}
|
|
}
|