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