/
PizzaController.java
370 lines (355 loc) · 15.8 KB
/
PizzaController.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.pizza.controller;
import com.pizza.annotation.RoleAdmin;
import com.pizza.annotation.RoleAdminOrUser;
import com.pizza.configuration.rest.RestRoutes;
import com.pizza.dto.PizzaDto;
import com.pizza.model.Ingredient;
import com.pizza.model.Pizza;
import com.pizza.service.PizzaService;
import com.pizza.util.converter.PizzaConverter;
import com.spring5microservices.common.dto.ErrorResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import static java.lang.String.format;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* Rest services to work with {@link Pizza}
*/
@AllArgsConstructor
@Log4j2
@RestController
@RequestMapping(RestRoutes.PIZZA.ROOT)
@Validated
@Tag(name = "PizzaController", description = "Endpoints to manage operations related with pizzas")
public class PizzaController {
@Lazy
private final PizzaService service;
@Lazy
private final PizzaConverter converter;
/**
* Used to create a new {@link PizzaDto}
*
* @param pizzaDto
* {@link PizzaDto} to create
*
* @return if {@code pizzaDto} is not {@code null}: {@link HttpStatus#CREATED} and created {@link Pizza}
* if {@code pizzaDto} is {@code null}: {@link HttpStatus#UNPROCESSABLE_ENTITY} and {@code Null}
*/
@Operation(
summary = "Create a pizza",
description = "Create a pizza (only allowed to user with role admin)"
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "201",
description = "The given pizza was successfully created",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = PizzaDto.class)
)
),
@ApiResponse(
responseCode = "400",
description = "There was a problem in the given request, the given parameters have not passed the required validations",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "401",
description = "The user has not authorization to execute this request or provided authorization has expired",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "422",
description = "The pizza could not be created"
),
@ApiResponse(
responseCode = "500",
description = "There was an internal problem in the server",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
)
}
)
@PostMapping
@Transactional(rollbackFor = Exception.class)
@RoleAdmin
public Mono<ResponseEntity<PizzaDto>> create(@RequestBody @Valid final PizzaDto pizzaDto) {
log.info(
format("Creating the pizza: %s",
pizzaDto)
);
return Mono.just(
service.save(
converter.fromDtoToModel(pizzaDto)
)
.map(converter::fromModelToDto)
.map(p ->
new ResponseEntity<>(
p,
CREATED
)
)
.orElseGet(() ->
new ResponseEntity<>(UNPROCESSABLE_ENTITY)
)
);
}
/**
* Returns the {@link Pizza} which name matches with the given one.
*
* @param name
* Name to search in the current {@link Pizza#getName()}s
*
* @return if {@code name} was found: {@link HttpStatus#OK} and {@link PizzaDto} that matches
* if {@code name} was not found: {@link HttpStatus#NOT_FOUND}
*/
@Operation(
summary = "Find pizza information matches given name",
description = "Find pizza information matches given name (only allowed to user with role admin/user)"
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "There is a pizza with the given name",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = PizzaDto.class)
)
),
@ApiResponse(
responseCode = "400",
description = "There was a problem in the given request, the given parameters have not passed the required validations",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "401",
description = "The user has not authorization to execute this request or provided authorization has expired",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "404",
description = "There is no a pizza with the given name"
),
@ApiResponse(
responseCode = "500",
description = "There was an internal problem in the server",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
)
}
)
@GetMapping("/{name}")
@Transactional(readOnly = true)
@RoleAdminOrUser
public Mono<ResponseEntity<PizzaDto>> findByName(@PathVariable @Size(min=1, max=64) final String name) {
log.info(
format("Searching the pizza with name: %s",
name)
);
return Mono.just(
service.findByName(name)
.map(converter::fromModelToDto)
.map(p ->
new ResponseEntity<>(
p,
OK
)
)
.orElseGet(() ->
new ResponseEntity<>(NOT_FOUND)
)
);
}
/**
* Returns the required page information about {@link Pizza}s with their {@link Ingredient}s
*
* @param page
* Number of page to get
* @param size
* Number of elements in every page
*
* @return {@link Page} of {@link PizzaDto}
*/
@Operation(
summary = "Get list of pizzas with their ingredients",
description = "Get list of pizzas with their ingredients (only allowed to user with role admin/user)"
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "The list of existing pizzas",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(type = "List", implementation = PizzaDto.class)
)
),
@ApiResponse(
responseCode = "400",
description = "There was a problem in the given request, the given parameters have not passed the required validations",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "401",
description = "The user has not authorization to execute this request or provided authorization has expired",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "500",
description = "There was an internal problem in the server",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
)
}
)
@GetMapping(RestRoutes.PIZZA.PAGE_WITH_INGREDIENTS)
@Transactional(readOnly = true)
@RoleAdminOrUser
public Mono<Page<PizzaDto>> findPageWithIngredients(@RequestParam(value = "page") @PositiveOrZero final int page,
@RequestParam(value = "size") @Positive final int size) {
log.info(
format("Returning the page of pizzas related with page: %d and size: %d",
page, size)
);
return Mono.just(
service.findPageWithIngredients(
page,
size,
null
)
.map(converter::fromModelToDto)
);
}
/**
* Used to update an existing {@link PizzaDto}
*
* @param pizzaDto
* {@link PizzaDto} to update
*
* @return if {@code pizzaDto} is not {@code null} and exists: {@link HttpStatus#OK} and updated {@link Pizza}
* if {@code pizzaDto} is {@code null} or not exists: {@link HttpStatus#NOT_FOUND} and {@code Null}
*/
@Operation(
summary = "Update a pizza",
description = "Update a pizza (only allowed to user with role admin)"
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "201",
description = "The given pizza was successfully update",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = PizzaDto.class)
)
),
@ApiResponse(
responseCode = "400",
description = "There was a problem in the given request, the given parameters have not passed the required validations",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "401",
description = "The user has not authorization to execute this request or provided authorization has expired",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
@ApiResponse(
responseCode = "404",
description = "There is no a pizza matches with provided information"
),
@ApiResponse(
responseCode = "500",
description = "There was an internal problem in the server",
content = @Content(
mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponseDto.class)
)
),
}
)
@PutMapping
@Transactional(rollbackFor = Exception.class)
@RoleAdmin
public Mono<ResponseEntity<PizzaDto>> update(@RequestBody @Valid final PizzaDto pizzaDto) {
log.info(
format("Updating the pizza: %s",
pizzaDto)
);
return Mono.just(
service.save(
converter.fromDtoToModel(pizzaDto)
)
.map(converter::fromModelToDto)
.map(p ->
new ResponseEntity<>(
p,
OK
)
)
.orElseGet(() ->
new ResponseEntity<>(NOT_FOUND)
)
);
}
}