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