1 | package edu.ucsb.cs156.courses.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.courses.entities.PersonalSchedule; | |
4 | import edu.ucsb.cs156.courses.entities.User; | |
5 | import edu.ucsb.cs156.courses.errors.EntityNotFoundException; | |
6 | import edu.ucsb.cs156.courses.models.CurrentUser; | |
7 | import edu.ucsb.cs156.courses.repositories.PersonalScheduleRepository; | |
8 | import io.swagger.v3.oas.annotations.Operation; | |
9 | import io.swagger.v3.oas.annotations.Parameter; | |
10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
11 | import java.util.Optional; | |
12 | import javax.validation.Valid; | |
13 | import lombok.extern.slf4j.Slf4j; | |
14 | import org.springframework.beans.factory.annotation.Autowired; | |
15 | import org.springframework.security.access.prepost.PreAuthorize; | |
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 | @Tag(name = "PersonalSchedules") | |
26 | @RequestMapping("/api/personalschedules") | |
27 | @RestController | |
28 | @Slf4j | |
29 | public class PersonalSchedulesController extends ApiController { | |
30 | ||
31 | @Autowired PersonalScheduleRepository personalscheduleRepository; | |
32 | ||
33 | @Operation(summary = "List all personal schedules") | |
34 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
35 | @GetMapping("/admin/all") | |
36 | public Iterable<PersonalSchedule> allUsersSchedules() { | |
37 | Iterable<PersonalSchedule> personalschedules = personalscheduleRepository.findAll(); | |
38 |
1
1. allUsersSchedules : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::allUsersSchedules → KILLED |
return personalschedules; |
39 | } | |
40 | ||
41 | @Operation(summary = "List this user's personal schedules") | |
42 | @PreAuthorize("hasRole('ROLE_USER')") | |
43 | @GetMapping("/all") | |
44 | public Iterable<PersonalSchedule> thisUsersSchedules() { | |
45 | CurrentUser currentUser = getCurrentUser(); | |
46 | Iterable<PersonalSchedule> personalschedules = | |
47 | personalscheduleRepository.findAllByUserId(currentUser.getUser().getId()); | |
48 |
1
1. thisUsersSchedules : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::thisUsersSchedules → KILLED |
return personalschedules; |
49 | } | |
50 | ||
51 | @Operation(summary = "Get a single personal schedule (if it belongs to current user)") | |
52 | @PreAuthorize("hasRole('ROLE_USER')") | |
53 | @GetMapping("") | |
54 | public PersonalSchedule getScheduleById(@Parameter(name = "id") @RequestParam Long id) { | |
55 | User currentUser = getCurrentUser().getUser(); | |
56 | PersonalSchedule personalschedule = | |
57 | personalscheduleRepository | |
58 | .findByIdAndUser(id, currentUser) | |
59 |
1
1. lambda$getScheduleById$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$getScheduleById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
60 | ||
61 |
1
1. getScheduleById : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::getScheduleById → KILLED |
return personalschedule; |
62 | } | |
63 | ||
64 | @Operation(summary = "Get a single personal schedule (no matter who it belongs to, admin only)") | |
65 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
66 | @GetMapping("/admin") | |
67 | public PersonalSchedule getScheduleById_admin(@Parameter(name = "id") @RequestParam Long id) { | |
68 | PersonalSchedule personalschedule = | |
69 | personalscheduleRepository | |
70 | .findById(id) | |
71 |
1
1. lambda$getScheduleById_admin$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$getScheduleById_admin$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
72 | ||
73 |
1
1. getScheduleById_admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::getScheduleById_admin → KILLED |
return personalschedule; |
74 | } | |
75 | ||
76 | @Operation(summary = "Create a new personal schedule") | |
77 | @PreAuthorize("hasRole('ROLE_USER')") | |
78 | @PostMapping("/post") | |
79 | public PersonalSchedule postSchedule( | |
80 | @Parameter(name = "name") @RequestParam String name, | |
81 | @Parameter(name = "description") @RequestParam String description, | |
82 | @Parameter(name = "quarter") @RequestParam String quarter) { | |
83 | CurrentUser currentUser = getCurrentUser(); | |
84 | log.info("currentUser={}", currentUser); | |
85 | ||
86 | // Checks length of name parameter (length should be 15 chars or less, nonzero) | |
87 |
2
1. postSchedule : changed conditional boundary → KILLED 2. postSchedule : negated conditional → KILLED |
if (name.length() > 15) { |
88 | throw new IllegalArgumentException("name parameter restricted to 15 chars or less"); | |
89 | } | |
90 | ||
91 | PersonalSchedule personalschedule = new PersonalSchedule(); | |
92 |
1
1. postSchedule : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setUser → KILLED |
personalschedule.setUser(currentUser.getUser()); |
93 |
1
1. postSchedule : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setName → KILLED |
personalschedule.setName(name); |
94 |
1
1. postSchedule : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setDescription → KILLED |
personalschedule.setDescription(description); |
95 |
1
1. postSchedule : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setQuarter → KILLED |
personalschedule.setQuarter(quarter); |
96 | ||
97 | Optional<PersonalSchedule> existCheck = | |
98 | personalscheduleRepository.findByUserAndNameAndQuarter( | |
99 | currentUser.getUser(), name, quarter); | |
100 |
1
1. postSchedule : negated conditional → KILLED |
if (existCheck.isPresent()) { |
101 | throw new IllegalArgumentException( | |
102 | "A personal schedule with that name already exists in that quarter"); | |
103 | } | |
104 | PersonalSchedule savedPersonalSchedule = personalscheduleRepository.save(personalschedule); | |
105 |
1
1. postSchedule : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::postSchedule → KILLED |
return savedPersonalSchedule; |
106 | } | |
107 | ||
108 | @Operation(summary = "Delete a personal schedule owned by this user") | |
109 | @PreAuthorize("hasRole('ROLE_USER')") | |
110 | @DeleteMapping("") | |
111 | public Object deleteSchedule(@Parameter(name = "id") @RequestParam Long id) { | |
112 | User currentUser = getCurrentUser().getUser(); | |
113 | PersonalSchedule personalschedule = | |
114 | personalscheduleRepository | |
115 | .findByIdAndUser(id, currentUser) | |
116 |
1
1. lambda$deleteSchedule$2 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$deleteSchedule$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
117 | ||
118 |
1
1. deleteSchedule : removed call to edu/ucsb/cs156/courses/repositories/PersonalScheduleRepository::delete → KILLED |
personalscheduleRepository.delete(personalschedule); |
119 | ||
120 |
1
1. deleteSchedule : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::deleteSchedule → KILLED |
return genericMessage("PersonalSchedule with id %s deleted".formatted(id)); |
121 | } | |
122 | ||
123 | @Operation(summary = "Delete another user's personal schedule") | |
124 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
125 | @DeleteMapping("/admin") | |
126 | public Object deleteSchedule_Admin(@Parameter(name = "id") @RequestParam Long id) { | |
127 | PersonalSchedule personalschedule = | |
128 | personalscheduleRepository | |
129 | .findById(id) | |
130 |
1
1. lambda$deleteSchedule_Admin$3 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$deleteSchedule_Admin$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
131 | ||
132 |
1
1. deleteSchedule_Admin : removed call to edu/ucsb/cs156/courses/repositories/PersonalScheduleRepository::delete → KILLED |
personalscheduleRepository.delete(personalschedule); |
133 | ||
134 |
1
1. deleteSchedule_Admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::deleteSchedule_Admin → KILLED |
return genericMessage("PersonalSchedule with id %s deleted".formatted(id)); |
135 | } | |
136 | ||
137 | @Operation(summary = "Update a single personal schedule (if it belongs to current user)") | |
138 | @PreAuthorize("hasRole('ROLE_USER')") | |
139 | @PutMapping("") | |
140 | public PersonalSchedule putScheduleById( | |
141 | @Parameter(name = "id") @RequestParam Long id, | |
142 | @RequestBody @Valid PersonalSchedule incomingSchedule) { | |
143 | User currentUser = getCurrentUser().getUser(); | |
144 | PersonalSchedule personalschedule = | |
145 | personalscheduleRepository | |
146 | .findByIdAndUser(id, currentUser) | |
147 |
1
1. lambda$putScheduleById$4 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$putScheduleById$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
148 | ||
149 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setName → KILLED |
personalschedule.setName(incomingSchedule.getName()); |
150 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setDescription → KILLED |
personalschedule.setDescription(incomingSchedule.getDescription()); |
151 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setQuarter → KILLED |
personalschedule.setQuarter(incomingSchedule.getQuarter()); |
152 | ||
153 | personalscheduleRepository.save(personalschedule); | |
154 | ||
155 |
1
1. putScheduleById : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::putScheduleById → KILLED |
return personalschedule; |
156 | } | |
157 | ||
158 | @Operation( | |
159 | summary = | |
160 | "Update a single Schedule (regardless of ownership, admin only, can't change ownership)") | |
161 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
162 | @PutMapping("/admin") | |
163 | public PersonalSchedule putScheduleById_admin( | |
164 | @Parameter(name = "id") @RequestParam Long id, | |
165 | @RequestBody @Valid PersonalSchedule incomingSchedule) { | |
166 | PersonalSchedule personalschedule = | |
167 | personalscheduleRepository | |
168 | .findById(id) | |
169 |
1
1. lambda$putScheduleById_admin$5 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$putScheduleById_admin$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, id)); |
170 | ||
171 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setName → KILLED |
personalschedule.setName(incomingSchedule.getName()); |
172 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setDescription → KILLED |
personalschedule.setDescription(incomingSchedule.getDescription()); |
173 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setQuarter → KILLED |
personalschedule.setQuarter(incomingSchedule.getQuarter()); |
174 | ||
175 | personalscheduleRepository.save(personalschedule); | |
176 | ||
177 |
1
1. putScheduleById_admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::putScheduleById_admin → KILLED |
return personalschedule; |
178 | } | |
179 | } | |
Mutations | ||
38 |
1.1 |
|
48 |
1.1 |
|
59 |
1.1 |
|
61 |
1.1 |
|
71 |
1.1 |
|
73 |
1.1 |
|
87 |
1.1 2.2 |
|
92 |
1.1 |
|
93 |
1.1 |
|
94 |
1.1 |
|
95 |
1.1 |
|
100 |
1.1 |
|
105 |
1.1 |
|
116 |
1.1 |
|
118 |
1.1 |
|
120 |
1.1 |
|
130 |
1.1 |
|
132 |
1.1 |
|
134 |
1.1 |
|
147 |
1.1 |
|
149 |
1.1 |
|
150 |
1.1 |
|
151 |
1.1 |
|
155 |
1.1 |
|
169 |
1.1 |
|
171 |
1.1 |
|
172 |
1.1 |
|
173 |
1.1 |
|
177 |
1.1 |