| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import jakarta.validation.Valid; | |
| 4 | ||
| 5 | import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | import org.springframework.http.HttpStatus; | |
| 7 | import org.springframework.http.ResponseEntity; | |
| 8 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 10 | import org.springframework.web.bind.annotation.GetMapping; | |
| 11 | import org.springframework.web.bind.annotation.PostMapping; | |
| 12 | import org.springframework.web.bind.annotation.PutMapping; | |
| 13 | import org.springframework.web.bind.annotation.RequestBody; | |
| 14 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestParam; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | ||
| 18 | import org.springframework.web.server.ResponseStatusException; | |
| 19 | ||
| 20 | import edu.ucsb.cs156.gauchoride.entities.User; | |
| 21 | import edu.ucsb.cs156.gauchoride.models.CurrentUser; | |
| 22 | import edu.ucsb.cs156.gauchoride.services.CurrentUserService; | |
| 23 | ||
| 24 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 25 | import edu.ucsb.cs156.gauchoride.entities.DriverAvailability; | |
| 26 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 27 | import edu.ucsb.cs156.gauchoride.repositories.DriverAvailabilityRepository; | |
| 28 | import io.swagger.v3.oas.annotations.Operation; | |
| 29 | import io.swagger.v3.oas.annotations.Parameter; | |
| 30 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 31 | import lombok.extern.slf4j.Slf4j; | |
| 32 | ||
| 33 | @Tag(name = "DriverAvailability") | |
| 34 | @RequestMapping("/api/driverAvailability") | |
| 35 | @RestController | |
| 36 | @Slf4j | |
| 37 | public class DriverAvailabilityController extends ApiController{ | |
| 38 |      | |
| 39 |     @Autowired | |
| 40 |     DriverAvailabilityRepository driverAvailabilityRepository; | |
| 41 | ||
| 42 |     //Allows driver to create and post new availability | |
| 43 |     @Operation(summary= "Create a new driver availability") | |
| 44 |     @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 45 |     @PostMapping("/new") | |
| 46 |     public DriverAvailability postDriverAvailability( | |
| 47 |             @Parameter(name="day") @RequestParam String day, | |
| 48 |             @Parameter(name="startTime") @RequestParam String startTime, | |
| 49 |             @Parameter(name="endTime") @RequestParam String endTime, | |
| 50 |             @Parameter(name="notes") @RequestParam String notes) | |
| 51 |             throws JsonProcessingException { | |
| 52 | ||
| 53 | ||
| 54 |         log.info("notes={}", notes); | |
| 55 | ||
| 56 |         Long UserId = getCurrentUser().getUser().getId(); | |
| 57 | ||
| 58 |         DriverAvailability driverAvailability = new DriverAvailability(); | |
| 59 | 
1
1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDriverId → KILLED | 
        driverAvailability.setDriverId(UserId); | 
| 60 | 
1
1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED | 
        driverAvailability.setDay(day); | 
| 61 | 
1
1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED | 
        driverAvailability.setStartTime(startTime); | 
| 62 | 
1
1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED | 
        driverAvailability.setEndTime(endTime); | 
| 63 | 
1
1. postDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED | 
        driverAvailability.setNotes(notes); | 
| 64 | ||
| 65 |         DriverAvailability savedDriverAvailability = driverAvailabilityRepository.save(driverAvailability); | |
| 66 | ||
| 67 | 
1
1. postDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::postDriverAvailability → KILLED | 
        return savedDriverAvailability; | 
| 68 |     } | |
| 69 | ||
| 70 |     //GET all availability submissions for the current user. | |
| 71 |     @Operation(summary = "Get all driver availabilites owned by the current user") | |
| 72 |     @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 73 |     @GetMapping("") | |
| 74 |     public Iterable<DriverAvailability> allApplications() { | |
| 75 |         Iterable<DriverAvailability> availabilities; | |
| 76 |         availabilities = driverAvailabilityRepository.findAllByDriverId(getCurrentUser().getUser().getId()); | |
| 77 | 
1
1. allApplications : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allApplications → KILLED | 
        return availabilities; | 
| 78 |     } | |
| 79 | ||
| 80 |     //GET a single availability by ID if owned by the current user | |
| 81 |     @Operation(summary = "Get a single availability but only if owned by the current user") | |
| 82 |     @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 83 |     @GetMapping("id") | |
| 84 |     public DriverAvailability getById( | |
| 85 |                     @Parameter(name="id", description = "Long, Id of the driver availability to get",  | |
| 86 |                     required = true)   | |
| 87 |                     @RequestParam Long id)  | |
| 88 |     { | |
| 89 |         DriverAvailability availability = driverAvailabilityRepository.findById(id) | |
| 90 | 
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$getById$0 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); | 
| 91 | ||
| 92 | 
1
1. getById : negated conditional → KILLED | 
        if (availability.getDriverId() != getCurrentUser().getUser().getId()){ | 
| 93 |             throw new ResponseStatusException(HttpStatus.FORBIDDEN, "You are not authorized to access this resource."); | |
| 94 |         } | |
| 95 |          | |
| 96 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::getById → KILLED | 
        return availability; | 
| 97 |     } | |
| 98 | ||
| 99 |     //Edits an availability if owned by current user | |
| 100 |     @Operation(summary = "Edit an existing driver availability but only if it is owned by the current user") | |
| 101 |     @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 102 |     @PutMapping("") | |
| 103 |     public ResponseEntity<Object> updateDriverAvailability( | |
| 104 |                             @Parameter(name="id", description="long, Id of the driver availability to be edited",  | |
| 105 |                             required = true) | |
| 106 |                             @RequestParam Long id, | |
| 107 |                             @RequestBody @Valid DriverAvailability incoming) | |
| 108 |     { | |
| 109 |         DriverAvailability availability = driverAvailabilityRepository.findById(id) | |
| 110 | 
1
1. lambda$updateDriverAvailability$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$updateDriverAvailability$1 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); | 
| 111 | ||
| 112 | 
1
1. updateDriverAvailability : negated conditional → KILLED | 
        if (availability.getDriverId() != getCurrentUser().getUser().getId()) { | 
| 113 |             throw new ResponseStatusException(HttpStatus.FORBIDDEN, "You are not authorized to access this resource."); | |
| 114 |         } | |
| 115 | ||
| 116 | 
1
1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED | 
        availability.setDay(incoming.getDay()); | 
