| | 1 | | using Despesas.Application.Abstractions; |
| | 2 | | using Despesas.Application.Dtos; |
| | 3 | | using Despesas.GlobalException.CustomExceptions.Core; |
| | 4 | | using Domain.Entities; |
| | 5 | | using Microsoft.AspNetCore.Authorization; |
| | 6 | | using Microsoft.AspNetCore.Mvc; |
| | 7 | |
|
| | 8 | | namespace Despesas.Backend.Controllers; |
| | 9 | | public class DespesaController : AuthController |
| | 10 | | { |
| | 11 | | private readonly IBusinessBase<DespesaDto, Despesa> _despesaBusiness; |
| 11 | 12 | | public DespesaController(IBusinessBase<DespesaDto, Despesa> despesaBusiness) |
| 11 | 13 | | { |
| 11 | 14 | | _despesaBusiness = despesaBusiness; |
| 11 | 15 | | } |
| | 16 | |
|
| | 17 | | [HttpGet] |
| | 18 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 19 | | [ProducesResponseType(200, Type = typeof(IList<DespesaDto>))] |
| | 20 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 21 | | [ProducesResponseType(401)] |
| | 22 | | [ProducesResponseType(403)] |
| | 23 | | public async Task<IActionResult> Get() |
| 2 | 24 | | { |
| 2 | 25 | | return Ok(await _despesaBusiness.FindAll(UserIdentity)); |
| 2 | 26 | | } |
| | 27 | |
|
| | 28 | | [HttpGet("GetById/{id}")] |
| | 29 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 30 | | [ProducesResponseType(200, Type = typeof(DespesaDto))] |
| | 31 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 32 | | [ProducesResponseType(401)] |
| | 33 | | [ProducesResponseType(403)] |
| | 34 | | public async Task<IActionResult> Get([FromRoute] Guid id) |
| 2 | 35 | | { |
| 2 | 36 | | var despesa = await _despesaBusiness.FindById(id, UserIdentity) |
| 2 | 37 | | ?? throw new CustomException("Nenhuma despesa foi encontrada."); |
| 1 | 38 | | return Ok(despesa); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | [HttpPost] |
| | 42 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 43 | | [ProducesResponseType(200, Type = typeof(DespesaDto))] |
| | 44 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 45 | | [ProducesResponseType(401)] |
| | 46 | | [ProducesResponseType(403)] |
| | 47 | | public async Task<IActionResult> Post([FromBody] DespesaDto despesa) |
| 2 | 48 | | { |
| 2 | 49 | | despesa.UsuarioId = UserIdentity; |
| 2 | 50 | | despesa = await _despesaBusiness.Create(despesa) |
| 2 | 51 | | ?? throw new CustomException("Não foi possível realizar o cadastro da despesa."); |
| 1 | 52 | | return Ok(despesa); |
| 1 | 53 | | } |
| | 54 | |
|
| | 55 | | [HttpPut] |
| | 56 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 57 | | [ProducesResponseType(200, Type = typeof(DespesaDto))] |
| | 58 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 59 | | [ProducesResponseType(401)] |
| | 60 | | [ProducesResponseType(403)] |
| | 61 | | public async Task<IActionResult> Put([FromBody] DespesaDto despesa) |
| 2 | 62 | | { |
| 2 | 63 | | despesa.UsuarioId = UserIdentity; |
| 2 | 64 | | despesa = await _despesaBusiness.Update(despesa) |
| 2 | 65 | | ?? throw new CustomException("Não foi possível atualizar o cadastro da despesa."); |
| 1 | 66 | | return Ok(despesa); |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | [HttpDelete("{idDespesa}")] |
| | 70 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 71 | | [ProducesResponseType(200, Type = typeof(bool))] |
| | 72 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 73 | | [ProducesResponseType(401)] |
| | 74 | | [ProducesResponseType(403)] |
| | 75 | | public async Task<IActionResult> Delete(Guid idDespesa) |
| 3 | 76 | | { |
| 3 | 77 | | var despesa = new DespesaDto |
| 3 | 78 | | { |
| 3 | 79 | | Id = idDespesa, |
| 3 | 80 | | UsuarioId = UserIdentity |
| 3 | 81 | | }; |
| | 82 | |
|
| 3 | 83 | | return await _despesaBusiness.Delete(despesa) ? Ok(true) : BadRequest("Erro ao excluir Despesa!"); |
| 3 | 84 | | } |
| | 85 | | } |