65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Item;
|
|
|
|
[ApiController]
|
|
[Route("api/items")]
|
|
public class ItemController(ItemService service) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("item.view")]
|
|
public async Task<IActionResult> GetAll(
|
|
[FromQuery] Guid? categoryId,
|
|
[FromQuery] Guid? subCategoryId,
|
|
[FromQuery] Guid? brandId) =>
|
|
Ok(await service.GetAllAsync(categoryId, subCategoryId, brandId));
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("item.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("item.manage")]
|
|
public async Task<IActionResult> Create(CreateItemRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return Conflict(new { error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize("item.manage")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateItemRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize("item.manage")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpPut("{id:guid}/uom-conversions")]
|
|
[Authorize("item.manage")]
|
|
public async Task<IActionResult> ReplaceConversions(Guid id, ReplaceConversionsRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.ReplaceConversionsAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
}
|