88import com .outfitlab .project .domain .useCases .garment .*;
99import com .outfitlab .project .domain .useCases .bucketImages .SaveImage ;
1010import com .outfitlab .project .presentation .dto .AllGarmentsResponse ;
11+ import org .springframework .security .core .Authentication ;
12+ import org .springframework .security .core .context .SecurityContextHolder ;
1113import com .outfitlab .project .presentation .dto .GarmentRequestDTO ;
1214import org .springframework .data .domain .Page ;
1315import org .springframework .http .ResponseEntity ;
@@ -35,9 +37,11 @@ public class GarmentController {
3537 private final UpdateGarment updateGarment ;
3638
3739 public GarmentController (GetGarmentsByType getGarmentsByType , AddGarmentToFavorite addGarmentToFavourite ,
38- DeleteGarmentFromFavorite deleteGarmentFromFavorite , GetGarmentsFavoritesForUserByEmail getGarmentsFavoritesForUserByEmail ,
39- CreateGarment createGarment , SaveImage saveImage , DeleteGarment deleteGarment , GarmentRepository garmentRepository ,
40- GetGarmentByCode getGarmentByCode , DeleteImage deleteImage , UpdateGarment updateGarment ) {
40+ DeleteGarmentFromFavorite deleteGarmentFromFavorite ,
41+ GetGarmentsFavoritesForUserByEmail getGarmentsFavoritesForUserByEmail ,
42+ CreateGarment createGarment , SaveImage saveImage , DeleteGarment deleteGarment ,
43+ GarmentRepository garmentRepository ,
44+ GetGarmentByCode getGarmentByCode , DeleteImage deleteImage , UpdateGarment updateGarment ) {
4145 this .getGarmentsByType = getGarmentsByType ;
4246 this .addGarmentToFavourite = addGarmentToFavourite ;
4347 this .deleteGarmentFromFavorite = deleteGarmentFromFavorite ;
@@ -52,7 +56,7 @@ public GarmentController(GetGarmentsByType getGarmentsByType, AddGarmentToFavori
5256
5357 @ GetMapping ("/{typeOfGarment}" )
5458 public ResponseEntity <?> getGarmentsByType (@ RequestParam (defaultValue = "0" ) int page ,
55- @ PathVariable String typeOfGarment ){
59+ @ PathVariable String typeOfGarment ) {
5660 try {
5761 return ResponseEntity .ok (buildResponse (this .getGarmentsByType .execute (typeOfGarment , page )
5862 .map (GarmentDTO ::convertModelToDTO )));
@@ -62,16 +66,15 @@ public ResponseEntity<?> getGarmentsByType(@RequestParam(defaultValue = "0") int
6266 }
6367
6468 @ GetMapping ("/all" )
65- public ResponseEntity <?> getAllGarments (@ RequestParam (defaultValue = "0" ) int page ){
69+ public ResponseEntity <?> getAllGarments (@ RequestParam (defaultValue = "0" ) int page ) {
6670 String top = "superior" , bottoms = "inferior" ;
6771
6872 try {
6973 return ResponseEntity .ok (
7074 new AllGarmentsResponse (
7175 buildResponse (this .getGarmentsByType .execute (top , page ).map (GarmentDTO ::convertModelToDTO )),
72- buildResponse (this .getGarmentsByType .execute (bottoms , page ).map (GarmentDTO ::convertModelToDTO ))
73- )
74- );
76+ buildResponse (
77+ this .getGarmentsByType .execute (bottoms , page ).map (GarmentDTO ::convertModelToDTO ))));
7578 } catch (GarmentNotFoundException e ) {
7679 return buildResponseEntityError (e .getMessage ());
7780 }
@@ -89,60 +92,70 @@ private static Map<String, Object> buildResponse(Page<GarmentDTO> response) {
8992 }
9093
9194 @ GetMapping ("/favorite/add/{garmentCode}" )
92- public ResponseEntity <?> addGarmentToFavorite (@ PathVariable String garmentCode ){
95+ public ResponseEntity <?> addGarmentToFavorite (@ PathVariable String garmentCode ) {
9396 try {
94- String userEmail = "german@gmail.com" ; //acá hay que obtenerlo de la session, NO recibirlo por parámetro sino obtenerlo por session, ahora dejo esto pq no tenemos CRUD de user.
97+ // Obtener email del usuario autenticado desde el JWT token
98+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
99+ String userEmail = authentication .getName ();
100+
95101 return ResponseEntity .ok (this .addGarmentToFavourite .execute (garmentCode , userEmail ));
96- } catch (GarmentNotFoundException | UserNotFoundException | UserGarmentFavoriteAlreadyExistsException |
97- FavoritesException e ) {
102+ } catch (GarmentNotFoundException | UserNotFoundException | UserGarmentFavoriteAlreadyExistsException
103+ | FavoritesException e ) {
98104 return buildResponseEntityError (e .getMessage ());
99105 }
100106 }
101107
102108 @ GetMapping ("/favorite/delete/{garmentCode}" )
103- public ResponseEntity <?> deleteGarmentFromFavorite (@ PathVariable String garmentCode ){
109+ public ResponseEntity <?> deleteGarmentFromFavorite (@ PathVariable String garmentCode ) {
104110 try {
105- String userEmail = "german@gmail.com" ; //acá hay que obtenerlo de la session, NO recibirlo por parámetro sino obtenerlo por session, ahora dejo esto pq no tenemos CRUD de user.
111+ // Obtener email del usuario autenticado desde el JWT token
112+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
113+ String userEmail = authentication .getName ();
114+
106115 return ResponseEntity .ok (this .deleteGarmentFromFavorite .execute (garmentCode , userEmail ));
107116 } catch (UserGarmentFavoriteNotFoundException | UserNotFoundException | GarmentNotFoundException e ) {
108117 return buildResponseEntityError (e .getMessage ());
109118 }
110119 }
111120
112121 @ GetMapping ("/favorite" )
113- public ResponseEntity <?> getFavorites (@ RequestParam (defaultValue = "0" ) int page ){
122+ public ResponseEntity <?> getFavorites (@ RequestParam (defaultValue = "0" ) int page ) {
114123 try {
115- String userEmail = "german@gmail.com" ; //acá hay que obtenerlo de la session, NO recibirlo por parámetro sino obtenerlo por session, ahora dejo esto pq no tenemos CRUD de user.
124+ // Obtener email del usuario autenticado desde el JWT token
125+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
126+ String userEmail = authentication .getName ();
127+
116128 return ResponseEntity .ok (this .getGarmentsFavoritesForUserByEmail .execute (userEmail , page ));
117129 } catch (UserNotFoundException | PageLessThanZeroException | FavoritesException e ) {
118130 return buildResponseEntityError (e .getMessage ());
119131 }
120132 }
121133
122134 @ PostMapping (value = "/new" , consumes = "multipart/form-data" )
123- public ResponseEntity <?> newGarment (@ ModelAttribute GarmentRequestDTO request , @ AuthenticationPrincipal UserDetails user ) {
124- String brandCode = "puma" ; //user.marca.brandCode
125- try {
135+ public ResponseEntity <?> newGarment (@ ModelAttribute GarmentRequestDTO request ,
136+ @ AuthenticationPrincipal UserDetails user ) {
137+ String brandCode = "puma" ; // user.marca.brandCode
138+ try {
126139 this .createGarment .execute (
127140 request .getNombre (),
128141 request .getTipo (),
129142 request .getColorNombre (),
130143 brandCode ,
131144 saveImageAndGetUrl (request .getImagen (), "garment_images" ),
132145 request .getClimaNombre (),
133- request .getOcasionesNombres ()
134- );
146+ request .getOcasionesNombres ());
135147
136148 return ResponseEntity .ok ("Prenda creada correctamente." );
137- }catch (BrandsNotFoundException e ){
149+ } catch (BrandsNotFoundException e ) {
138150 return buildResponseEntityError (e .getMessage ());
139151 }
140152 }
141153
142154 @ PutMapping (value = "/update/{garmentCode}" , consumes = "multipart/form-data" )
143- public ResponseEntity <?> updateGarment (@ PathVariable String garmentCode , @ ModelAttribute GarmentRequestDTO request , @ AuthenticationPrincipal UserDetails user ) {
144- String brandCode = "puma" ; //user.marca.brandCode
145- try {
155+ public ResponseEntity <?> updateGarment (@ PathVariable String garmentCode , @ ModelAttribute GarmentRequestDTO request ,
156+ @ AuthenticationPrincipal UserDetails user ) {
157+ String brandCode = "puma" ; // user.marca.brandCode
158+ try {
146159 String oldImageUrl = request .getImagen () != null ? getOldImageUrlOfGarment (garmentCode ) : "" ;
147160
148161 this .updateGarment .execute (
@@ -154,23 +167,23 @@ public ResponseEntity<?> updateGarment(@PathVariable String garmentCode, @ModelA
154167 garmentCode ,
155168 checkIfImageIsEmptyToSaveAndGetUrl (request ),
156169 request .getClimaNombre (),
157- request .getOcasionesNombres ()
158- );
170+ request .getOcasionesNombres ());
159171 deleteImage (oldImageUrl );
160172
161173 return ResponseEntity .ok ("Prenda acctualizada correctamente." );
162- }catch (GarmentNotFoundException e ){
174+ } catch (GarmentNotFoundException e ) {
163175 return buildResponseEntityError (e .getMessage ());
164176 }
165177 }
166178
167179 @ DeleteMapping ("/delete/{garmentCode}" )
168- public ResponseEntity <?> deleteGarment (@ PathVariable String garmentCode , @ AuthenticationPrincipal UserDetails user ) {
169- String brandCode = "puma" ; //user.marca.brandCode
170- try {
180+ public ResponseEntity <?> deleteGarment (@ PathVariable String garmentCode ,
181+ @ AuthenticationPrincipal UserDetails user ) {
182+ String brandCode = "puma" ; // user.marca.brandCode
183+ try {
171184 tryToDeleteGarmentAndImage (garmentCode , brandCode );
172185 return ResponseEntity .ok ("Prenda eliminada correctamente." );
173- }catch (BrandsNotFoundException | DeleteGarmentException e ){
186+ } catch (BrandsNotFoundException | DeleteGarmentException e ) {
174187 return buildResponseEntityError (e .getMessage ());
175188 }
176189 }
@@ -182,7 +195,7 @@ private void tryToDeleteGarmentAndImage(String garmentCode, String brandCode) {
182195 this .deleteGarment .execute (garment , brandCode );
183196 }
184197
185- private ResponseEntity <?> buildResponseEntityError (String message ){
198+ private ResponseEntity <?> buildResponseEntityError (String message ) {
186199 return ResponseEntity
187200 .status (404 )
188201 .body (message );
@@ -197,7 +210,8 @@ private String checkIfImageIsEmptyToSaveAndGetUrl(GarmentRequestDTO request) {
197210 }
198211
199212 private void deleteImage (String oldImageUrl ) {
200- if (!oldImageUrl .isEmpty ()) this .deleteImage .execute (oldImageUrl );
213+ if (!oldImageUrl .isEmpty ())
214+ this .deleteImage .execute (oldImageUrl );
201215 }
202216
203217 private String saveImageAndGetUrl (MultipartFile image , String folder ) {
0 commit comments