File size: 3,704 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
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Materiais;
using SCM.Application.Materiais.Dtos;
using SCM.Shared.Api;
using SCM.Shared.Constants;

namespace SCM.Api.Controllers;

/// <summary>Delivery Orders (MSP) — recebimento físico de itens de material, por obra.</summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/delivery-orders")]
[Authorize]
[Produces("application/json")]
public sealed class DeliveryOrdersController : ControllerBase
{
    private readonly IDeliveryOrderService _service;

    public DeliveryOrdersController(IDeliveryOrderService service) => _service = service;

    /// <summary>Lista as DOs de uma obra (filtros opcionais: notaFiscal LIKE, tipo macro).</summary>
    [HttpGet]
    [ProducesResponseType(typeof(IReadOnlyList<DeliveryOrderResponse>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IReadOnlyList<DeliveryOrderResponse>>> GetByObra(
        [FromQuery] Guid obraId, [FromQuery] string? notaFiscal = null,
        [FromQuery] Guid? tipoMaterialMacroId = null, CancellationToken ct = default)
        => Ok(await _service.GetByObraAsync(obraId, notaFiscal, tipoMaterialMacroId, ct));

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

    /// <summary>Cria uma DO (par nota fiscal + item único por obra; tipo macro precisa de agrupador).</summary>
    [HttpPost]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor + "," + Roles.Engenheiro)]
    [ProducesResponseType(typeof(DeliveryOrderResponse), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
    public async Task<ActionResult<DeliveryOrderResponse>> Create(
        [FromBody] CreateDeliveryOrderRequest 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 uma DO (a obra é imutável).</summary>
    [HttpPut("{id:guid}")]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor + "," + Roles.Engenheiro)]
    [ProducesResponseType(typeof(DeliveryOrderResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
    public async Task<ActionResult<DeliveryOrderResponse>> Update(
        Guid id, [FromBody] UpdateDeliveryOrderRequest request, CancellationToken ct = default)
        => Ok(await _service.UpdateAsync(id, request, ct));

    /// <summary>Exclui uma DO.</summary>
    [HttpDelete("{id:guid}")]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
    [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();
    }
}