| 346 | == **ID 4** - View My Trips |
| 347 | The `/trips/user` endpoint allows authenticated users to view the list of trips they have booked. |
| 348 | It retrieves the trips associated with the currently logged-in user and displays them dynamically. |
| 349 | The request is handled by `UserTripController`, which interacts with `TripService` and `AuthenticationService` to fetch user-specific trip data. |
| 350 | |
| 351 | === |
| 352 | **Example URL for viewing booked trips:** |
| 353 | - `/trips/user` |
| 354 | |
| 355 | === |
| 356 | {{{ |
| 357 | @RequestMapping("/trips") |
| 358 | public class UserTripController { |
| 359 | private final AuthenticationService authenticationService; |
| 360 | private final TripService tripService; |
| 361 | |
| 362 | public UserTripController(AuthenticationService authenticationService, TripService tripService) { |
| 363 | this.authenticationService = authenticationService; |
| 364 | this.tripService = tripService; |
| 365 | } |
| 366 | |
| 367 | @GetMapping("/user") |
| 368 | public String myTripsPage(Model model) { |
| 369 | Integer currentAccountId = authenticationService.getAuthenticatedUserId(); |
| 370 | |
| 371 | model.addAttribute("trips", tripService.findTripsBookedByAccount(currentAccountId)); |
| 372 | model.addAttribute("display", "user/my-trips"); |
| 373 | |
| 374 | return "master"; |
| 375 | } |
| 376 | } |
| 377 | }}} |
| 378 | === |
| 379 | |
| 380 | ==== **Controller Details:** |
| 381 | - **URL Mapping:** `/trips/user` |
| 382 | - **Method:** GET |
| 383 | - **Functionality:** |
| 384 | - Retrieves the currently logged-in user’s booked trips using `tripService.findTripsBookedByAccount(currentAccountId)`. |
| 385 | - Passes the retrieved trips to the model under the key `"trips"`. |
| 386 | - Uses the `"master"` template and embeds `"user/my-trips"` dynamically for rendering. |
| 387 | |
| 388 | ==== **Breakdown:** |
| 389 | - When a user accesses `/trips/user`, the controller first retrieves the authenticated user's ID using `authenticationService.getAuthenticatedUserId()`. |
| 390 | - It then fetches all trips booked by the user from `tripService.findTripsBookedByAccount(currentAccountId)`. |
| 391 | - The retrieved trips are added to the model, ensuring they can be displayed on the front end. |
| 392 | |
| 393 | === |
| 394 | ==== **Result of /trips/user** |
| 395 | [[Image(my-trips.png, 100%)]] |
| 396 | === |
| 397 | |
| 398 | |