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 |
1
1. deleteSchedule : removed call to edu/ucsb/cs156/courses/repositories/PersonalScheduleRepository::delete → KILLED |
personalscheduleRepository.delete(personalschedule); |
122 | ||
123 | Iterable<PSCourse> coursesToDelete = coursesRepository.findAllByPsId(id); | |
124 | for (PSCourse course : coursesToDelete) { | |
125 |
1
1. deleteSchedule : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED |
coursesRepository.delete(course); |
126 | } | |
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$3 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::lambda$deleteSchedule_Admin$3 → 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 | Iterable<PSCourse> coursesToDelete = coursesRepository.findAllByPsId(id); | |
143 | for (PSCourse course : coursesToDelete) { | |
144 |
1
1. deleteSchedule_Admin : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED |
coursesRepository.delete(course); |
145 | } | |
146 | ||
147 |
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)); |
148 | } | |
149 | ||
150 | @Operation(summary = "Update a single personal schedule (if it belongs to current user)") | |
151 | @PreAuthorize("hasRole('ROLE_USER')") | |
152 | @PutMapping("") | |
153 | public PersonalSchedule putScheduleById( | |
154 | @Parameter(name = "id") @RequestParam Long id, | |
155 | @RequestBody @Valid PersonalSchedule incomingSchedule) { | |
156 | User currentUser = getCurrentUser().getUser(); | |
157 | PersonalSchedule personalschedule = | |
158 | personalscheduleRepository | |
159 | .findByIdAndUser(id, currentUser) | |
160 |
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)); |
161 | ||
162 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setName → KILLED |
personalschedule.setName(incomingSchedule.getName()); |
163 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setDescription → KILLED |
personalschedule.setDescription(incomingSchedule.getDescription()); |
164 |
1
1. putScheduleById : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setQuarter → KILLED |
personalschedule.setQuarter(incomingSchedule.getQuarter()); |
165 | ||
166 | personalscheduleRepository.save(personalschedule); | |
167 | ||
168 |
1
1. putScheduleById : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::putScheduleById → KILLED |
return personalschedule; |
169 | } | |
170 | ||
171 | @Operation( | |
172 | summary = | |
173 | "Update a single Schedule (regardless of ownership, admin only, can't change ownership)") | |
174 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
175 | @PutMapping("/admin") | |
176 | public PersonalSchedule putScheduleById_admin( | |
177 | @Parameter(name = "id") @RequestParam Long id, | |
178 | @RequestBody @Valid PersonalSchedule incomingSchedule) { | |
179 | PersonalSchedule personalschedule = | |
180 | personalscheduleRepository | |
181 | .findById(id) | |
182 |
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)); |
183 | ||
184 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setName → KILLED |
personalschedule.setName(incomingSchedule.getName()); |
185 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setDescription → KILLED |
personalschedule.setDescription(incomingSchedule.getDescription()); |
186 |
1
1. putScheduleById_admin : removed call to edu/ucsb/cs156/courses/entities/PersonalSchedule::setQuarter → KILLED |
personalschedule.setQuarter(incomingSchedule.getQuarter()); |
187 | ||
188 | personalscheduleRepository.save(personalschedule); | |
189 | ||
190 |
1
1. putScheduleById_admin : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSchedulesController::putScheduleById_admin → KILLED |
return personalschedule; |
191 | } | |
192 | } | |
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 |
|
121 |
1.1 |
|
125 |
1.1 |
|
128 |
1.1 |
|
138 |
1.1 |
|
140 |
1.1 |
|
144 |
1.1 |
|
147 |
1.1 |
|
160 |
1.1 |
|
162 |
1.1 |
|
163 |
1.1 |
|
164 |
1.1 |
|
168 |
1.1 |
|
182 |
1.1 |
|
184 |
1.1 |
|
185 |
1.1 |
|
186 |
1.1 |
|
190 |
1.1 |