-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservationController.java
More file actions
145 lines (134 loc) · 8.5 KB
/
Copy pathReservationController.java
File metadata and controls
145 lines (134 loc) · 8.5 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
package kattsyn.dev.rentplace.controllers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import kattsyn.dev.rentplace.dtos.*;
import kattsyn.dev.rentplace.services.ReservationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequiredArgsConstructor
@RestController
@Validated
@RequestMapping("${api.path}/reservations")
@Tag(name = "Reservation Controller", description = "Взаимодействие с бронированиями")
public class ReservationController {
private final ReservationService reservationService;
@Operation(
summary = "Получение всех бронирований",
description = "Позволяет получить все бронирования"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Успешно", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO[].class))),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@GetMapping("/")
public ResponseEntity<List<ReservationDTO>> getReservations() {
List<ReservationDTO> reservationDTOS = reservationService.findAllReservations();
return ResponseEntity.ok(reservationDTOS);
}
@Operation(
summary = "Получение всех бронирований пользователя",
description = "Позволяет получить все бронирования пользователя"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Успешно", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO[].class))),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@GetMapping("/my")
@SecurityRequirement(name = "JWT")
public ResponseEntity<List<ReservationDTO>> getUserReservations(Authentication authentication) {
List<ReservationDTO> reservationDTOS = reservationService.findAllReservationsByRenterEmail(authentication.getName());
return ResponseEntity.ok(reservationDTOS);
}
@Operation(
summary = "Получение бронирования",
description = "Получение бронирования по id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Успешно", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO.class))
}),
@ApiResponse(responseCode = "400", description = "Получен некорректный ID", content = @Content),
@ApiResponse(responseCode = "404", description = "Бронирование не найдено", content = @Content),
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
@SecurityRequirement(name = "JWT")
@GetMapping("/{id}")
public ResponseEntity<ReservationDTO> getReservation(@PathVariable
@Valid @Parameter(description = "id бронирования", example = "1") long id) {
ReservationDTO reservationDTO = reservationService.getReservationDTOById(id);
return ResponseEntity.ok(reservationDTO);
}
@Operation(
summary = "Создать бронирование",
description = "Создать бронирование"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Успешно создано", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO.class))
}),
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
@SecurityRequirement(name = "JWT")
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ReservationDTO> createReservation(@Valid @ModelAttribute ReservationCreateEditDTO reservationCreateEditDTO,
Authentication authentication) {
reservationService.allowedToCreateReservationOrAdmin(reservationCreateEditDTO, authentication.getName());
return ResponseEntity.ok(reservationService.createReservation(reservationCreateEditDTO));
}
@Operation(
summary = "Изменить бронирование",
description = "Изменить бронирование"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Успешно изменено", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO.class))
}),
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
@SecurityRequirement(name = "JWT")
@SecurityRequirement(name = "JWT")
@PatchMapping(path = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ReservationDTO> updateReservation(@PathVariable @Parameter(description = "id бронирования для изменения") long id,
@Valid @ModelAttribute ReservationCreateEditDTO reservationCreateEditDTO) {
return ResponseEntity.ok(reservationService.updateReservation(id, reservationCreateEditDTO));
}
@DeleteMapping("/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Успешно. Возвращает удаленную сущность", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ReservationDTO.class))
}),
@ApiResponse(responseCode = "400", description = "Получен некорректный ID", content = @Content),
@ApiResponse(responseCode = "404", description = "Категория не найдена", content = @Content),
@ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content),
@ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content)
})
@PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_USER')" )
@SecurityRequirement(name = "JWT")
@SecurityRequirement(name = "JWT")
public ResponseEntity<ReservationDTO> deleteReservation(
@PathVariable
@Valid @Parameter(description = "id бронирования", example = "1") long id,
Authentication authentication
) {
reservationService.ownsReservationOrAdmin(id, authentication.getName());
return ResponseEntity.ok(reservationService.deleteById(id));
}
}