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