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

namespace SCM.Api.Controllers;

/// <summary>Cadastro-base de obras.</summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/obras")]
[Authorize]
[Produces("application/json")]
public sealed class ObrasController : ControllerBase
{
    private readonly IObraService _service;

    public ObrasController(IObraService service) => _service = service;

    /// <summary>Lista todas as obras (ordenadas por código).</summary>
    [HttpGet]
    [ProducesResponseType(typeof(IReadOnlyList<ObraResponse>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IReadOnlyList<ObraResponse>>> GetAll(CancellationToken ct = default)
        => Ok(await _service.GetAllAsync(ct));

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

    /// <summary>Cria uma obra.</summary>
    [HttpPost]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
    [ProducesResponseType(typeof(ObraResponse), StatusCodes.Status201Created)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
    public async Task<ActionResult<ObraResponse>> Create(
        [FromBody] CreateObraRequest 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 obra.</summary>
    [HttpPut("{id:guid}")]
    [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
    [ProducesResponseType(typeof(ObraResponse), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
    public async Task<ActionResult<ObraResponse>> Update(
        Guid id, [FromBody] UpdateObraRequest request, CancellationToken ct = default)
        => Ok(await _service.UpdateAsync(id, request, ct));

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