104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.PriceList;
|
|
|
|
[ApiController]
|
|
[Route("api/price-lists")]
|
|
public class PriceListController(
|
|
PriceListService service,
|
|
IScopeAccessService scopeAccess,
|
|
ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("price-list.view")]
|
|
public async Task<IActionResult> GetAll([FromQuery] Guid? regionId)
|
|
{
|
|
if (regionId.HasValue)
|
|
{
|
|
var userId = currentUser.UserId;
|
|
if (userId.HasValue && !await scopeAccess.HasRegionScopeAsync(userId.Value, regionId.Value))
|
|
return StatusCode(403);
|
|
}
|
|
return Ok(await service.GetAllAsync(regionId));
|
|
}
|
|
|
|
[HttpGet("resolve")]
|
|
[Authorize("price-list.view")]
|
|
public async Task<IActionResult> Resolve([FromQuery] Guid itemId, [FromQuery] Guid storeId, [FromQuery] DateTime saleDate)
|
|
{
|
|
var result = await service.ResolveByStoreAsync(itemId, storeId, saleDate);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("price-list.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("price-list.manage")]
|
|
public async Task<IActionResult> Create(CreatePriceListRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize("price-list.manage")]
|
|
public async Task<IActionResult> 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}")]
|
|
[Authorize("price-list.manage")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpPut("{id:guid}/items/{sku}")]
|
|
[Authorize("price-list.manage")]
|
|
public async Task<IActionResult> 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}")]
|
|
[Authorize("price-list.manage")]
|
|
public async Task<IActionResult> RemoveItem(Guid id, string sku, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.RemoveItemAsync(id, sku, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return Conflict(new { error = ex.Message });
|
|
}
|
|
}
|
|
}
|