44using System ;
55using Microsoft . AspNetCore . Http ;
66using BrnEventos . Application . Dtos ;
7+ using System . IO ;
8+ using Microsoft . AspNetCore . Hosting ;
9+ using System . Linq ;
710
811namespace BrnEventos . API . Controllers
912{
@@ -12,10 +15,12 @@ namespace BrnEventos.API.Controllers
1215 public class EventosController : ControllerBase
1316 {
1417 private readonly IEventoService _eventoService ;
18+ private readonly IWebHostEnvironment _hostEnvironment ;
1519
16- public EventosController ( IEventoService eventoService )
20+ public EventosController ( IEventoService eventoService , IWebHostEnvironment hostEnvironment )
1721 {
1822 _eventoService = eventoService ;
23+ _hostEnvironment = hostEnvironment ;
1924 }
2025
2126 [ HttpGet ]
@@ -68,6 +73,31 @@ public async Task<IActionResult> GetByTema(string tema)
6873 }
6974 }
7075
76+ [ HttpPost ( "upload-image/{eventoId}" ) ]
77+ public async Task < IActionResult > UploadImage ( int eventoId )
78+ {
79+ try
80+ {
81+ var evento = await _eventoService . GetEventoByIdAsync ( eventoId , true ) ;
82+ if ( evento == null ) return NoContent ( ) ;
83+
84+ var file = Request . Form . Files [ 0 ] ;
85+ if ( file . Length > 0 )
86+ {
87+ DeleteImage ( evento . ImagemURL ) ;
88+ evento . ImagemURL = await SaveImage ( file ) ;
89+ }
90+ var EventoRetorno = await _eventoService . UpdateEvento ( eventoId , evento ) ;
91+
92+ return Ok ( EventoRetorno ) ;
93+ }
94+ catch ( Exception ex )
95+ {
96+ return this . StatusCode ( StatusCodes . Status500InternalServerError ,
97+ $ "Erro ao tentar adicionar a IMAGEM! Erro: { ex . Message } ") ;
98+ }
99+ }
100+
71101 [ HttpPost ]
72102 public async Task < IActionResult > Post ( EventoDto model )
73103 {
@@ -110,15 +140,48 @@ public async Task<IActionResult> Delete(int id)
110140 var evento = await _eventoService . GetEventoByIdAsync ( id , true ) ;
111141 if ( evento == null ) return NoContent ( ) ;
112142
113- return await _eventoService . DeleteEvento ( id )
114- ? Ok ( new { menssage = "Deletado" } )
115- : throw new Exception ( "Ocorreu um erro não específico ao tentar deletar o Evento" ) ;
143+ if ( await _eventoService . DeleteEvento ( id ) )
144+ {
145+ DeleteImage ( evento . ImagemURL ) ;
146+ return Ok ( new { menssage = "Deletado" } ) ;
147+ }
148+ else
149+ {
150+ throw new Exception ( "Ocorreu um erro não específico ao tentar deletar o Evento" ) ;
151+ }
116152 }
117153 catch ( Exception ex )
118154 {
119155 return this . StatusCode ( StatusCodes . Status500InternalServerError ,
120156 $ "Erro ao tentar deletar eventos! Erro: { ex . Message } ") ;
121157 }
122158 }
159+
160+ [ NonAction ]
161+ public async Task < string > SaveImage ( IFormFile imageFile )
162+ {
163+ string imageName = new String ( Path . GetFileNameWithoutExtension ( imageFile . FileName )
164+ . Take ( 10 )
165+ . ToArray ( )
166+ ) . Replace ( ' ' , '-' ) ;
167+
168+ imageName = $ "{ imageName } { DateTime . UtcNow . ToString ( "yymmssfff" ) } { Path . GetExtension ( imageFile . FileName ) } ";
169+
170+ var imagePath = Path . Combine ( _hostEnvironment . ContentRootPath , @"Resources/images" , imageName ) ;
171+
172+ using ( var fileStream = new FileStream ( imagePath , FileMode . Create ) )
173+ {
174+ await imageFile . CopyToAsync ( fileStream ) ;
175+ }
176+ return imageName ;
177+ }
178+
179+ [ NonAction ]
180+ public void DeleteImage ( string imageName )
181+ {
182+ var imagePath = Path . Combine ( _hostEnvironment . ContentRootPath , @"Resources/images" , imageName ) ;
183+ if ( System . IO . File . Exists ( imagePath ) )
184+ System . IO . File . Delete ( imagePath ) ;
185+ }
123186 }
124187}
0 commit comments