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