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