- Timestamp:
- 04/28/25 14:21:17 (3 weeks ago)
- Branches:
- main
- Children:
- e15e8d9
- Parents:
- f5b256e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/rezevirajmasa/demo/web/rest/testController.java
rf5b256e rdeea3c4 1 1 package com.example.rezevirajmasa.demo.web.rest; 2 2 3 import com.example.rezevirajmasa.demo.dto.CustomerDTO; 4 import com.example.rezevirajmasa.demo.dto.UserDto; 3 import com.example.rezevirajmasa.demo.dto.*; 5 4 import com.example.rezevirajmasa.demo.mappers.UserMapper; 6 5 import com.example.rezevirajmasa.demo.model.*; 6 import com.example.rezevirajmasa.demo.model.exceptions.InvalidReservationException; 7 7 import com.example.rezevirajmasa.demo.service.*; 8 8 import com.example.rezevirajmasa.demo.service.impl.TokenService; … … 25 25 import java.time.LocalDateTime; 26 26 import java.time.format.DateTimeFormatter; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Optional; 27 import java.util.*; 28 import java.util.stream.Collectors; 31 29 32 30 @CrossOrigin(origins = "http://localhost:3000/") … … 57 55 } 58 56 59 //restaurants 60 61 @ RequestMapping("/api/restaurants")62 public ResponseEntity<List<Restaurant >> getAllRestaurants() {63 return new ResponseEntity<List<Restaurant>>(restaurantService.listall(), HttpStatus.OK);64 } 65 66 @ RequestMapping("/api/restaurants/{restaurantId}")67 public ResponseEntity<Restaurant > getRestaurantById(@PathVariable Long restaurantId) {68 return new ResponseEntity<Restaurant >(restaurantService.findById(restaurantId), HttpStatus.OK);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 return new ResponseEntity<RestaurantDTO>(restaurantService.findById(restaurantId), HttpStatus.OK); 69 67 } 70 68 71 69 @PostMapping("/api/search") 72 public ResponseEntity<List<Restaurant >> searchRestaurants(@RequestBody Map<String, Object> requestData) {70 public ResponseEntity<List<RestaurantDTO>> searchRestaurants(@RequestBody Map<String, Object> requestData) { 73 71 Optional<String> dateTimeOptional = Optional.ofNullable((String) requestData.get("dateTime")); 74 72 int partySize = Integer.parseInt(requestData.get("partySize").toString()); … … 84 82 } 85 83 86 List<Restaurant > filteredRestaurants = restaurantService.findRestaurantsBySearchParams(parsedDateTime, partySize, search);87 88 return new ResponseEntity< List<Restaurant>>(filteredRestaurants, HttpStatus.OK);84 List<RestaurantDTO> filteredRestaurants = restaurantService.findRestaurantsBySearchParams(parsedDateTime, partySize, search); 85 86 return new ResponseEntity<>(filteredRestaurants, HttpStatus.OK); 89 87 } 90 88 91 89 @PostMapping("/api/search/shortcut/{param}") 92 public ResponseEntity<List<Restaurant >> searchByCuisineTypeShortcut(@PathVariable String param) {93 List<Restaurant > filteredRestaurants;90 public ResponseEntity<List<RestaurantDTO>> searchByCuisineTypeShortcut(@PathVariable String param) { 91 List<RestaurantDTO> filteredRestaurants; 94 92 if(param != null && !param.isEmpty()) { 95 93 filteredRestaurants = restaurantService.findRestaurantsByCuisineType(param); … … 97 95 filteredRestaurants = restaurantService.listall(); 98 96 } 99 return new ResponseEntity<List<Restaurant >>(filteredRestaurants, HttpStatus.OK);97 return new ResponseEntity<List<RestaurantDTO>>(filteredRestaurants, HttpStatus.OK); 100 98 } 101 99 … … 106 104 } 107 105 106 // User calls 107 108 @GetMapping("/api/user/{email}") 109 public ResponseEntity<User> getUserByEmail(@PathVariable String email) 110 { 111 User user = userService.findByMail(email); 112 return ResponseEntity.ok(user); 113 } 114 108 115 // Customer CALLS 109 116 110 @ RequestMapping("/api/customers")117 @GetMapping("/api/customers") 111 118 public ResponseEntity<List<CustomerDTO>> getAllCustomers() { 112 119 List<Customer> customers = customerService.listAll(); … … 119 126 } 120 127 121 @ RequestMapping("/api/customers/{id}")128 @GetMapping("/api/customers/{id}") 122 129 public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) { 123 130 return new ResponseEntity<Customer>(customerService.findById(id), HttpStatus.OK); … … 130 137 ); 131 138 } 132 @GetMapping("/api/user")133 public ResponseEntity<?> getCurrentUser() {134 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();135 136 if (authentication == null || !authentication.isAuthenticated() || authentication.getPrincipal().equals("anonymousUser")) {137 return new ResponseEntity<>("User is not authenticated", HttpStatus.UNAUTHORIZED);138 }139 140 Customer customer = (Customer) authentication.getPrincipal();141 return new ResponseEntity<>(customer, HttpStatus.OK);142 }143 144 139 145 140 @PostMapping("/api/customers") … … 166 161 167 162 @GetMapping("/api/reservations/by/{email}") 168 public ResponseEntity<List<Reservation>> getReservations(@PathVariable String email) { 169 User user = userService.findByMail(email); 170 return new ResponseEntity<List<Reservation>>(reservationService.findReservationByUser(user), HttpStatus.OK); 171 } 172 173 @PostMapping("/api/reservations/{email}") 174 public ResponseEntity<Reservation> createReservation(@RequestBody Reservation reservation, @PathVariable String email) { 175 User user = userService.findByMail(email); 163 public ResponseEntity<List<ReservationDTO>> getReservations(@PathVariable String email) { 164 if (email == null || email.isEmpty()) { 165 return ResponseEntity.badRequest().build(); 166 } 167 User user = userService.findByMail(email); 168 if (user == null) { 169 return ResponseEntity.notFound().build(); 170 } 171 172 List<Reservation> reservations = reservationService.findReservationByUser(user); 173 List<ReservationDTO> reservationDTOs = reservations.stream() 174 .map(ReservationDTO::new) 175 .collect(Collectors.toList()); 176 177 return ResponseEntity.ok(reservationDTOs); 178 } 179 180 @PostMapping("/api/reservations") 181 public ResponseEntity<?> createReservation(@RequestBody ReservationDTO reservation) { 182 User user = userService.findByMail(reservation.getUserEmail()); 176 183 Reservation savedReservation = reservationService.makeReservationRest(reservation, user); 177 184 … … 179 186 } 180 187 181 182 188 @GetMapping("/api/reservations/{reservationId}") 183 public ResponseEntity<Reservation > getReservation(@PathVariable Long reservationId) {189 public ResponseEntity<ReservationDTO> getReservation(@PathVariable Long reservationId) { 184 190 Reservation reservation = reservationService.findById(reservationId); 185 191 if (reservation != null) { 186 return ResponseEntity.ok(reservation); 187 } else { 188 return ResponseEntity.notFound().build(); 189 } 190 } 192 return ResponseEntity.ok(new ReservationDTO(reservation)); 193 } else { 194 return ResponseEntity.notFound().build(); 195 } 196 } 197 198 // @GetMapping("/api/reservations/{restaurantId}") 199 // public ResponseEntity<List<Reservation>> getReservationsByRestaurant(@PathVariable Long restaurantId) 200 // { 201 // Restaurant restaurant = restaurantService.findByIdRestaurant(restaurantId); 202 // List<Reservation> reservations = reservationService.findAllByRestaurant(restaurant); 203 // return ResponseEntity.ok(reservations); 204 // } 205 206 @GetMapping("/api/table-reservations/{tableId}") 207 public ResponseEntity<List<LocalDateTime>> tableReservations(@PathVariable Long tableId) { 208 TableEntity table = tableService.findByIdTable(tableId); 209 if (table == null) { 210 return ResponseEntity.notFound().build(); 211 } 212 213 List<Reservation> reservations = reservationService.reservationsForTable(table); 214 List<LocalDateTime> reservedTimes = reservations.stream() 215 .map(Reservation::getReservationDateTime) 216 .collect(Collectors.toList()); 217 218 return ResponseEntity.ok(reservedTimes); 219 } 220 191 221 192 222 @PostMapping("/api/reservations/{reservationId}/{email}") 193 223 public ResponseEntity<Reservation> editReservation(@PathVariable Long reservationId, 194 @RequestBody Reservation reservation,224 @RequestBody ReservationDTO reservationDTO, 195 225 @PathVariable String email) { 196 226 User user = userService.findByMail(email); 197 227 198 if (!reservation .getReservationID().equals(reservationId)) {228 if (!reservationDTO.getReservationID().equals(reservationId)) { 199 229 return ResponseEntity.badRequest().build(); 200 230 } 201 231 202 // Fetch existing reservation 203 Reservation existingReservation = reservationService.findById(reservationId); 204 if (existingReservation == null) { 205 return ResponseEntity.notFound().build(); 206 } 207 208 if (!reservation.getCheckInTime().equals(existingReservation.getCheckInTime())) { 209 tableService.canceledTimeSlots(existingReservation.getTable().getId(), existingReservation.getCheckInTime()); 210 211 tableService.deleteTimeSlotsForReservation(reservation.getTable().getId(), reservation.getCheckInTime()); 212 } 213 214 existingReservation.setReservationDateTime(LocalDateTime.now()); 215 existingReservation.setCheckInTime(reservation.getCheckInTime()); 216 existingReservation.setCheckOutTime(reservation.getCheckInTime().plusHours(2)); 217 existingReservation.setPartySize(reservation.getPartySize()); 218 existingReservation.setSpecialRequests(reservation.getSpecialRequests()); 219 220 Reservation updatedReservation = reservationService.makeReservationRest(existingReservation, user); 221 222 System.out.println("Updated reservation time: " + updatedReservation.getCheckInTime()); 223 return ResponseEntity.ok(updatedReservation); 224 } 232 try { 233 Reservation updatedReservation = reservationService.updateReservation(reservationId, reservationDTO, user); 234 return ResponseEntity.ok(updatedReservation); 235 } catch (InvalidReservationException e) { 236 return ResponseEntity.status(409).body(null); 237 } catch (Exception e) { 238 return ResponseEntity.status(500).build(); 239 } 240 } 241 225 242 226 243 … … 241 258 } 242 259 243 // TableEntity Calls 244 260 // TableEntity Calls 245 261 @GetMapping("/api/tables/{tableId}") 246 public ResponseEntity<TableEntity> findTableById(@PathVariable Long tableId) { 247 return new ResponseEntity<TableEntity>(tableService.findById(tableId), HttpStatus.OK); 262 public ResponseEntity<TableDTO> findTableById(@PathVariable Long tableId) { 263 TableDTO tableDTO = tableService.findById(tableId); 264 return new ResponseEntity<>(tableDTO, HttpStatus.OK); 248 265 } 249 266 … … 255 272 return new ResponseEntity<>(reservations, HttpStatus.OK); 256 273 } 257 258 274 }
Note:
See TracChangeset
for help on using the changeset viewer.