| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import edu.ucsb.cs156.gauchoride.entities.Ride; | |
| 7 | import edu.ucsb.cs156.gauchoride.entities.Shift; | |
| 8 | import edu.ucsb.cs156.gauchoride.repositories.ShiftRepository; | |
| 9 | import edu.ucsb.cs156.gauchoride.repositories.UserRepository; | |
| 10 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 11 | import edu.ucsb.cs156.gauchoride.models.CurrentUser; | |
| 12 | ||
| 13 | import java.nio.file.AccessDeniedException; | |
| 14 | import java.time.LocalTime; | |
| 15 | ||
| 16 | import javax.validation.Valid; | |
| 17 | ||
| 18 | import org.springframework.beans.factory.annotation.Autowired; | |
| 19 | import org.springframework.http.ResponseEntity; | |
| 20 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 21 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
| 22 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 23 | import org.springframework.web.bind.annotation.GetMapping; | |
| 24 | import org.springframework.web.bind.annotation.PostMapping; | |
| 25 | import org.springframework.web.bind.annotation.PutMapping; | |
| 26 | import org.springframework.web.bind.annotation.RequestBody; | |
| 27 | import org.springframework.web.bind.annotation.PathVariable; | |
| 28 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 29 | import org.springframework.web.bind.annotation.RequestParam; | |
| 30 | import org.springframework.web.bind.annotation.RestController; | |
| 31 | ||
| 32 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 33 | import io.swagger.v3.oas.annotations.Operation; | |
| 34 | import io.swagger.v3.oas.annotations.Parameter; | |
| 35 | ||
| 36 | ||
| 37 | @Tag(name = "Shift information") | |
| 38 | @RequestMapping("/api/shift") | |
| 39 | @RestController | |
| 40 | public class ShiftController extends ApiController { | |
| 41 | @Autowired | |
| 42 | ShiftRepository shiftRepository; | |
| 43 | ||
| 44 | @Autowired | |
| 45 | ObjectMapper mapper; | |
| 46 | ||
| 47 | @Operation(summary = "Get a list of all shifts") | |
| 48 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_RIDER')") | |
| 49 | @GetMapping("/all") | |
| 50 | public ResponseEntity<String> allShifts() | |
| 51 | throws JsonProcessingException { | |
| 52 | Iterable<Shift> shifts = shiftRepository.findAll(); | |
| 53 | String body = mapper.writeValueAsString(shifts); | |
| 54 |
1
1. allShifts : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::allShifts → KILLED |
return ResponseEntity.ok().body(body); |
| 55 | } | |
| 56 | ||
| 57 | @Operation(summary = "Get shift by id") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER') || hasRole('ROLE_RIDER')") | |
| 59 | @GetMapping("/get") | |
| 60 | public Shift shiftByID( | |
| 61 | @Parameter(name = "id", description = "Long, id number of shift to get", example = "1", required = true) @RequestParam Long id) | |
| 62 | throws JsonProcessingException { | |
| 63 | Shift shift = shiftRepository.findById(id) | |
| 64 |
1
1. lambda$shiftByID$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$shiftByID$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Shift.class, id)); |
| 65 |
1
1. shiftByID : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::shiftByID → KILLED |
return shift; |
| 66 | } | |
| 67 | ||
| 68 | @Operation(summary = "Get a list of all shifts for given driverId") | |
| 69 | @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 70 | @GetMapping("/drivershifts") | |
| 71 | public ResponseEntity<String> driverShifts() | |
| 72 | throws JsonProcessingException { | |
| 73 | Long driverID = super.getCurrentUser().getUser().getId(); | |
| 74 | Iterable<Shift> shifts = shiftRepository.findByDriverID(driverID); | |
| 75 | String body = mapper.writeValueAsString(shifts); | |
| 76 |
1
1. driverShifts : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::driverShifts → KILLED |
return ResponseEntity.ok().body(body); |
| 77 | } | |
| 78 | ||
| 79 | ||
| 80 | @Operation(summary = "Create a new shift") | |
| 81 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 82 | @PostMapping("/post") | |
| 83 | public Shift postShift( | |
| 84 | @Parameter(name="day", description ="String, day of the week", example = "Monday") @RequestParam String day, | |
| 85 | @Parameter(name="shiftStart", description="String, Format: HH:MM(A/P)M", example="11:00AM") @RequestParam String shiftStart, | |
| 86 | @Parameter(name="shiftEnd", description="String, Format: HH:MM(A/P)M", example="11:00AM") @RequestParam String shiftEnd, | |
| 87 | @Parameter(name="driverID", description = "Long, driver ID") @RequestParam long driverID , | |
| 88 | @Parameter(name="driverBackupID", description = "Long, driver backup id") @RequestParam long driverBackupID | |
| 89 | ) | |
| 90 | { | |
| 91 | ||
| 92 | Shift shift = new Shift(); | |
| 93 | ||
| 94 |
1
1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED |
shift.setDriverID(driverID); |
| 95 |
1
1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED |
shift.setDay(day); |
| 96 |
1
1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED |
shift.setShiftStart(shiftStart); |
| 97 |
1
1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED |
shift.setShiftEnd(shiftEnd); |
| 98 |
1
1. postShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED |
shift.setDriverBackupID(driverBackupID); |
| 99 | ||
| 100 | Shift savedShift = shiftRepository.save(shift); | |
| 101 | ||
| 102 |
1
1. postShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::postShift → KILLED |
return savedShift; |
| 103 | } | |
| 104 | ||
| 105 | ||
| 106 | ||
| 107 | @Operation(summary= "Delete a Shift") | |
| 108 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 109 | @DeleteMapping("") | |
| 110 | public Object deleteShift( | |
| 111 | @Parameter(name="id", description = "Long, id number of shift to get", example = "1", required = true) | |
| 112 | @RequestParam long id){ | |
| 113 | Shift shift = shiftRepository.findById(id) | |
| 114 |
1
1. lambda$deleteShift$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$deleteShift$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Shift.class, id)); |
| 115 | | |
| 116 |
1
1. deleteShift : removed call to edu/ucsb/cs156/gauchoride/repositories/ShiftRepository::delete → KILLED |
shiftRepository.delete(shift); |
| 117 |
1
1. deleteShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::deleteShift → KILLED |
return genericMessage("Shift with id %s deleted".formatted(id)); |
| 118 | ||
| 119 | } | |
| 120 | ||
| 121 | ||
| 122 | @Operation(summary = "Update a single shift") | |
| 123 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 124 | @PutMapping("") | |
| 125 | public Shift updateShift( | |
| 126 | @Parameter(name="id", description = "Long, id number of shift to get", example = "1", required = true) | |
| 127 | @RequestParam Long id, | |
| 128 | @RequestBody @Valid Shift incoming) { | |
| 129 | ||
| 130 | Shift shift = shiftRepository.findById(id) | |
| 131 |
1
1. lambda$updateShift$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::lambda$updateShift$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Shift.class, id)); |
| 132 | ||
| 133 | | |
| 134 |
1
1. updateShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDay → KILLED |
shift.setDay(incoming.getDay()); |
| 135 |
1
1. updateShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftStart → KILLED |
shift.setShiftStart(incoming.getShiftStart()); |
| 136 |
1
1. updateShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setShiftEnd → KILLED |
shift.setShiftEnd(incoming.getShiftEnd()); |
| 137 |
1
1. updateShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverID → KILLED |
shift.setDriverID(incoming.getDriverID()); |
| 138 |
1
1. updateShift : removed call to edu/ucsb/cs156/gauchoride/entities/Shift::setDriverBackupID → KILLED |
shift.setDriverBackupID(incoming.getDriverBackupID()); |
| 139 | ||
| 140 | shiftRepository.save(shift); | |
| 141 | ||
| 142 |
1
1. updateShift : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ShiftController::updateShift → KILLED |
return shift; |
| 143 | } | |
| 144 | ||
| 145 | } | |
Mutations | ||
| 54 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 76 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 102 |
1.1 |
|
| 114 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 131 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |
|
| 136 |
1.1 |
|
| 137 |
1.1 |
|
| 138 |
1.1 |
|
| 142 |
1.1 |