| 117 | 
1
1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED | 
        availability.setStartTime(incoming.getStartTime()); | 
| 118 | 
1
1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED | 
        availability.setEndTime(incoming.getEndTime()); | 
| 119 | 
1
1. updateDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED | 
        availability.setNotes(incoming.getNotes()); | 
| 120 | ||
| 121 |         driverAvailabilityRepository.save(availability); | |
| 122 | 
1
1. updateDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateDriverAvailability → KILLED | 
        return ResponseEntity.ok(availability); | 
| 123 | ||
| 124 |     } | |
| 125 | ||
| 126 |     //DELETE for driver Availability | |
| 127 |     @Operation(summary= "Delete a driver availability") | |
| 128 |     @PreAuthorize("hasRole('ROLE_DRIVER')") | |
| 129 |     @DeleteMapping("") | |
| 130 |     public Object deleteDriverAvailability( | |
| 131 |             @Parameter(name="id") @RequestParam Long id) { | |
| 132 |         DriverAvailability availability = driverAvailabilityRepository.findById(id) | |
| 133 | 
1
1. lambda$deleteDriverAvailability$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$deleteDriverAvailability$2 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); | 
| 134 | ||
| 135 | 
1
1. deleteDriverAvailability : negated conditional → KILLED | 
        if (availability.getDriverId() != getCurrentUser().getUser().getId()) { | 
| 136 |             throw new ResponseStatusException(HttpStatus.FORBIDDEN, "You are not authorized to access this resource."); | |
| 137 |         } | |
| 138 | ||
| 139 | 
1
1. deleteDriverAvailability : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverAvailabilityRepository::delete → KILLED | 
        driverAvailabilityRepository.delete(availability); | 
| 140 | 
1
1. deleteDriverAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::deleteDriverAvailability → KILLED | 
        return genericMessage("DriverAvailability with id %s deleted".formatted(id)); | 
| 141 |     } | |
| 142 | ||
| 143 |     //Lets Admin GET a single availability by ID | |
| 144 |     @Operation(summary = "Admin can get a single availability by id") | |
| 145 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 146 |     @GetMapping("admin") | |
| 147 |     public DriverAvailability adminGetById( | |
| 148 |                     @Parameter(name="id", description = "Long, Id of the driver availability to get",  | |
| 149 |                     required = true)   | |
| 150 |                     @RequestParam Long id)  | |
| 151 |     { | |
| 152 |         DriverAvailability availability = driverAvailabilityRepository.findById(id) | |
| 153 | 
1
1. lambda$adminGetById$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$adminGetById$3 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); | 
| 154 | 
1
1. adminGetById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::adminGetById → KILLED | 
        return availability; | 
| 155 |     } | |
| 156 | ||
| 157 |     //Lets admins get all driver availabilties | |
| 158 |     @Operation(summary= "List all driver availabilities") | |
| 159 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 160 |     @GetMapping("/admin/all") | |
| 161 |     public Iterable<DriverAvailability> allDriverAvailabilities() { | |
| 162 |         Iterable<DriverAvailability> availabilities = driverAvailabilityRepository.findAll(); | |
| 163 | 
1
1. allDriverAvailabilities : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allDriverAvailabilities → KILLED | 
        return availabilities; | 
| 164 |     } | |
| 165 | ||
| 166 | } | |
Mutations | ||
| 59 | 
 
 1.1  | 
|
| 60 | 
 
 1.1  | 
|
| 61 | 
 
 1.1  | 
|
| 62 | 
 
 1.1  | 
|
| 63 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 77 | 
 
 1.1  | 
|
| 90 | 
 
 1.1  | 
|
| 92 | 
 
 1.1  | 
|
| 96 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  | 
|
| 112 | 
 
 1.1  | 
|
| 116 | 
 
 1.1  | 
|
| 117 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 119 | 
 
 1.1  | 
|
| 122 | 
 
 1.1  | 
|
| 133 | 
 
 1.1  | 
|
| 135 | 
 
 1.1  | 
|
| 139 | 
 
 1.1  | 
|
| 140 | 
 
 1.1  | 
|
| 153 | 
 
 1.1  | 
|
| 154 | 
 
 1.1  | 
|
| 163 | 
 
 1.1  |