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