1 | package com.example.rezevirajmasa.demo.web.rest;
|
---|
2 |
|
---|
3 | import com.example.rezevirajmasa.demo.dto.*;
|
---|
4 | import com.example.rezevirajmasa.demo.mappers.UserMapper;
|
---|
5 | import com.example.rezevirajmasa.demo.model.*;
|
---|
6 | import com.example.rezevirajmasa.demo.model.exceptions.InvalidReservationException;
|
---|
7 | import com.example.rezevirajmasa.demo.service.*;
|
---|
8 | import com.example.rezevirajmasa.demo.service.impl.TokenService;
|
---|
9 | import jakarta.servlet.http.HttpServletRequest;
|
---|
10 | import jdk.jfr.consumer.RecordingStream;
|
---|
11 | import org.apache.coyote.Response;
|
---|
12 | import org.apache.http.protocol.HTTP;
|
---|
13 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
14 | import org.springframework.http.HttpStatus;
|
---|
15 | import org.springframework.http.ResponseEntity;
|
---|
16 | import org.springframework.security.core.Authentication;
|
---|
17 | import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
---|
18 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
19 | import org.springframework.security.core.parameters.P;
|
---|
20 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
21 | import org.springframework.web.bind.annotation.*;
|
---|
22 |
|
---|
23 | import java.nio.file.attribute.UserPrincipal;
|
---|
24 | import java.security.Principal;
|
---|
25 | import java.time.LocalDateTime;
|
---|
26 | import java.time.format.DateTimeFormatter;
|
---|
27 | import java.util.*;
|
---|
28 | import java.util.stream.Collectors;
|
---|
29 |
|
---|
30 | @CrossOrigin(origins = "http://localhost:3000/")
|
---|
31 | @RestController
|
---|
32 | public class testController {
|
---|
33 | private final RestaurantService restaurantService;
|
---|
34 | private final UserService userService;
|
---|
35 | private final ReservationService reservationService;
|
---|
36 | private final ReservationHistoryService reservationHistoryService;
|
---|
37 | private final TableService tableService;
|
---|
38 | private final MenuService menuService;
|
---|
39 | private final UserMapper userMapper;
|
---|
40 | private final TokenService tokenService;
|
---|
41 | public testController(RestaurantService restaurantService, UserService userService, ReservationService reservationService, ReservationHistoryService reservationHistoryService, TableService tableService, MenuService menuService, UserMapper userMapper, TokenService tokenService) {
|
---|
42 | this.restaurantService = restaurantService;
|
---|
43 | this.userService = userService;
|
---|
44 | this.reservationService = reservationService;
|
---|
45 | this.reservationHistoryService = reservationHistoryService;
|
---|
46 | this.tableService = tableService;
|
---|
47 | this.menuService = menuService;
|
---|
48 | this.userMapper = userMapper;
|
---|
49 | this.tokenService = tokenService;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping("/api/memberships")
|
---|
53 | public ResponseEntity<List<MembershipLevel>> getMemeberships() {
|
---|
54 | return new ResponseEntity<List<MembershipLevel>>(List.of(MembershipLevel.values()), HttpStatus.OK);
|
---|
55 | }
|
---|
56 |
|
---|
57 | //restaurants calls
|
---|
58 |
|
---|
59 | @GetMapping("/api/restaurants")
|
---|
60 | public ResponseEntity<List<RestaurantDTO>> getAllRestaurants() {
|
---|
61 | return new ResponseEntity<List<RestaurantDTO>>(restaurantService.listall(), HttpStatus.OK);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @GetMapping("/api/restaurants/{restaurantId}")
|
---|
65 | public ResponseEntity<RestaurantDTO> getRestaurantById(@PathVariable Long restaurantId) {
|
---|
66 | RestaurantDTO restaurantDTO = restaurantService.findById(restaurantId);
|
---|
67 | return new ResponseEntity<>(restaurantDTO, HttpStatus.OK);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @PostMapping("/api/search")
|
---|
71 | public ResponseEntity<List<RestaurantDTO>> searchRestaurants(@RequestBody Map<String, Object> requestData) {
|
---|
72 | Optional<String> dateTimeOptional = Optional.ofNullable((String) requestData.get("dateTime"));
|
---|
73 | int partySize = Integer.parseInt(requestData.get("partySize").toString());
|
---|
74 | Optional<String> searchOptional = Optional.ofNullable((String) requestData.get("search"));
|
---|
75 |
|
---|
76 | String dateTime = dateTimeOptional.orElse(null);
|
---|
77 | String search = searchOptional.orElse(null);
|
---|
78 |
|
---|
79 | LocalDateTime parsedDateTime = null;
|
---|
80 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
---|
81 | if (dateTime != null) {
|
---|
82 | parsedDateTime = LocalDateTime.parse(dateTime, formatter);
|
---|
83 | }
|
---|
84 |
|
---|
85 | List<RestaurantDTO> filteredRestaurants = restaurantService.findRestaurantsBySearchParams(parsedDateTime, partySize, search);
|
---|
86 |
|
---|
87 | return new ResponseEntity<>(filteredRestaurants, HttpStatus.OK);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @PostMapping("/api/search/shortcut/{param}")
|
---|
91 | public ResponseEntity<List<RestaurantDTO>> searchByCuisineTypeShortcut(@PathVariable String param) {
|
---|
92 | List<RestaurantDTO> filteredRestaurants;
|
---|
93 | if(param != null && !param.isEmpty()) {
|
---|
94 | filteredRestaurants = restaurantService.findRestaurantsByCuisineType(param);
|
---|
95 | } else {
|
---|
96 | filteredRestaurants = restaurantService.listall();
|
---|
97 | }
|
---|
98 | return new ResponseEntity<List<RestaurantDTO>>(filteredRestaurants, HttpStatus.OK);
|
---|
99 | }
|
---|
100 |
|
---|
101 | @GetMapping("/api/cuisineTypes")
|
---|
102 | public ResponseEntity<List<String>> getAllCuisineTypes() {
|
---|
103 | List<String> cuisineTypes = restaurantService.findALlCuisineTypes();
|
---|
104 | return new ResponseEntity<>(cuisineTypes, HttpStatus.OK);
|
---|
105 | }
|
---|
106 |
|
---|
107 | // User calls
|
---|
108 |
|
---|
109 | @GetMapping("/api/user/{email}")
|
---|
110 | public ResponseEntity<User> getUserByEmail(@PathVariable String email)
|
---|
111 | {
|
---|
112 | User user = userService.findByMail(email);
|
---|
113 | return ResponseEntity.ok(user);
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Reservation Calls
|
---|
117 |
|
---|
118 | @GetMapping("/api/reservations/by/{email}")
|
---|
119 | public ResponseEntity<List<ReservationDTO>> getReservations(@PathVariable String email) {
|
---|
120 | if (email == null || email.isEmpty()) {
|
---|
121 | return ResponseEntity.badRequest().build();
|
---|
122 | }
|
---|
123 | User user = userService.findByMail(email);
|
---|
124 | if (user == null) {
|
---|
125 | return ResponseEntity.notFound().build();
|
---|
126 | }
|
---|
127 |
|
---|
128 | List<Reservation> reservations = reservationService.findReservationByUser(user);
|
---|
129 | List<ReservationDTO> reservationDTOs = reservations.stream()
|
---|
130 | .map(ReservationDTO::new)
|
---|
131 | .collect(Collectors.toList());
|
---|
132 |
|
---|
133 | return ResponseEntity.ok(reservationDTOs);
|
---|
134 | }
|
---|
135 |
|
---|
136 | @PostMapping("/api/reservations")
|
---|
137 | public ResponseEntity<?> createReservation(@RequestBody ReservationDTO reservation){
|
---|
138 | User user = userService.findByMail(reservation.getUserEmail());
|
---|
139 | Restaurant restaurant = restaurantService.findByIdRestaurant(reservation.getRestaurantId());
|
---|
140 | Reservation savedReservation = reservationService.makeReservationRest(reservation, user, restaurant);
|
---|
141 |
|
---|
142 | return new ResponseEntity<>(savedReservation, HttpStatus.CREATED);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @GetMapping("/api/reservations/{reservationId}")
|
---|
146 | public ResponseEntity<ReservationDTO> getReservation(@PathVariable Long reservationId) {
|
---|
147 | Reservation reservation = reservationService.findById(reservationId);
|
---|
148 | if (reservation != null) {
|
---|
149 | return ResponseEntity.ok(new ReservationDTO(reservation));
|
---|
150 | } else {
|
---|
151 | return ResponseEntity.notFound().build();
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // @GetMapping("/api/reservations/{restaurantId}")
|
---|
156 | // public ResponseEntity<List<Reservation>> getReservationsByRestaurant(@PathVariable Long restaurantId)
|
---|
157 | // {
|
---|
158 | // Restaurant restaurant = restaurantService.findByIdRestaurant(restaurantId);
|
---|
159 | // List<Reservation> reservations = reservationService.findAllByRestaurant(restaurant);
|
---|
160 | // return ResponseEntity.ok(reservations);
|
---|
161 | // }
|
---|
162 |
|
---|
163 | @GetMapping("/api/table-reservations/{tableId}")
|
---|
164 | public ResponseEntity<List<LocalDateTime>> tableReservations(@PathVariable Long tableId) {
|
---|
165 | TableEntity table = tableService.findByIdTable(tableId);
|
---|
166 | if (table == null) {
|
---|
167 | return ResponseEntity.notFound().build();
|
---|
168 | }
|
---|
169 |
|
---|
170 | List<Reservation> reservations = reservationService.reservationsForTable(table);
|
---|
171 | List<LocalDateTime> reservedTimes = reservations.stream()
|
---|
172 | .map(Reservation::getReservationDateTime)
|
---|
173 | .collect(Collectors.toList());
|
---|
174 |
|
---|
175 | return ResponseEntity.ok(reservedTimes);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | @PostMapping("/api/reservations/{reservationId}/{email}")
|
---|
180 | public ResponseEntity<Reservation> editReservation(@PathVariable Long reservationId,
|
---|
181 | @RequestBody ReservationDTO reservationDTO,
|
---|
182 | @PathVariable String email) {
|
---|
183 | User user = userService.findByMail(email);
|
---|
184 | Restaurant restaurant = restaurantService.findByIdRestaurant(reservationDTO.getRestaurantId());
|
---|
185 | if (!reservationDTO.getReservationID().equals(reservationId)) {
|
---|
186 | return ResponseEntity.badRequest().build();
|
---|
187 | }
|
---|
188 |
|
---|
189 | try {
|
---|
190 | Reservation updatedReservation = reservationService.updateReservation(reservationId, reservationDTO, user);
|
---|
191 | return ResponseEntity.ok(updatedReservation);
|
---|
192 | } catch (InvalidReservationException e) {
|
---|
193 | return ResponseEntity.status(409).body(null);
|
---|
194 | } catch (Exception e) {
|
---|
195 | return ResponseEntity.status(500).build();
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | @DeleteMapping("/api/reservations/delete/{reservationId}")
|
---|
202 | public ResponseEntity<Response> deleteReservation(@PathVariable Long reservationId) {
|
---|
203 | boolean deleted = reservationService.cancelReservation(reservationId);
|
---|
204 | if(deleted) {
|
---|
205 | return ResponseEntity.ok().build();
|
---|
206 | } else {
|
---|
207 | return ResponseEntity.notFound().build();
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | @GetMapping("/api/reservations/past/{email}")
|
---|
212 | public ResponseEntity<List<Restaurant.ReservationHistory>> pastReservations(@PathVariable String email) {
|
---|
213 | User user = userService.findByMail(email);
|
---|
214 | return null;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // TableEntity Calls
|
---|
218 | @GetMapping("/api/tables/{tableId}")
|
---|
219 | public ResponseEntity<TableDTO> findTableById(@PathVariable Long tableId) {
|
---|
220 | TableDTO tableDTO = tableService.findById(tableId);
|
---|
221 | return new ResponseEntity<>(tableDTO, HttpStatus.OK);
|
---|
222 | }
|
---|
223 |
|
---|
224 | // past reservation calls
|
---|
225 | @GetMapping("/api/past-reservations/{email}")
|
---|
226 | public ResponseEntity<List<Restaurant.ReservationHistory>> findPastReservationsByUser(@PathVariable String email) {
|
---|
227 | User user = userService.findByMail(email);
|
---|
228 | List<Restaurant.ReservationHistory> reservations = reservationHistoryService.findByUser(user);
|
---|
229 | return new ResponseEntity<>(reservations, HttpStatus.OK);
|
---|
230 | }
|
---|
231 |
|
---|
232 | // menu calls
|
---|
233 | @GetMapping("/api/restaurant-menu/{restaurantId}")
|
---|
234 | public List<Menu> getMenuByRestaurantId(@PathVariable Long restaurantId) {
|
---|
235 | return menuService.getMenuByRestaurantId(restaurantId);
|
---|
236 | }
|
---|
237 | }
|
---|