| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.gauchoride.entities.RiderApplication; | |
| 4 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.gauchoride.repositories.RiderApplicationRepository; | |
| 6 | ||
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | ||
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.http.ResponseEntity; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
| 15 | import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder.SecretKeyReactiveJwtDecoderBuilder; | |
| 16 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 17 | import org.springframework.web.bind.annotation.GetMapping; | |
| 18 | import org.springframework.web.bind.annotation.PostMapping; | |
| 19 | import org.springframework.web.bind.annotation.PutMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestBody; | |
| 21 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 22 | import org.springframework.web.bind.annotation.RequestParam; | |
| 23 | import org.springframework.web.bind.annotation.RestController; | |
| 24 | ||
| 25 | import javax.validation.Valid; | |
| 26 | ||
| 27 | import java.sql.Date; | |
| 28 | import java.time.LocalDate; | |
| 29 | import java.util.ArrayList; | |
| 30 | import java.util.List; | |
| 31 | ||
| 32 | ||
| 33 | @Tag(name = "Rider Application") | |
| 34 | @RequestMapping("/api") | |
| 35 | @RestController | |
| 36 | ||
| 37 | public class RiderApplicationController extends ApiController { | |
| 38 | ||
| 39 | @Autowired | |
| 40 | RiderApplicationRepository riderApplicationRepository; | |
| 41 | | |
| 42 | ||
| 43 | // // Endpoints for ROLE_MEMBER | |
| 44 | ||
| 45 | @Operation(summary = "Create a new rider application with the current user as the requester") | |
| 46 | @PreAuthorize("hasRole('ROLE_MEMBER')") | |
| 47 | @PostMapping("/riderApplication/new") | |
| 48 | public RiderApplication postRiderApplication( | |
| 49 | @Parameter(name="perm_number", description="String, Perm number consisting of 7 characters", example = "1234567", required = true) | |
| 50 | @RequestParam String perm_number, | |
| 51 | ||
| 52 | @Parameter(name="description", description="String, Please describe the mobility limitations that cause you to need to use the Gauchoride service. ", example = "My legs are broken", required = true) | |
| 53 | @RequestParam String description | |
| 54 | ) | |
| 55 | { | |
| 56 | RiderApplication riderApplication = new RiderApplication(); | |
| 57 | // Get the current date | |
| 58 | LocalDate localDate = LocalDate.now(); | |
| 59 | Date currentDate = Date.valueOf(localDate); | |
| 60 | ||
| 61 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setStatus → KILLED |
riderApplication.setStatus("pending"); |
| 62 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setUserId → KILLED |
riderApplication.setUserId(getCurrentUser().getUser().getId()); |
| 63 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setEmail → KILLED |
riderApplication.setEmail(getCurrentUser().getUser().getEmail()); |
| 64 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setPerm_number → KILLED |
riderApplication.setPerm_number(perm_number); |
| 65 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setCreated_date → KILLED |
riderApplication.setCreated_date(currentDate); |
| 66 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setUpdated_date → KILLED |
riderApplication.setUpdated_date(currentDate); |
| 67 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setDescription → KILLED |
riderApplication.setDescription(description); |
| 68 |
1
1. postRiderApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setNotes → KILLED |
riderApplication.setNotes(""); |
| 69 | ||
| 70 | RiderApplication savedApplication = riderApplicationRepository.save(riderApplication); | |
| 71 |
1
1. postRiderApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::postRiderApplication → KILLED |
return savedApplication; |
| 72 | }; | |
| 73 | ||
| 74 | @Operation(summary = "Get all rider applications owned by the current user") | |
| 75 | @PreAuthorize("hasRole('ROLE_MEMBER')") | |
| 76 | @GetMapping("/rider") | |
| 77 | public Iterable<RiderApplication> allApplications() | |
| 78 | { | |
| 79 | Iterable<RiderApplication> applications; | |
| 80 | applications = riderApplicationRepository.findAllByUserId(getCurrentUser().getUser().getId()); | |
| 81 |
1
1. allApplications : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::allApplications → KILLED |
return applications; |
| 82 | }; | |
| 83 | ||
| 84 | @Operation(summary = "Get a single rider application but only if owned by the current user") | |
| 85 | @PreAuthorize("hasRole('ROLE_MEMBER')") | |
| 86 | @GetMapping("/riderApplication") | |
| 87 | public RiderApplication getById( | |
| 88 | @Parameter(name="id", description = "Long, Id of the RiderApplication to get", | |
| 89 | required = true) | |
| 90 | @RequestParam Long id) | |
| 91 | { | |
| 92 | RiderApplication application; | |
| 93 | application = riderApplicationRepository.findByIdAndUserId(id, getCurrentUser().getUser().getId()) | |
| 94 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RiderApplication.class, id)); |
| 95 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::getById → KILLED |
return application; |
| 96 | }; | |
| 97 | ||
| 98 | @Operation(summary = "Edit an existing rider application but only if it is owned by the current user and the application is in the correct status") | |
| 99 | @PreAuthorize("hasRole('ROLE_MEMBER')") | |
| 100 | @PutMapping("/riderApplication") | |
| 101 | public ResponseEntity<Object> updateApplication( | |
| 102 | @Parameter(name="id", description="long, Id of the Application to be edited", | |
| 103 | required = true) | |
| 104 | @RequestParam Long id, | |
| 105 | @RequestBody @Valid RiderApplication incoming) | |
| 106 | { | |
| 107 | RiderApplication application; | |
| 108 | ||
| 109 | application = riderApplicationRepository.findByIdAndUserId(id, getCurrentUser().getUser().getId()) | |
| 110 |
1
1. lambda$updateApplication$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::lambda$updateApplication$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RiderApplication.class, id)); |
| 111 | ||
| 112 |
1
1. updateApplication : negated conditional → KILLED |
if ("pending".equals(application.getStatus())) |
| 113 | { | |
| 114 | // Get the current date | |
| 115 | LocalDate localDate = LocalDate.now(); | |
| 116 | Date currentDate = Date.valueOf(localDate); | |
| 117 | ||
| 118 |
1
1. updateApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setPerm_number → KILLED |
application.setPerm_number(incoming.getPerm_number()); |
| 119 |
1
1. updateApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setUpdated_date → KILLED |
application.setUpdated_date(currentDate); |
| 120 |
1
1. updateApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setDescription → KILLED |
application.setDescription(incoming.getDescription()); |
| 121 | ||
| 122 | riderApplicationRepository.save(application); | |
| 123 |
1
1. updateApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::updateApplication → KILLED |
return ResponseEntity.ok(application); |
| 124 | } | |
| 125 | else | |
| 126 | { | |
| 127 | String errorMessage = "RiderApplication with \"" + application.getStatus() + "\" status cannot be updated"; | |
| 128 |
1
1. updateApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::updateApplication → KILLED |
return ResponseEntity.badRequest().body(errorMessage); |
| 129 | } | |
| 130 | }; | |
| 131 | ||
| 132 | @Operation(summary = "Cancel an existing rider application but only if it is owned by the current user and the application is in the correct status") | |
| 133 | @PreAuthorize("hasRole('ROLE_MEMBER')") | |
| 134 | @PutMapping("/riderApplication/cancel") | |
| 135 | public Object cancelApplication( | |
| 136 | @Parameter(name="id", description="long, Id of the Application to be edited", | |
| 137 | required = true) | |
| 138 | @RequestParam Long id) | |
| 139 | | |
| 140 | { | |
| 141 | RiderApplication application; | |
| 142 | ||
| 143 | application = riderApplicationRepository.findByIdAndUserId(id, getCurrentUser().getUser().getId()) | |
| 144 |
1
1. lambda$cancelApplication$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::lambda$cancelApplication$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RiderApplication.class, id)); |
| 145 | | |
| 146 |
1
1. cancelApplication : negated conditional → KILLED |
if ("pending".equals(application.getStatus())) |
| 147 | { | |
| 148 | // Get the current date | |
| 149 | LocalDate localDate = LocalDate.now(); | |
| 150 | Date currentDate = Date.valueOf(localDate); | |
| 151 | ||
| 152 |
1
1. cancelApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setStatus → KILLED |
application.setStatus("cancelled"); |
| 153 |
1
1. cancelApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setUpdated_date → KILLED |
application.setUpdated_date(currentDate); |
| 154 |
1
1. cancelApplication : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setCancelled_date → KILLED |
application.setCancelled_date(currentDate); |
| 155 | riderApplicationRepository.save(application); | |
| 156 | ||
| 157 |
1
1. cancelApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::cancelApplication → KILLED |
return genericMessage("Application with id %s is deleted".formatted(id)); |
| 158 | } | |
| 159 | else | |
| 160 | { | |
| 161 |
1
1. cancelApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::cancelApplication → KILLED |
return genericMessage("Application with \"%s\" status cannot be cancelled".formatted(application.getStatus())); |
| 162 | } | |
| 163 | }; | |
| 164 | ||
| 165 | ||
| 166 | // // Endpoints for ROLE_ADMIN | |
| 167 | ||
| 168 | @Operation(summary = "Get all rider applications") | |
| 169 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 170 | @GetMapping("/rider/admin/all") | |
| 171 | public Iterable<RiderApplication> allApplicationsAdmin() | |
| 172 | { | |
| 173 | Iterable<RiderApplication> applications; | |
| 174 | applications = riderApplicationRepository.findAll(); | |
| 175 |
1
1. allApplicationsAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::allApplicationsAdmin → KILLED |
return applications; |
| 176 | }; | |
| 177 | ||
| 178 | @Operation(summary = "Get all pending rider applications") | |
| 179 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 180 | @GetMapping("/rider/admin/pending") | |
| 181 | public Iterable<RiderApplication> allPendingApplications() | |
| 182 | { | |
| 183 | Iterable<RiderApplication> pendingApplications; | |
| 184 | pendingApplications = riderApplicationRepository.findAllByStatus("pending"); | |
| 185 | ||
| 186 |
1
1. allPendingApplications : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::allPendingApplications → KILLED |
return pendingApplications; |
| 187 | }; | |
| 188 | ||
| 189 | @Operation(summary = "Get a specific rider application") | |
| 190 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 191 | @GetMapping("/rider/admin") | |
| 192 | public RiderApplication specificApplication( | |
| 193 | @Parameter(name="id", description="long, Id of the Application to find", | |
| 194 | required = true) | |
| 195 | @RequestParam Long id) | |
| 196 | { | |
| 197 | RiderApplication application; | |
| 198 | application = riderApplicationRepository.findById(id) | |
| 199 |
1
1. lambda$specificApplication$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::lambda$specificApplication$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RiderApplication.class, id)); |
| 200 |
1
1. specificApplication : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::specificApplication → KILLED |
return application; |
| 201 | }; | |
| 202 | ||
| 203 | @Operation(summary = "Update the status/notes field of an application") | |
| 204 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 205 | @PutMapping("/rider/admin") | |
| 206 | public RiderApplication updateApplicationAdmin( | |
| 207 | @Parameter(name="id", description="long, Id of the Application to be updated", | |
| 208 | required = true) | |
| 209 | @RequestParam Long id, | |
| 210 | ||
| 211 | @Parameter(name="status", description="String, New Status of the Application", | |
| 212 | required = false) | |
| 213 | @RequestParam String status, | |
| 214 | ||
| 215 | @Parameter(name="notes", description="String, Notes to notify the Applicant", | |
| 216 | required = false) | |
| 217 | @RequestParam String notes) | |
| 218 | { | |
| 219 | RiderApplication application; | |
| 220 | application = riderApplicationRepository.findById(id) | |
| 221 |
1
1. lambda$updateApplicationAdmin$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::lambda$updateApplicationAdmin$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(RiderApplication.class, id)); |
| 222 | ||
| 223 |
1
1. updateApplicationAdmin : negated conditional → KILLED |
if (!status.isEmpty()) |
| 224 | { | |
| 225 |
1
1. updateApplicationAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setStatus → KILLED |
application.setStatus(status); |
| 226 | } | |
| 227 | ||
| 228 |
1
1. updateApplicationAdmin : negated conditional → KILLED |
if (!notes.isEmpty()) |
| 229 | { | |
| 230 |
1
1. updateApplicationAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/RiderApplication::setNotes → KILLED |
application.setNotes(notes); |
| 231 | } | |
| 232 | | |
| 233 | riderApplicationRepository.save(application); | |
| 234 |
1
1. updateApplicationAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/RiderApplicationController::updateApplicationAdmin → KILLED |
return application; |
| 235 | }; | |
| 236 | ||
| 237 | } | |
Mutations | ||
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 71 |
1.1 |
|
| 81 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 120 |
1.1 |
|
| 123 |
1.1 |
|
| 128 |
1.1 |
|
| 144 |
1.1 |
|
| 146 |
1.1 |
|
| 152 |
1.1 |
|
| 153 |
1.1 |
|
| 154 |
1.1 |
|
| 157 |
1.1 |
|
| 161 |
1.1 |
|
| 175 |
1.1 |
|
| 186 |
1.1 |
|
| 199 |
1.1 |
|
| 200 |
1.1 |
|
| 221 |
1.1 |
|
| 223 |
1.1 |
|
| 225 |
1.1 |
|
| 228 |
1.1 |
|
| 230 |
1.1 |
|
| 234 |
1.1 |