Changes between Version 9 and Version 10 of UseCaseImplementation


Ignore:
Timestamp:
02/17/25 13:37:39 (5 days ago)
Author:
222077
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UseCaseImplementation

    v9 v10  
    6868
    6969===
     70=== **ID 2** - Organize Trip
     71The `/routes` endpoint provides transport organizers with the ability to manage their routes and organize trips.
     72It supports **viewing authorized routes**, **viewing trips for a specific route**, and **adding new trips**.
     73The request is handled by `CompanyRouteController` and `CompanyTripController`, which interact with `CompanyRouteService` and `CompanyTripService` to manage routes and trips.
    7074
     75Here is an example of the flow to organize a trip:
     76- View routes: `/routes/company`
     77- View trips for a specific route: `/routes/company/view-trips/{routeId}`
     78- Add a new trip: `/routes/company/view-trips/{routeId}/add-trip`
    7179
     80===
     81{{{
     82    @RequestMapping("/routes")
     83    public class CompanyRouteController {
     84        private final CompanyRouteService companyRouteService;
    7285
     86        public CompanyRouteController(CompanyRouteService companyRouteService) {
     87            this.companyRouteService = companyRouteService;
     88        }
     89
     90        @GetMapping("/company")
     91        public String routes(Model model) {
     92            model.addAttribute("companyRoutes", companyRouteService.getAuthorizedRoutes());
     93            model.addAttribute("display", "/company/company-route");
     94
     95            return "master";
     96        }
     97    }
     98}}}
     99
     100===
     101{{{
     102    @RequestMapping("/routes/company/view-trips/{routeId}")
     103    public class CompanyTripController {
     104        private final CompanyTripService companyTripService;
     105        private final RouteService routeService;
     106        private final LocationService locationService;
     107
     108        public CompanyTripController(CompanyTripService companyTripService, RouteService routeService, LocationService locationService) {
     109            this.companyTripService = companyTripService;
     110            this.routeService = routeService;
     111            this.locationService = locationService;
     112        }
     113
     114        @GetMapping
     115        public String routeTrips(@PathVariable Integer routeId, Model model) {
     116            Route route = routeService.findById(routeId);
     117
     118            model.addAttribute("trips", companyTripService.getAuthorizedTripsByRoute(routeId));
     119            model.addAttribute("routeId", routeId);
     120            model.addAttribute("locations", locationService.findAll());
     121            model.addAttribute("routeSource", route.getSource());
     122            model.addAttribute("routeDestination", route.getDestination());
     123            model.addAttribute("display", "/company/company-view-trip");
     124
     125            return "master";
     126        }
     127
     128        @PostMapping("/add-trip")
     129        public String addNewTrip(@PathVariable Integer routeId,
     130                                 @RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
     131                                 @RequestParam("freeSeats") int freeSeats,
     132                                 @RequestParam("locations") List<Integer> locationIds,
     133                                 @RequestParam("etas") @DateTimeFormat(pattern = "HH:mm") List<LocalTime> etas,
     134                                 RedirectAttributes redirectAttributes) {
     135
     136            try {
     137                Route route = routeService.findById(routeId);
     138                companyTripService.createTrip(route, date, freeSeats, locationIds, etas);
     139                redirectAttributes.addFlashAttribute("message", "Trip created successfully!");
     140            } catch (IllegalArgumentException | SecurityException e) {
     141                redirectAttributes.addFlashAttribute("error", e.getMessage());
     142            }
     143            return "redirect:/routes/company/view-trips/" + routeId;
     144        }
     145    }
     146}}}
     147
     148===
     149==== Controller Details:
     150- **URL Mapping:** `/routes`
     151- **Method:** `GET`, `POST`
     152- **Functionality:**
     153  - **GET `/routes/company`**: Displays **authorized routes** for the transport organizer.
     154  - **GET `/routes/company/view-trips/{routeId}`**: Displays all **authorized trips** for the selected route.
     155  - **POST `/routes/company/view-trips/{routeId}/add-trip`**: Allows the transport organizer to **add a new trip** to an existing route.
     156- **View:** Uses the `master` template and dynamically embeds `/company/company-route` and `/company/company-view-trip`.
     157
     158==== Breakdown:
     159- When the transport organizer accesses `/routes/company`, the controller retrieves all authorized routes using `companyRouteService.getAuthorizedRoutes()` and displays them.
     160- When the transport organizer accesses `/routes/company/view-trips/{routeId}`, the controller retrieves all authorized trips for the specified route using `companyTripService.getAuthorizedTripsByRoute(routeId)` and displays them along with the locations.
     161{{{
     162    /**
     163     * Returns every trip for specific route if user is authorized.
     164     *
     165     * @param routeId the routeId for getting authorized trips
     166     * @return all trips for the route || SecurityException("Unauthorized to access these trips.")
     167     */
     168    public List<Trip> getAuthorizedTripsByRoute(Integer routeId) {
     169        Integer transportOrganizerId = authorizationService.getAuthenticatedTransportOrganizerId();
     170
     171        List<Trip> trips = tripService.findAllByPredicate(TripSpecification.tripsByRoute(routeId));
     172
     173        if (!trips.isEmpty() && !trips.get(0).getTranOrg().getTranOrgId().equals(transportOrganizerId)) {
     174            throw new SecurityException("Unauthorized to access these trips.");
     175        }
     176        return trips;
     177    }
     178
     179}}}
     180- The transport organizer can add a new trip by submitting the form with details like date, free seats, locations, and estimated times of arrival (ETAs).
     181
     182===
     183==== Result of `/routes/company`
     184[[Image(company-routes-view.png, 100%)]]
     185===
     186
     187==== Result of `/routes/company/view-trips/{routeId}`
     188[[Image(company-trips-view.png, 100%)]]
     189===
     190
     191=== Result of `/routes/company/view-trips/{routeId}/add-trip`
     192[[Image(add-trip-modal.png, 100%)]]
     193
     194[[Image(add-trip-success.png, 100%)]]
     195===