| | 1 | | using Despesas.Application.Abstractions; |
| | 2 | | using Despesas.Application.Dtos; |
| | 3 | | using Microsoft.AspNetCore.Authorization; |
| | 4 | | using Microsoft.AspNetCore.Mvc; |
| | 5 | |
|
| | 6 | | namespace Despesas.Backend.Controllers; |
| | 7 | |
|
| | 8 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | 9 | | [Authorize(Roles = "Disabled")] |
| | 10 | | public class ImagemPerfilUsuarioController : AuthController |
| | 11 | | { |
| | 12 | | private readonly IImagemPerfilUsuarioBusiness<ImagemPerfilDto, UsuarioDto> _imagemPerfilBussiness; |
| | 13 | |
|
| 13 | 14 | | public ImagemPerfilUsuarioController(IImagemPerfilUsuarioBusiness<ImagemPerfilDto, UsuarioDto> imagemPerfilBussiness |
| 13 | 15 | | { |
| 13 | 16 | | _imagemPerfilBussiness = imagemPerfilBussiness; |
| 13 | 17 | | } |
| | 18 | |
|
| | 19 | |
|
| | 20 | | [HttpGet] |
| | 21 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 22 | | [ProducesResponseType(200, Type = typeof(ImagemPerfilDto))] |
| | 23 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 24 | | [ProducesResponseType(401)] |
| | 25 | | [ProducesResponseType(403)] |
| | 26 | | public async Task<IActionResult> GetImagemPerfil() |
| 2 | 27 | | { |
| | 28 | | try |
| 2 | 29 | | { |
| | 30 | |
|
| 2 | 31 | | var result = await _imagemPerfilBussiness.FindAll(UserIdentity); |
| 13 | 32 | | var imagemPerfilUsuario = result.Find(prop => prop.UsuarioId.Equals(UserIdentity)); |
| | 33 | |
|
| 2 | 34 | | if (imagemPerfilUsuario != null) |
| 1 | 35 | | return Ok(imagemPerfilUsuario); |
| | 36 | | else |
| 1 | 37 | | return Ok(new ImagemPerfilDto()); |
| | 38 | | } |
| 0 | 39 | | catch (Exception ex) |
| 0 | 40 | | { |
| 0 | 41 | | if (ex is ArgumentException argEx) |
| 0 | 42 | | return BadRequest(argEx.Message); |
| | 43 | |
|
| 0 | 44 | | return BadRequest("Usuário não possui nenhuma imagem de perfil cadastrada!"); |
| | 45 | | } |
| 2 | 46 | | } |
| | 47 | |
|
| | 48 | | [HttpPost] |
| | 49 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 50 | | [ProducesResponseType(200, Type = typeof(ImagemPerfilDto))] |
| | 51 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 52 | | [ProducesResponseType(401)] |
| | 53 | | [ProducesResponseType(403)] |
| | 54 | | public async Task<IActionResult> PostImagemPerfil(IFormFile file) |
| 6 | 55 | | { |
| | 56 | | try |
| 6 | 57 | | { |
| 6 | 58 | | ImagemPerfilDto imagemPerfilUsuario = await ConvertFileToImagemPerfilUsuarioDtoAsync(file, UserIdentity); |
| 4 | 59 | | var _imagemPerfilUsuario = _imagemPerfilBussiness.Create(imagemPerfilUsuario); |
| | 60 | |
|
| 4 | 61 | | if (_imagemPerfilUsuario != null) |
| 3 | 62 | | return Ok(_imagemPerfilUsuario); |
| | 63 | | else |
| 1 | 64 | | throw new(); |
| | 65 | | } |
| 3 | 66 | | catch (Exception ex) |
| 3 | 67 | | { |
| 3 | 68 | | if (ex is ArgumentException argEx) |
| 1 | 69 | | return BadRequest(argEx.Message); |
| | 70 | |
|
| 2 | 71 | | return BadRequest("Erro ao incluir imagem de perfil!"); |
| | 72 | | } |
| 6 | 73 | | } |
| | 74 | |
|
| | 75 | | [HttpPut] |
| | 76 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 77 | | [ProducesResponseType(200, Type = typeof(ImagemPerfilDto))] |
| | 78 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 79 | | [ProducesResponseType(401)] |
| | 80 | | [ProducesResponseType(403)] |
| | 81 | | public async Task<IActionResult> PutImagemPerfil(IFormFile file) |
| 6 | 82 | | { |
| | 83 | | try |
| 6 | 84 | | { |
| 6 | 85 | | ImagemPerfilDto imagemPerfilUsuario = await ConvertFileToImagemPerfilUsuarioDtoAsync(file, UserIdentity); |
| 4 | 86 | | imagemPerfilUsuario = await _imagemPerfilBussiness.Update(imagemPerfilUsuario); |
| 3 | 87 | | if (imagemPerfilUsuario != null) |
| 3 | 88 | | return Ok(imagemPerfilUsuario); |
| | 89 | | else |
| 0 | 90 | | throw new(); |
| | 91 | | } |
| 3 | 92 | | catch (Exception ex) |
| 3 | 93 | | { |
| 3 | 94 | | if (ex is ArgumentException argEx) |
| 1 | 95 | | return BadRequest(argEx.Message); |
| | 96 | |
|
| 2 | 97 | | return BadRequest("Erro ao atualizar imagem de perfil!"); |
| | 98 | | } |
| 6 | 99 | | } |
| | 100 | |
|
| | 101 | | [HttpDelete] |
| | 102 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 103 | | [ProducesResponseType(200, Type = typeof(bool))] |
| | 104 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 105 | | [ProducesResponseType(401)] |
| | 106 | | [ProducesResponseType(403)] |
| | 107 | | public async Task<IActionResult> DeleteImagemPerfil() |
| 3 | 108 | | { |
| | 109 | | try |
| 3 | 110 | | { |
| 3 | 111 | | if (await _imagemPerfilBussiness.Delete(UserIdentity)) |
| 1 | 112 | | return Ok(true); |
| | 113 | | else |
| 1 | 114 | | throw new(); |
| | 115 | | } |
| 2 | 116 | | catch (Exception ex) |
| 2 | 117 | | { |
| 2 | 118 | | if (ex is ArgumentException argEx) |
| 0 | 119 | | return BadRequest(argEx.Message); |
| | 120 | |
|
| 2 | 121 | | return BadRequest("Erro ao excluir imagem do perfil!"); |
| | 122 | | } |
| 3 | 123 | | } |
| | 124 | |
|
| | 125 | | private async Task<ImagemPerfilDto> ConvertFileToImagemPerfilUsuarioDtoAsync(IFormFile file, Guid idUsuario) |
| 12 | 126 | | { |
| 12 | 127 | | string fileName = idUsuario.ToString().Replace("-", "") + "-img-perfil-" + DateTime.Now.ToString("yyyyMMddHHmmss |
| 12 | 128 | | string typeFile = ""; |
| 12 | 129 | | int posicaoUltimoPontoNoArquivo = file.FileName.LastIndexOf('.'); |
| 12 | 130 | | if (posicaoUltimoPontoNoArquivo >= 0 && posicaoUltimoPontoNoArquivo < file.FileName.Length - 1) |
| 12 | 131 | | typeFile = file.FileName.Substring(posicaoUltimoPontoNoArquivo + 1); |
| | 132 | |
|
| 12 | 133 | | if (typeFile == "jpg" || typeFile == "png" || typeFile == "jpeg") |
| 10 | 134 | | { |
| 10 | 135 | | using (var memoryStream = new MemoryStream()) |
| 10 | 136 | | { |
| 10 | 137 | | await file.CopyToAsync(memoryStream); |
| | 138 | |
|
| 10 | 139 | | ImagemPerfilDto imagemPerfilUsuario = new ImagemPerfilDto |
| 10 | 140 | | { |
| 10 | 141 | | Name = fileName, |
| 10 | 142 | | Type = typeFile, |
| 10 | 143 | | ContentType = file.ContentType, |
| 10 | 144 | | UsuarioId = UserIdentity, |
| 10 | 145 | | Arquivo = memoryStream.GetBuffer() |
| 10 | 146 | | }; |
| 8 | 147 | | return imagemPerfilUsuario; |
| | 148 | | } |
| | 149 | | } |
| | 150 | | else |
| 2 | 151 | | throw new ArgumentException("Apenas arquivos do tipo jpg, jpeg ou png são aceitos."); |
| 8 | 152 | | } |
| | 153 | | } |