| | 1 | | using Despesas.Application.Abstractions; |
| | 2 | | using Despesas.Application.Dtos; |
| | 3 | | using Despesas.GlobalException.CustomExceptions; |
| | 4 | | using Despesas.GlobalException.CustomExceptions.Core; |
| | 5 | | using Microsoft.AspNetCore.Authorization; |
| | 6 | | using Microsoft.AspNetCore.Mvc; |
| | 7 | |
|
| | 8 | | namespace Despesas.Backend.Controllers; |
| | 9 | |
|
| | 10 | | public class UsuarioController : AuthController |
| | 11 | | { |
| | 12 | | private readonly IUsuarioBusiness<UsuarioDto> _usuarioBusiness; |
| | 13 | |
|
| 12 | 14 | | public UsuarioController(IUsuarioBusiness<UsuarioDto> usuarioBusiness) |
| 12 | 15 | | { |
| 12 | 16 | | _usuarioBusiness = usuarioBusiness; |
| 12 | 17 | | } |
| | 18 | |
|
| | 19 | | [HttpGet] |
| | 20 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 21 | | [ProducesResponseType(200, Type = typeof(UsuarioDto))] |
| | 22 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 23 | | [ProducesResponseType(401)] |
| | 24 | | [ProducesResponseType(403)] |
| | 25 | | public async Task<IActionResult> Get() |
| 2 | 26 | | { |
| 2 | 27 | | var usuario = await _usuarioBusiness.FindById(UserIdentity) |
| 2 | 28 | | ?? throw new UsuarioNaoEncontradoException(); |
| 1 | 29 | | return Ok(usuario); |
| 1 | 30 | | } |
| | 31 | |
|
| | 32 | | [HttpPost] |
| | 33 | | [Authorize("Bearer", Roles = "Admin")] |
| | 34 | | [ProducesResponseType(200, Type = typeof(UsuarioDto))] |
| | 35 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 36 | | [ProducesResponseType(401)] |
| | 37 | | [ProducesResponseType(403)] |
| | 38 | | public async Task<IActionResult> Post([FromBody] UsuarioDto usuarioDto) |
| 2 | 39 | | { |
| 2 | 40 | | usuarioDto.UsuarioId = UserIdentity; |
| 2 | 41 | | usuarioDto = await _usuarioBusiness.Create(usuarioDto) |
| 2 | 42 | | ?? throw new CustomException("Erro ao cadastrar Usuário!"); |
| 1 | 43 | | return Ok(usuarioDto); |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | [HttpPut] |
| | 47 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 48 | | [ProducesResponseType(200, Type = typeof(UsuarioDto))] |
| | 49 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 50 | | [ProducesResponseType(401)] |
| | 51 | | [ProducesResponseType(403)] |
| | 52 | | public async Task<IActionResult> Put([FromBody] UsuarioDto usuarioDto) |
| 2 | 53 | | { |
| 2 | 54 | | usuarioDto.UsuarioId = UserIdentity; |
| 2 | 55 | | usuarioDto = await _usuarioBusiness.Update(usuarioDto) |
| 2 | 56 | | ?? throw new CustomException("Erro ao atualizar dados pessoais do usuário!"); |
| 1 | 57 | | return Ok(usuarioDto); |
| 1 | 58 | | } |
| | 59 | |
|
| | 60 | | [HttpDelete] |
| | 61 | | [Authorize("Bearer", Roles = "Admin")] |
| | 62 | | [ProducesResponseType(200, Type = typeof(bool))] |
| | 63 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 64 | | [ProducesResponseType(401)] |
| | 65 | | [ProducesResponseType(403)] |
| | 66 | | public async Task<IActionResult> Delete([FromBody] UsuarioDto usuarioDto) |
| 2 | 67 | | { |
| 2 | 68 | | usuarioDto.UsuarioId = UserIdentity; |
| 2 | 69 | | return await _usuarioBusiness.Delete(usuarioDto) |
| 2 | 70 | | ? Ok(true) |
| 2 | 71 | | : BadRequest("Erro ao excluir Usuário!"); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | [HttpGet("GetProfileImage")] |
| | 75 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 76 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 77 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 78 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 79 | | [ProducesResponseType(401)] |
| | 80 | | [ProducesResponseType(403)] |
| | 81 | | public async Task<IActionResult> GetProfileImage() |
| 2 | 82 | | { |
| 2 | 83 | | var image = await _usuarioBusiness.GetProfileImage(UserIdentity); |
| | 84 | |
|
| 2 | 85 | | if (image == null || image.Length == 0) |
| 1 | 86 | | return NoContent(); |
| | 87 | |
|
| 1 | 88 | | return File(image, "image/png"); |
| 2 | 89 | | } |
| | 90 | |
|
| | 91 | | [HttpPut("UpdateProfileImage")] |
| | 92 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 93 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 94 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 95 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 96 | | [ProducesResponseType(401)] |
| | 97 | | [ProducesResponseType(403)] |
| | 98 | | public async Task<IActionResult> PutProfileImage(IFormFile file) |
| 2 | 99 | | { |
| 2 | 100 | | var image = await _usuarioBusiness.UpdateProfileImage(UserIdentity, file); |
| | 101 | |
|
| 2 | 102 | | if (image == null || image.Length == 0) |
| 1 | 103 | | return NoContent(); |
| | 104 | |
|
| 1 | 105 | | return File(image, file.ContentType); |
| 2 | 106 | | } |
| | 107 | |
|
| | 108 | | } |