1 | package edu.ucsb.cs156.gauchoride.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.gauchoride.entities.DriverAvailability; | |
4 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
5 | import edu.ucsb.cs156.gauchoride.repositories.DriverAvailabilityRepository; | |
6 | import io.swagger.v3.oas.annotations.tags.Tag; | |
7 | import io.swagger.v3.oas.annotations.Operation; | |
8 | import io.swagger.v3.oas.annotations.Parameter; | |
9 | ||
10 | import org.springframework.beans.factory.annotation.Autowired; | |
11 | import org.springframework.security.access.prepost.PreAuthorize; | |
12 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
13 | import org.springframework.web.bind.annotation.DeleteMapping; | |
14 | import org.springframework.web.bind.annotation.GetMapping; | |
15 | import org.springframework.web.bind.annotation.PostMapping; | |
16 | import org.springframework.web.bind.annotation.PutMapping; | |
17 | import org.springframework.web.bind.annotation.RequestBody; | |
18 | import org.springframework.web.bind.annotation.RequestMapping; | |
19 | import org.springframework.web.bind.annotation.RequestParam; | |
20 | import org.springframework.web.bind.annotation.RestController; | |
21 | ||
22 | import javax.validation.Valid; | |
23 | ||
24 | @Tag(name = "Driver Availability") | |
25 | @RequestMapping("/api/driverAvailability") | |
26 | @RestController | |
27 | ||
28 | public class DriverAvailabilityController extends ApiController{ | |
29 | | |
30 | @Autowired | |
31 | DriverAvailabilityRepository driverAvailabilityRepository; | |
32 | ||
33 | @Operation(summary = "List all availabilities, only current user's if not admin") | |
34 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER')") | |
35 | @GetMapping("/all") | |
36 | public Iterable<DriverAvailability> allAvailabilities() { | |
37 | Iterable<DriverAvailability> availabilities; | |
38 | ||
39 |
1
1. allAvailabilities : negated conditional → KILLED |
if (getCurrentUser().getRoles().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) { |
40 | availabilities = driverAvailabilityRepository.findAll(); | |
41 | } else { | |
42 | availabilities = driverAvailabilityRepository.findAllByDriverId(getCurrentUser().getUser().getId()); | |
43 | } | |
44 | ||
45 |
1
1. allAvailabilities : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::allAvailabilities → KILLED |
return availabilities; |
46 | } | |
47 | ||
48 | @Operation(summary = "Get a single availability by id, can only be current user's if not admin") | |
49 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER')") | |
50 | @GetMapping("") | |
51 | public DriverAvailability getById( | |
52 | @Parameter(name="id", description = "long, Id of the availability to get", | |
53 | required = true) | |
54 | @RequestParam Long id) { | |
55 | DriverAvailability availability; | |
56 | | |
57 |
1
1. getById : negated conditional → KILLED |
if (getCurrentUser().getRoles().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) { |
58 | availability = driverAvailabilityRepository.findById(id) | |
59 |
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));; |
60 | } else { | |
61 | availability = driverAvailabilityRepository.findByIdAndDriverId(id, getCurrentUser().getUser().getId()) | |
62 |
1
1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$getById$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); |
63 | } | |
64 | ||
65 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::getById → KILLED |
return availability; |
66 | } | |
67 | ||
68 | @Operation(summary = "Create a new driver availability") | |
69 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER')") | |
70 | @PostMapping("/new") | |
71 | public DriverAvailability postAvailability( | |
72 | @Parameter(name="day", description="String, Day of the week driver is available (Monday - Sunday) and allows Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday", | |
73 | example="Tuesday", required = true) | |
74 | @RequestParam String day, | |
75 | ||
76 | @Parameter(name="startTime", description="String, Time the availability starts HH:MM(A/P)M", example="12:30AM", required = true) | |
77 | @RequestParam String startTime, | |
78 | ||
79 | @Parameter(name="endTime", description="String, Time the availability ends HH:MM(A/P)M", example="12:30AM", required = true) | |
80 | @RequestParam String endTime, | |
81 | ||
82 | @Parameter(name="notes", description="String, Extra information", example="Have to stay near the city", required = true) | |
83 | @RequestParam String notes | |
84 | ) | |
85 | { | |
86 | ||
87 | DriverAvailability availability = new DriverAvailability(); | |
88 | | |
89 |
1
1. postAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDriverId → KILLED |
availability.setDriverId(getCurrentUser().getUser().getId()); |
90 |
1
1. postAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED |
availability.setDay(day); |
91 |
1
1. postAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED |
availability.setStartTime(startTime); |
92 |
1
1. postAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED |
availability.setEndTime(endTime); |
93 |
1
1. postAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED |
availability.setNotes(notes); |
94 | ||
95 | DriverAvailability savedAvailability = driverAvailabilityRepository.save(availability); | |
96 | ||
97 |
1
1. postAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::postAvailability → KILLED |
return savedAvailability; |
98 | } | |
99 | ||
100 | @Operation(summary = "Delete an availability if admin or owned by current user") | |
101 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER')") | |
102 | @DeleteMapping("") | |
103 | public Object deleteAvailability( | |
104 | @Parameter(name="id", description="long, Id of the availability to be deleted", | |
105 | required = true) | |
106 | @RequestParam Long id) { | |
107 | ||
108 | DriverAvailability availability; | |
109 | ||
110 |
1
1. deleteAvailability : negated conditional → KILLED |
if (getCurrentUser().getRoles().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) { |
111 | availability = driverAvailabilityRepository.findById(id) | |
112 |
1
1. lambda$deleteAvailability$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$deleteAvailability$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));; |
113 | } else { | |
114 | availability = driverAvailabilityRepository.findByIdAndDriverId(id, getCurrentUser().getUser().getId()) | |
115 |
1
1. lambda$deleteAvailability$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$deleteAvailability$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); |
116 | } | |
117 | ||
118 |
1
1. deleteAvailability : removed call to edu/ucsb/cs156/gauchoride/repositories/DriverAvailabilityRepository::delete → KILLED |
driverAvailabilityRepository.delete(availability); |
119 |
1
1. deleteAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::deleteAvailability → KILLED |
return genericMessage("Availability with id %s deleted".formatted(id)); |
120 | } | |
121 | ||
122 | ||
123 | @Operation(summary = "Edits an availability if admin or owned by current user") | |
124 | @PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_DRIVER')") | |
125 | @PutMapping("") | |
126 | public DriverAvailability updateAvailability( | |
127 | @Parameter(name="id", description="long, Id of the Availability to be edited", | |
128 | required = true) | |
129 | @RequestParam Long id, | |
130 | @RequestBody @Valid DriverAvailability incoming) { | |
131 | ||
132 | DriverAvailability availability; | |
133 | ||
134 |
1
1. updateAvailability : negated conditional → KILLED |
if (getCurrentUser().getRoles().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) { |
135 | availability = driverAvailabilityRepository.findById(id) | |
136 |
1
1. lambda$updateAvailability$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$updateAvailability$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id));; |
137 | } else { | |
138 | availability = driverAvailabilityRepository.findByIdAndDriverId(id, getCurrentUser().getUser().getId()) | |
139 |
1
1. lambda$updateAvailability$5 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::lambda$updateAvailability$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(DriverAvailability.class, id)); |
140 | } | |
141 | ||
142 |
1
1. updateAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setDay → KILLED |
availability.setDay(incoming.getDay()); |
143 |
1
1. updateAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setStartTime → KILLED |
availability.setStartTime(incoming.getStartTime()); |
144 |
1
1. updateAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setEndTime → KILLED |
availability.setEndTime(incoming.getEndTime()); |
145 |
1
1. updateAvailability : removed call to edu/ucsb/cs156/gauchoride/entities/DriverAvailability::setNotes → KILLED |
availability.setNotes(incoming.getNotes()); |
146 | ||
147 | driverAvailabilityRepository.save(availability); | |
148 | ||
149 |
1
1. updateAvailability : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/DriverAvailabilityController::updateAvailability → KILLED |
return availability; |
150 | } | |
151 | ||
152 | } | |
Mutations | ||
39 |
1.1 |
|
45 |
1.1 |
|
57 |
1.1 |
|
59 |
1.1 |
|
62 |
1.1 |
|
65 |
1.1 |
|
89 |
1.1 |
|
90 |
1.1 |
|
91 |
1.1 |
|
92 |
1.1 |
|
93 |
1.1 |
|
97 |
1.1 |
|
110 |
1.1 |
|
112 |
1.1 |
|
115 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |
|
134 |
1.1 |
|
136 |
1.1 |
|
139 |
1.1 |
|
142 |
1.1 |
|
143 |
1.1 |
|
144 |
1.1 |
|
145 |
1.1 |
|
149 |
1.1 |