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