1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | ||
3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
4 | import io.swagger.v3.oas.annotations.tags.Tag; | |
5 | import lombok.extern.slf4j.Slf4j; | |
6 | import io.swagger.v3.oas.annotations.Operation; | |
7 | import io.swagger.v3.oas.annotations.Parameter; | |
8 | import org.springframework.beans.factory.annotation.Autowired; | |
9 | import org.springframework.http.HttpStatus; | |
10 | import org.springframework.http.ResponseEntity; | |
11 | import org.springframework.security.access.prepost.PreAuthorize; | |
12 | import org.springframework.security.core.context.SecurityContextHolder; | |
13 | import org.springframework.web.bind.annotation.*; | |
14 | import org.springframework.data.domain.Page; | |
15 | import org.springframework.data.domain.PageRequest; | |
16 | import org.springframework.data.domain.Sort; | |
17 | import org.springframework.format.annotation.DateTimeFormat; | |
18 | import java.util.Date; | |
19 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
20 | ||
21 | ||
22 | ||
23 | import edu.ucsb.cs156.happiercows.entities.Announcement; | |
24 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
25 | import edu.ucsb.cs156.happiercows.repositories.AnnouncementRepository; | |
26 | ||
27 | import edu.ucsb.cs156.happiercows.entities.User; | |
28 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
29 | import edu.ucsb.cs156.happiercows.models.CreateAnnouncementParams; | |
30 | import edu.ucsb.cs156.happiercows.models.CreateCommonsParams; | |
31 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
32 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategies; | |
33 | ||
34 | import org.springframework.security.core.Authentication; | |
35 | import java.text.SimpleDateFormat; | |
36 | import java.util.TimeZone; | |
37 | import java.util.Calendar; | |
38 | import java.util.Date; | |
39 | ||
40 | ||
41 | import java.util.Optional; | |
42 | ||
43 | import javax.validation.Valid; | |
44 | ||
45 | @Tag(name = "Announcements") | |
46 | @RequestMapping("/api/announcements") | |
47 | @RestController | |
48 | @Slf4j | |
49 | public class AnnouncementsController extends ApiController{ | |
50 | ||
51 | @Autowired | |
52 | private AnnouncementRepository announcementRepository; | |
53 | ||
54 | @Autowired | |
55 | private UserCommonsRepository userCommonsRepository; | |
56 | ||
57 | @Autowired | |
58 | ObjectMapper mapper; | |
59 | ||
60 | ||
61 | @Operation(summary = "Create an announcement", description = "Create an announcement associated with a specific commons") | |
62 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
63 | @PostMapping("/post/{commonsId}") | |
64 | public ResponseEntity<Object> createAnnouncement( | |
65 | @Parameter(description = "The id of the common") @PathVariable Long commonsId, | |
66 | @Parameter(description = "The datetime at which the announcement will be shown (defaults to current time)") | |
67 | @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") | |
68 | Date startDate, | |
69 | @Parameter(description = "The datetime at which the announcement will stop being shown (optional)") | |
70 | @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") | |
71 | Date endDate, | |
72 | @Parameter(description = "The announcement to be sent out") @RequestParam String announcementText) { | |
73 | | |
74 | User user = getCurrentUser().getUser(); | |
75 | Long userId = user.getId(); | |
76 | ||
77 | // Make sure the user is part of the commons or is an admin | |
78 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
79 |
3
1. createAnnouncement : negated conditional → KILLED 2. lambda$createAnnouncement$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncement$0 → KILLED 3. lambda$createAnnouncement$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncement$0 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
80 | log.info("User is not an admin"); | |
81 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
82 | ||
83 |
1
1. createAnnouncement : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
84 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.badRequest().body("Commons_id must exist."); |
85 | } | |
86 | } | |
87 | ||
88 | // Fix timezone difference for PST | |
89 |
1
1. createAnnouncement : negated conditional → KILLED |
if (startDate != null) { |
90 | Calendar calendar = Calendar.getInstance(); | |
91 |
1
1. createAnnouncement : removed call to java/util/Calendar::setTime → KILLED |
calendar.setTime(startDate); |
92 | ||
93 |
1
1. createAnnouncement : removed call to java/util/Calendar::set → KILLED |
calendar.set(Calendar.HOUR_OF_DAY, 8); |
94 | startDate = calendar.getTime(); | |
95 | } | |
96 | else { | |
97 | log.info("Start date not specified. Defaulting to current date."); | |
98 | startDate = new Date(); | |
99 | } | |
100 | ||
101 | // Fix timezone difference for PST | |
102 |
1
1. createAnnouncement : negated conditional → KILLED |
if (endDate != null) { |
103 | Calendar calendar = Calendar.getInstance(); | |
104 |
1
1. createAnnouncement : removed call to java/util/Calendar::setTime → KILLED |
calendar.setTime(endDate); |
105 | ||
106 |
1
1. createAnnouncement : removed call to java/util/Calendar::set → KILLED |
calendar.set(Calendar.HOUR_OF_DAY, 8); |
107 | endDate = calendar.getTime(); | |
108 | } | |
109 | ||
110 |
1
1. createAnnouncement : negated conditional → KILLED |
if (announcementText == "") { |
111 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.badRequest().body("Announcement cannot be empty."); |
112 | } | |
113 |
2
1. createAnnouncement : negated conditional → KILLED 2. createAnnouncement : negated conditional → KILLED |
if (endDate != null && startDate.after(endDate)) { |
114 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.badRequest().body("Start date must be before end date."); |
115 | } | |
116 | ||
117 | // Create the announcement | |
118 | Announcement announcementObj = Announcement.builder() | |
119 | .commonsId(commonsId) | |
120 | .startDate(startDate) | |
121 | .endDate(endDate) | |
122 | .announcementText(announcementText) | |
123 | .build(); | |
124 | ||
125 | // Save the announcement | |
126 | announcementRepository.save(announcementObj); | |
127 | ||
128 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.ok(announcementObj); |
129 | } | |
130 | ||
131 | @Operation(summary = "Get all announcements", description = "Get all announcements associated with a specific commons.") | |
132 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
133 | @GetMapping("/getbycommonsid") | |
134 | public ResponseEntity<Object> getAnnouncements(@Parameter(description = "The id of the common") @RequestParam Long commonsId) { | |
135 | ||
136 | // Make sure the user is part of the commons or is an admin | |
137 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
138 |
3
1. getAnnouncements : negated conditional → KILLED 2. lambda$getAnnouncements$1 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getAnnouncements$1 → KILLED 3. lambda$getAnnouncements$1 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getAnnouncements$1 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
139 | log.info("User is not an admin"); | |
140 | User user = getCurrentUser().getUser(); | |
141 | Long userId = user.getId(); | |
142 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
143 | ||
144 |
1
1. getAnnouncements : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
145 |
1
1. getAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncements → KILLED |
return ResponseEntity.badRequest().body("Commons_id must exist."); |
146 | } | |
147 | } | |
148 | ||
149 | int MAX_ANNOUNCEMENTS = 1000; | |
150 | Page<Announcement> announcements = announcementRepository.findByCommonsId(commonsId, PageRequest.of(0, MAX_ANNOUNCEMENTS, Sort.by("startDate").descending())); | |
151 |
1
1. getAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncements → KILLED |
return ResponseEntity.ok(announcements); |
152 | } | |
153 | ||
154 | @Operation(summary = "Get announcements by id", description = "Get announcement by its id.") | |
155 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
156 | @GetMapping("/getbyid") | |
157 | public ResponseEntity<Object> getAnnouncementById(@Parameter(description = "The id of the announcement") @RequestParam Long id) { | |
158 | ||
159 | Optional<Announcement> announcementLookup = announcementRepository.findByAnnouncementId(id); | |
160 |
1
1. getAnnouncementById : negated conditional → KILLED |
if (!announcementLookup.isPresent()) { |
161 |
1
1. getAnnouncementById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementById → KILLED |
return ResponseEntity.badRequest().body("Cannot find announcement. id is invalid."); |
162 | ||
163 | } | |
164 |
1
1. getAnnouncementById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementById → KILLED |
return ResponseEntity.ok(announcementLookup.get()); |
165 | } | |
166 | ||
167 | @Operation(summary = "Edit an announcement", description = "Edit an announcement by its id.") | |
168 | @PreAuthorize("hasAnyRole('ROLE_ADMIN')") | |
169 | @PutMapping("/put") | |
170 | public ResponseEntity<Object> editAnnouncement( | |
171 | @Parameter(name="id") @RequestParam long id, | |
172 | @Parameter(name="request body") @RequestBody CreateAnnouncementParams params) { | |
173 | Optional<Announcement> existing = announcementRepository.findByAnnouncementId(id); | |
174 | ||
175 | Announcement updated; | |
176 | HttpStatus status; | |
177 | | |
178 | // Error 404: Not found | |
179 |
1
1. editAnnouncement : negated conditional → KILLED |
if (existing.isPresent()) { |
180 | updated = existing.get(); | |
181 | status = HttpStatus.OK; | |
182 | } else { | |
183 | updated = new Announcement(); | |
184 | throw new EntityNotFoundException(Announcement.class, id); | |
185 | } | |
186 | ||
187 | // Default start date is OK... | |
188 |
1
1. editAnnouncement : negated conditional → KILLED |
if (params.getStartDate() == null) { |
189 | log.info("Start date not specified. Defaulting to current date."); | |
190 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setStartDate → KILLED |
updated.setStartDate(new Date()); |
191 | } | |
192 | else { | |
193 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setStartDate → KILLED |
updated.setStartDate(params.getStartDate()); |
194 | } | |
195 | ||
196 | // ...But a start date after an end date is not OK. | |
197 | // This error only occurs if the user provides an end date. | |
198 | // Otherwise, the end date is NULL and the start date could be any date. | |
199 |
1
1. editAnnouncement : negated conditional → KILLED |
if(params.getEndDate() != null) { |
200 |
1
1. editAnnouncement : negated conditional → KILLED |
if (updated.getStartDate().after(params.getEndDate())) { |
201 | throw new IllegalArgumentException("The start date may not be after the end date."); | |
202 | } | |
203 | else { | |
204 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setEndDate → KILLED |
updated.setEndDate(params.getEndDate()); |
205 | } | |
206 | } | |
207 | ||
208 | // The provided announcement text must not be empty or missing. | |
209 |
2
1. editAnnouncement : negated conditional → KILLED 2. editAnnouncement : negated conditional → KILLED |
if (params.getAnnouncementText() == null || params.getAnnouncementText().equals("")) { |
210 | throw new IllegalArgumentException("Announcement Text cannot be empty."); | |
211 | } | |
212 | else { | |
213 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setAnnouncementText → KILLED |
updated.setAnnouncementText(params.getAnnouncementText()); |
214 | } | |
215 | ||
216 | announcementRepository.save(updated); | |
217 |
1
1. editAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::editAnnouncement → SURVIVED |
return ResponseEntity.ok(updated); |
218 | } | |
219 | ||
220 | ||
221 | @Operation(summary = "Delete an announcement", description = "Delete an announcement associated with an id") | |
222 | @PreAuthorize("hasAnyRole('ROLE_ADMIN')") | |
223 | @DeleteMapping("/delete") | |
224 | public ResponseEntity<Object> deleteAnnouncement(@Parameter(description = "The id of the chat message") @RequestParam Long id) { | |
225 | ||
226 | // Try to get the chat message | |
227 | Optional<Announcement> announcementLookup = announcementRepository.findByAnnouncementId(id); | |
228 |
1
1. deleteAnnouncement : negated conditional → KILLED |
if (!announcementLookup.isPresent()) { |
229 |
1
1. deleteAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncement → KILLED |
return ResponseEntity.badRequest().body("Cannot find announcement. id is invalid."); |
230 | } | |
231 | Announcement announcementObj = announcementLookup.get(); | |
232 | ||
233 | User user = getCurrentUser().getUser(); | |
234 | Long userId = user.getId(); | |
235 | ||
236 | // Hide the message | |
237 |
1
1. deleteAnnouncement : removed call to edu/ucsb/cs156/happiercows/repositories/AnnouncementRepository::delete → KILLED |
announcementRepository.delete(announcementObj); |
238 |
1
1. deleteAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncement → KILLED |
return ResponseEntity.ok(announcementObj); |
239 | } | |
240 | ||
241 | ||
242 | } | |
Mutations | ||
79 |
1.1 2.2 3.3 |
|
83 |
1.1 |
|
84 |
1.1 |
|
89 |
1.1 |
|
91 |
1.1 |
|
93 |
1.1 |
|
102 |
1.1 |
|
104 |
1.1 |
|
106 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
113 |
1.1 2.2 |
|
114 |
1.1 |
|
128 |
1.1 |
|
138 |
1.1 2.2 3.3 |
|
144 |
1.1 |
|
145 |
1.1 |
|
151 |
1.1 |
|
160 |
1.1 |
|
161 |
1.1 |
|
164 |
1.1 |
|
179 |
1.1 |
|
188 |
1.1 |
|
190 |
1.1 |
|
193 |
1.1 |
|
199 |
1.1 |
|
200 |
1.1 |
|
204 |
1.1 |
|
209 |
1.1 2.2 |
|
213 |
1.1 |
|
217 |
1.1 |
|
228 |
1.1 |
|
229 |
1.1 |
|
237 |
1.1 |
|
238 |
1.1 |