using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Distribution.Features.PriceList; [ApiController] [Route("api/price-lists")] [Authorize] public class PriceListController(PriceListService service) : ControllerBase { [HttpGet] public async Task GetAll([FromQuery] Guid? regionId) => Ok(await service.GetAllAsync(regionId)); [HttpGet("{id:guid}")] public async Task GetById(Guid id) { var result = await service.GetByIdAsync(id); return result is null ? NotFound() : Ok(result); } [HttpPost] public async Task Create(CreatePriceListRequest request, CancellationToken ct) { var result = await service.CreateAsync(request, ct); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); } [HttpPut("{id:guid}")] public async Task Update(Guid id, UpdatePriceListRequest request, CancellationToken ct) { try { var result = await service.UpdateAsync(id, request, ct); return result is null ? NotFound() : Ok(result); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } } [HttpDelete("{id:guid}")] public async Task Delete(Guid id, CancellationToken ct) { var deleted = await service.DeleteAsync(id, ct); return deleted ? NoContent() : NotFound(); } [HttpPut("{id:guid}/items/{sku}")] public async Task UpsertItem(Guid id, string sku, UpsertPriceListItemRequest request, CancellationToken ct) { try { var result = await service.UpsertItemAsync(id, sku, request, ct); return result is null ? NotFound() : Ok(result); } catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); } } [HttpDelete("{id:guid}/items/{sku}")] public async Task RemoveItem(Guid id, string sku, CancellationToken ct) { var result = await service.RemoveItemAsync(id, sku, ct); return result is null ? NotFound() : Ok(result); } }