-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarmentController.java
More file actions
222 lines (191 loc) · 10.1 KB
/
GarmentController.java
File metadata and controls
222 lines (191 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.outfitlab.project.presentation;
import com.outfitlab.project.domain.exceptions.*;
import com.outfitlab.project.domain.interfaces.repositories.GarmentRepository;
import com.outfitlab.project.domain.model.PrendaModel;
import com.outfitlab.project.domain.model.dto.GarmentDTO;
import com.outfitlab.project.domain.useCases.bucketImages.DeleteImage;
import com.outfitlab.project.domain.useCases.garment.*;
import com.outfitlab.project.domain.useCases.bucketImages.SaveImage;
import com.outfitlab.project.presentation.dto.AllGarmentsResponse;
import com.outfitlab.project.presentation.dto.GarmentRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/api/garments")
public class GarmentController {
private final GetGarmentsByType getGarmentsByType;
private final AddGarmentToFavorite addGarmentToFavourite;
private final DeleteGarmentFromFavorite deleteGarmentFromFavorite;
private final GetGarmentsFavoritesForUserByEmail getGarmentsFavoritesForUserByEmail;
private final CreateGarment createGarment;
private final SaveImage saveImage;
private final DeleteGarment deleteGarment;
private final GetGarmentByCode getGarmentByCode;
private final DeleteImage deleteImage;
private final UpdateGarment updateGarment;
public GarmentController(GetGarmentsByType getGarmentsByType, AddGarmentToFavorite addGarmentToFavourite,
DeleteGarmentFromFavorite deleteGarmentFromFavorite, GetGarmentsFavoritesForUserByEmail getGarmentsFavoritesForUserByEmail,
CreateGarment createGarment, SaveImage saveImage, DeleteGarment deleteGarment, GarmentRepository garmentRepository,
GetGarmentByCode getGarmentByCode, DeleteImage deleteImage, UpdateGarment updateGarment) {
this.getGarmentsByType = getGarmentsByType;
this.addGarmentToFavourite = addGarmentToFavourite;
this.deleteGarmentFromFavorite = deleteGarmentFromFavorite;
this.getGarmentsFavoritesForUserByEmail = getGarmentsFavoritesForUserByEmail;
this.createGarment = createGarment;
this.saveImage = saveImage;
this.deleteGarment = deleteGarment;
this.getGarmentByCode = getGarmentByCode;
this.deleteImage = deleteImage;
this.updateGarment = updateGarment;
}
@GetMapping("/{typeOfGarment}")
public ResponseEntity<?> getGarmentsByType(@RequestParam(defaultValue = "0") int page,
@PathVariable String typeOfGarment){
try {
return ResponseEntity.ok(buildResponse(this.getGarmentsByType.execute(typeOfGarment, page)
.map(GarmentDTO::convertModelToDTO)));
} catch (GarmentNotFoundException e) {
return buildResponseEntityError(e.getMessage());
}
}
@GetMapping("/all")
public ResponseEntity<?> getAllGarments(@RequestParam(defaultValue = "0") int page){
String top = "superior", bottoms = "inferior";
try {
return ResponseEntity.ok(
new AllGarmentsResponse(
buildResponse(this.getGarmentsByType.execute(top, page).map(GarmentDTO::convertModelToDTO)),
buildResponse(this.getGarmentsByType.execute(bottoms, page).map(GarmentDTO::convertModelToDTO))
)
);
} catch (GarmentNotFoundException e) {
return buildResponseEntityError(e.getMessage());
}
}
private static Map<String, Object> buildResponse(Page<GarmentDTO> response) {
Map<String, Object> pageResponse = new HashMap<>();
pageResponse.put("content", response.getContent());
pageResponse.put("page", response.getNumber());
pageResponse.put("size", response.getSize());
pageResponse.put("totalElements", response.getTotalElements());
pageResponse.put("totalPages", response.getTotalPages());
pageResponse.put("last", response.isLast());
return pageResponse;
}
@GetMapping("/favorite/add/{garmentCode}")
public ResponseEntity<?> addGarmentToFavorite(@PathVariable String garmentCode){
try {
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.
return ResponseEntity.ok(this.addGarmentToFavourite.execute(garmentCode, userEmail));
} catch (GarmentNotFoundException | UserNotFoundException | UserGarmentFavoriteAlreadyExistsException |
FavoritesException e) {
return buildResponseEntityError(e.getMessage());
}
}
@GetMapping("/favorite/delete/{garmentCode}")
public ResponseEntity<?> deleteGarmentFromFavorite(@PathVariable String garmentCode){
try {
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.
return ResponseEntity.ok(this.deleteGarmentFromFavorite.execute(garmentCode, userEmail));
} catch (UserGarmentFavoriteNotFoundException | UserNotFoundException | GarmentNotFoundException e) {
return buildResponseEntityError(e.getMessage());
}
}
@GetMapping("/favorite")
public ResponseEntity<?> getFavorites(@RequestParam(defaultValue = "0") int page){
try {
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.
return ResponseEntity.ok(this.getGarmentsFavoritesForUserByEmail.execute(userEmail, page));
} catch (UserNotFoundException | PageLessThanZeroException | FavoritesException e) {
return buildResponseEntityError(e.getMessage());
}
}
@PostMapping(value = "/new", consumes = "multipart/form-data")
public ResponseEntity<?> newGarment(@ModelAttribute GarmentRequestDTO request, @AuthenticationPrincipal UserDetails user) {
log.info(request.toString());
String brandCode = request.getCodigoMarca();
try{
this.createGarment.execute(
request.getNombre(),
request.getTipo(),
request.getColorNombre(),
brandCode,
saveImageAndGetUrl(request.getImagen(), "garment_images"),
request.getClimaNombre(),
request.getOcasionesNombres(),
request.getGenero()
);
return ResponseEntity.ok("Prenda creada correctamente.");
}catch (BrandsNotFoundException e){
return buildResponseEntityError(e.getMessage());
}
}
@PatchMapping(value = "/update/{garmentCode}", consumes = "multipart/form-data")
public ResponseEntity<?> updateGarment(@PathVariable String garmentCode, @ModelAttribute GarmentRequestDTO request, @AuthenticationPrincipal UserDetails user) {
log.info(request.toString());
String brandCode;
try{
PrendaModel existingGarment = this.getGarmentByCode.execute(garmentCode);
brandCode = existingGarment.getMarca().getCodigoMarca();
String oldImageUrl = request.getImagen() != null ? existingGarment.getImagenUrl() : "";
this.updateGarment.execute(
request.getNombre(),
request.getTipo(),
request.getColorNombre(),
request.getEvento(),
garmentCode,
brandCode,
checkIfImageIsEmptyToSaveAndGetUrl(request),
request.getClimaNombre(),
request.getOcasionesNombres(),
request.getGenero()
);
deleteImage(oldImageUrl);
return ResponseEntity.ok("Prenda actualizada correctamente.");
}catch (GarmentNotFoundException | BrandsNotFoundException e){
return buildResponseEntityError(e.getMessage());
}
}
@DeleteMapping("/delete/{garmentCode}")
public ResponseEntity<?> deleteGarment(@PathVariable String garmentCode, @AuthenticationPrincipal UserDetails user) {
String brandCode;
try{
PrendaModel garment = this.getGarmentByCode.execute(garmentCode);
brandCode = garment.getMarca().getCodigoMarca();
tryToDeleteGarmentAndImage(garmentCode, brandCode);
return ResponseEntity.ok("Prenda eliminada correctamente.");
}catch (GarmentNotFoundException | BrandsNotFoundException | DeleteGarmentException e){
return buildResponseEntityError(e.getMessage());
}
}
private void tryToDeleteGarmentAndImage(String garmentCode, String brandCode) {
PrendaModel garment = this.getGarmentByCode.execute(garmentCode);
deleteImage(garment.getImagenUrl());
this.deleteGarment.execute(garment, brandCode);
}
private ResponseEntity<?> buildResponseEntityError(String message){
return ResponseEntity
.status(404)
.body(message);
}
private String getOldImageUrlOfGarment(String garmentCode) {
return this.getGarmentByCode.execute(garmentCode).getImagenUrl();
}
private String checkIfImageIsEmptyToSaveAndGetUrl(GarmentRequestDTO request) {
return request.getImagen() != null ? saveImageAndGetUrl(request.getImagen(), "garment_images") : "";
}
private void deleteImage(String oldImageUrl) {
if (!oldImageUrl.isEmpty()) this.deleteImage.execute(oldImageUrl);
}
private String saveImageAndGetUrl(MultipartFile image, String folder) {
return this.saveImage.execute(image, folder);
}
}