| | 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 | === |