| | 1 | | using Despesas.Application.Abstractions; |
| | 2 | | using Despesas.Application.Dtos; |
| | 3 | | using Despesas.Application.Dtos.Core; |
| | 4 | | using Despesas.GlobalException.CustomExceptions.Core; |
| | 5 | | using Domain.Entities; |
| | 6 | | using Microsoft.AspNetCore.Authorization; |
| | 7 | | using Microsoft.AspNetCore.Mvc; |
| | 8 | |
|
| | 9 | | namespace Despesas.Backend.Controllers; |
| | 10 | |
|
| | 11 | | public class CategoriaController : AuthController |
| | 12 | | { |
| | 13 | | private readonly ICategoriaBusiness<CategoriaDto, Categoria> _categoriaBusiness; |
| 28 | 14 | | public CategoriaController(ICategoriaBusiness<CategoriaDto, Categoria> categoriaBusiness) |
| 28 | 15 | | { |
| 28 | 16 | | _categoriaBusiness = categoriaBusiness; |
| 28 | 17 | | } |
| | 18 | |
|
| | 19 | | [HttpGet] |
| | 20 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 21 | | [ProducesResponseType(200, Type = typeof(List<CategoriaDto>))] |
| | 22 | | [ProducesResponseType(401)] |
| | 23 | | [ProducesResponseType(403)] |
| | 24 | | public async Task<IActionResult> Get() |
| 1 | 25 | | { |
| 1 | 26 | | var categoria = await _categoriaBusiness.FindAll(UserIdentity); |
| 1 | 27 | | return Ok(categoria); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | [HttpGet("GetById/{idCategoria}")] |
| | 31 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 32 | | [ProducesResponseType(200, Type = typeof(CategoriaDto))] |
| | 33 | | [ProducesResponseType(401)] |
| | 34 | | [ProducesResponseType(403)] |
| | 35 | | public async Task<IActionResult> GetById([FromRoute] Guid idCategoria) |
| 2 | 36 | | { |
| 2 | 37 | | var ccategoria = await _categoriaBusiness.FindById(idCategoria, UserIdentity); |
| 2 | 38 | | return Ok(ccategoria); |
| 2 | 39 | | } |
| | 40 | |
|
| | 41 | | [HttpGet("GetByTipoCategoria/{tipoCategoria}")] |
| | 42 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 43 | | [ProducesResponseType(200, Type = typeof(List<CategoriaDto>))] |
| | 44 | | [ProducesResponseType(401)] |
| | 45 | | [ProducesResponseType(403)] |
| | 46 | | public async Task<IActionResult> GetByTipoCategoria([FromRoute] TipoCategoriaDto tipoCategoria) |
| 2 | 47 | | { |
| 2 | 48 | | var _categoria = await _categoriaBusiness.FindByTipocategoria(UserIdentity, (int)tipoCategoria); |
| 2 | 49 | | return Ok(_categoria); |
| 2 | 50 | | } |
| | 51 | |
|
| | 52 | | [HttpPost] |
| | 53 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 54 | | [ProducesResponseType(200, Type = typeof(CategoriaDto))] |
| | 55 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 56 | | [ProducesResponseType(401)] |
| | 57 | | [ProducesResponseType(403)] public async Task<IActionResult> Post([FromBody] CategoriaDto categoria) |
| 3 | 58 | | { |
| 3 | 59 | | if (categoria.IdTipoCategoria == (int)TipoCategoriaDto.Todas) |
| 1 | 60 | | return BadRequest("Nenhum tipo de Categoria foi selecionado!"); |
| | 61 | |
|
| 2 | 62 | | categoria.UsuarioId = UserIdentity; |
| 2 | 63 | | categoria = await _categoriaBusiness.Create(categoria) |
| 2 | 64 | | ?? throw new CustomException("Não foi possível realizar o cadastro de uma nova categoria, tente mais tarde o |
| 1 | 65 | | return Ok(categoria); |
| 2 | 66 | | } |
| | 67 | |
|
| | 68 | | [HttpPut] |
| | 69 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 70 | | [ProducesResponseType(200, Type = typeof(CategoriaDto))] |
| | 71 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 72 | | [ProducesResponseType(401)] |
| | 73 | | [ProducesResponseType(403)] |
| | 74 | | public async Task<IActionResult> Put([FromBody] CategoriaDto categoria) |
| 3 | 75 | | { |
| 3 | 76 | | if (categoria.IdTipoCategoria == TipoCategoriaDto.Todas) |
| 1 | 77 | | return BadRequest("Nenhum tipo de Categoria foi selecionado!"); |
| | 78 | |
|
| 2 | 79 | | categoria.UsuarioId = UserIdentity; |
| 2 | 80 | | categoria = await _categoriaBusiness.Update(categoria) |
| 2 | 81 | | ?? throw new CustomException("Erro ao atualizar categoria!"); |
| 1 | 82 | | return Ok(categoria); |
| 2 | 83 | | } |
| | 84 | |
|
| | 85 | | [HttpDelete("{idCategoria}")] |
| | 86 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 87 | | [ProducesResponseType(200, Type = typeof(bool))] |
| | 88 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 89 | | [ProducesResponseType(401)] |
| | 90 | | [ProducesResponseType(403)] |
| | 91 | | public async Task<IActionResult> Delete(Guid idCategoria) |
| 3 | 92 | | { |
| 3 | 93 | | var categoria = new CategoriaDto() |
| 3 | 94 | | { |
| 3 | 95 | | Id = idCategoria, |
| 3 | 96 | | UsuarioId = UserIdentity |
| 3 | 97 | | }; |
| 3 | 98 | | var result = await _categoriaBusiness.Delete(categoria); |
| 3 | 99 | | if (result) |
| 1 | 100 | | return Ok(result); |
| | 101 | | else |
| 2 | 102 | | return BadRequest("Erro ao deletar categoria!"); |
| 3 | 103 | | } |
| | 104 | | } |