File size: 4,116 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Cadastros;
using SCM.Application.Cadastros.Dtos;
using SCM.Shared.Api;
using SCM.Shared.Constants;

namespace SCM.Api.Controllers;

/// <summary>Conversões entre unidades de medida (par direcional com fator).</summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/conversoes-unidade-medida")]
[Authorize]
[Produces("application/json")]
public sealed class ConversoesUnidadeMedidaController : ControllerBase
{
    private readonly IConversaoUnidadeMedidaService _service;

    public ConversoesUnidadeMedidaController(IConversaoUnidadeMedidaService service) => _service = service;

    /// <summary>Lista todas as conversões (com os códigos das unidades).</summary>
    [HttpGet]
    [ProducesResponseType(typeof(IReadOnlyList<ConversaoUnidadeMedidaResponse>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IReadOnlyList<ConversaoUnidadeMedidaResponse>>> GetAll(CancellationToken ct = default)
        => Ok(await _service.GetAllAsync(ct));

    /// <summary>Obtém uma conversão pelo identificador.</summary>
    [HttpGet("{id:guid}")]
    [ProducesResponseType(typeof(ConversaoUnidadeMedidaResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ConversaoUnidadeMedidaResponse>> GetById(Guid id, CancellationToken ct = default)
        => Ok(await _service.GetByIdAsync(id, ct));

    /// <summary>Converte um valor entre duas unidades (404 se a conversão não estiver cadastrada).</summary>
    [HttpGet("converter")]
    [ProducesResponseType(typeof(ConversaoValorResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ConversaoValorResponse>> Converter(
        [FromQuery] Guid unidadeDeId, [FromQuery] Guid unidadeParaId, [FromQuery] decimal valor,
        CancellationToken ct = default)
        => Ok(await _service.ConverterAsync(unidadeDeId, unidadeParaId, valor, ct));

    /// <summary>Cria uma conversão (opcionalmente também a direção inversa, 1/fator).</summary>
    [HttpPost]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
    [ProducesResponseType(typeof(ConversaoUnidadeMedidaResponse), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
    public async Task<ActionResult<ConversaoUnidadeMedidaResponse>> Create(
        [FromBody] CreateConversaoUnidadeMedidaRequest request, CancellationToken ct = default)
    {
        var created = await _service.CreateAsync(request, ct);
        return CreatedAtAction(nameof(GetById), new { id = created.Id, version = "1.0" }, created);
    }

    /// <summary>Altera o fator de uma conversão (o par de unidades é imutável).</summary>
    [HttpPut("{id:guid}")]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
    [ProducesResponseType(typeof(ConversaoUnidadeMedidaResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ConversaoUnidadeMedidaResponse>> Update(
        Guid id, [FromBody] UpdateConversaoUnidadeMedidaRequest request, CancellationToken ct = default)
        => Ok(await _service.UpdateAsync(id, request, ct));

    /// <summary>Exclui uma conversão.</summary>
    [HttpDelete("{id:guid}")]
    [Authorize(Roles = Roles.Administrador)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> Delete(Guid id, CancellationToken ct = default)
    {
        await _service.DeleteAsync(id, ct);
        return NoContent();
    }
}