| 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 | import java.time.LocalDateTime; | |
| 28 | import java.time.ZoneId; | |
| 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 | private static final int ANNOUNCEMENT_TEXT_MAX_LENGTH = 255; | |
| 41 | private static final ZoneId ANNOUNCEMENTS_TIME_ZONE = ZoneId.of("America/Los_Angeles"); | |
| 42 | ||
| 43 | @Autowired | |
| 44 | private AnnouncementRepository announcementRepository; | |
| 45 | ||
| 46 | @Autowired | |
| 47 | private UserCommonsRepository userCommonsRepository; | |
| 48 | ||
| 49 | @Autowired | |
| 50 | ObjectMapper mapper; | |
| 51 | ||
| 52 | ||
| 53 | @Operation(summary = "Create an announcement", description = "Create an announcement associated with a specific commons") | |
| 54 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 55 | @PostMapping("/post") | |
| 56 | public ResponseEntity<Object> createAnnouncement( | |
| 57 | @Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 58 | @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, | |
| 59 | @Parameter(description = "The datetime at which the announcement will stop being shown (optional)") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate, | |
| 60 | @Parameter(description = "The announcement to be sent out") @RequestParam String announcementText) { | |
| 61 | ||
| 62 | User user = getCurrentUser().getUser(); | |
| 63 | Long userId = user.getId(); | |
| 64 | ||
| 65 | // Make sure the user is part of the commons or is an admin | |
| 66 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 67 |
3
1. createAnnouncement : negated conditional → KILLED 2. lambda$createAnnouncement$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncement$0 → KILLED 3. lambda$createAnnouncement$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncement$0 → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 68 | log.info("User is not an admin"); | |
| 69 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 70 | ||
| 71 |
1
1. createAnnouncement : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
| 72 |
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."); |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 |
1
1. createAnnouncement : negated conditional → KILLED |
if (startDate == null) { |
| 77 | log.info("Start date not specified. Defaulting to current date."); | |
| 78 | startDate = LocalDateTime.now(); | |
| 79 | } | |
| 80 | ||
| 81 |
1
1. createAnnouncement : negated conditional → KILLED |
if (announcementText.isBlank()) { |
| 82 |
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."); |
| 83 | } | |
| 84 |
2
1. createAnnouncement : changed conditional boundary → KILLED 2. createAnnouncement : negated conditional → KILLED |
if (announcementText.length() > ANNOUNCEMENT_TEXT_MAX_LENGTH) { |
| 85 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.badRequest().body( |
| 86 | String.format("Announcement must be %d characters or fewer.", ANNOUNCEMENT_TEXT_MAX_LENGTH)); | |
| 87 | } | |
| 88 |
2
1. createAnnouncement : negated conditional → KILLED 2. createAnnouncement : negated conditional → KILLED |
if (endDate != null && startDate.isAfter(endDate)) { |
| 89 |
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."); |
| 90 | } | |
| 91 | ||
| 92 | Date startDateAsDate = toDate(startDate); | |
| 93 |
1
1. createAnnouncement : negated conditional → KILLED |
Date endDateAsDate = endDate == null ? null : toDate(endDate); |
| 94 | ||
| 95 | // Create the announcement | |
| 96 | Announcement announcementObj = Announcement.builder() | |
| 97 | .commonsId(commonsId) | |
| 98 | .startDate(startDateAsDate) | |
| 99 | .endDate(endDateAsDate) | |
| 100 | .announcementText(announcementText) | |
| 101 | .build(); | |
| 102 | ||
| 103 | // Save the announcement | |
| 104 | announcementRepository.save(announcementObj); | |
| 105 | ||
| 106 |
1
1. createAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncement → KILLED |
return ResponseEntity.ok(announcementObj); |
| 107 | } | |
| 108 | ||
| 109 | @Operation(summary = "Get all announcements", description = "Get all announcements associated with a specific commons.") | |
| 110 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 111 | @GetMapping("/getbycommonsid") | |
| 112 | public ResponseEntity<Object> getAnnouncements(@Parameter(description = "The id of the common") @RequestParam Long commonsId) { | |
| 113 | ||
| 114 | // Make sure the user is part of the commons or is an admin | |
| 115 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 116 |
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"))){ |
| 117 | log.info("User is not an admin"); | |
| 118 | User user = getCurrentUser().getUser(); | |
| 119 | Long userId = user.getId(); | |
| 120 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 121 | ||
| 122 |
1
1. getAnnouncements : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
| 123 |
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."); |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | int MAX_ANNOUNCEMENTS = 1000; | |
| 128 | Page<Announcement> announcements = announcementRepository.findByCommonsId(commonsId, PageRequest.of(0, MAX_ANNOUNCEMENTS, Sort.by("startDate").descending())); | |
| 129 |
1
1. getAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncements → KILLED |
return ResponseEntity.ok(announcements); |
| 130 | } | |
| 131 | ||
| 132 | @Operation(summary = "Get current announcements", description = "Get current announcements for a commons.") | |
| 133 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 134 | @GetMapping("/current") | |
| 135 | public ResponseEntity<Object> getCurrentAnnouncements( | |
| 136 | @Parameter(description = "The id of the commons") @RequestParam Long commonsId) { | |
| 137 | ||
| 138 | User user = getCurrentUser().getUser(); | |
| 139 | Long userId = user.getId(); | |
| 140 | ||
| 141 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 142 |
3
1. lambda$getCurrentAnnouncements$2 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getCurrentAnnouncements$2 → KILLED 2. lambda$getCurrentAnnouncements$2 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getCurrentAnnouncements$2 → KILLED 3. getCurrentAnnouncements : negated conditional → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))) { |
| 143 | Optional<UserCommons> userCommonsLookup = | |
| 144 | userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 145 | ||
| 146 |
1
1. getCurrentAnnouncements : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
| 147 |
1
1. getCurrentAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getCurrentAnnouncements → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN) |
| 148 | .body("User is not a member of this commons."); | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | int MAX_ANNOUNCEMENTS = 1000; | |
| 153 | Page<Announcement> announcements = | |
| 154 | announcementRepository.findCurrentByCommonsId( | |
| 155 | commonsId, | |
| 156 | PageRequest.of(0, MAX_ANNOUNCEMENTS, Sort.by("startDate").descending()) | |
| 157 | ); | |
| 158 | ||
| 159 |
1
1. getCurrentAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getCurrentAnnouncements → KILLED |
return ResponseEntity.ok(announcements.getContent()); |
| 160 | } | |
| 161 | ||
| 162 | @Operation(summary = "Get announcements by id", description = "Get announcement by its id.") | |
| 163 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 164 | @GetMapping("/getbyid") | |
| 165 | public ResponseEntity<Object> getAnnouncementById(@Parameter(description = "The id of the announcement") @RequestParam Long id) { | |
| 166 | ||
| 167 | Optional<Announcement> announcementLookup = announcementRepository.findByAnnouncementId(id); | |
| 168 |
1
1. getAnnouncementById : negated conditional → KILLED |
if (!announcementLookup.isPresent()) { |
| 169 |
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."); |
| 170 | ||
| 171 | } | |
| 172 |
1
1. getAnnouncementById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementById → KILLED |
return ResponseEntity.ok(announcementLookup.get()); |
| 173 | } | |
| 174 | ||
| 175 | @Operation(summary = "Edit an announcement", description = "Edit an announcement by its id.") | |
| 176 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 177 | @PutMapping("/put") | |
| 178 | public ResponseEntity<Object> editAnnouncement( | |
| 179 | @Parameter(description = "The id of the announcement") @RequestParam Long id, | |
| 180 | @Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 181 | @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, | |
| 182 | @Parameter(description = "The datetime at which the announcement will stop being shown (optional)") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate, | |
| 183 | @Parameter(description = "The announcement to be sent out") @RequestParam String announcementText) { | |
| 184 | ||
| 185 | User user = getCurrentUser().getUser(); | |
| 186 | Long userId = user.getId(); | |
| 187 | ||
| 188 | // Make sure the user is part of the commons or is an admin | |
| 189 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 190 |
3
1. lambda$editAnnouncement$3 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$editAnnouncement$3 → KILLED 2. lambda$editAnnouncement$3 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$editAnnouncement$3 → KILLED 3. editAnnouncement : negated conditional → KILLED |
if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ |
| 191 | log.info("User is not an admin"); | |
| 192 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 193 | ||
| 194 |
1
1. editAnnouncement : negated conditional → KILLED |
if (!userCommonsLookup.isPresent()) { |
| 195 |
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."); |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 |
1
1. editAnnouncement : negated conditional → KILLED |
if (announcementText.isBlank()) { |
| 200 |
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."); |
| 201 | } | |
| 202 |
2
1. editAnnouncement : negated conditional → KILLED 2. editAnnouncement : changed conditional boundary → KILLED |
if (announcementText.length() > ANNOUNCEMENT_TEXT_MAX_LENGTH) { |
| 203 |
1
1. editAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::editAnnouncement → KILLED |
return ResponseEntity.badRequest().body( |
| 204 | String.format("Announcement must be %d characters or fewer.", ANNOUNCEMENT_TEXT_MAX_LENGTH)); | |
| 205 | } | |
| 206 | ||
| 207 |
1
1. editAnnouncement : negated conditional → KILLED |
if (startDate == null) { |
| 208 | log.info("Start date not specified. Defaulting to current date."); | |
| 209 | startDate = LocalDateTime.now(); | |
| 210 | } | |
| 211 | ||
| 212 |
2
1. editAnnouncement : negated conditional → KILLED 2. editAnnouncement : negated conditional → KILLED |
if (endDate != null && startDate.isAfter(endDate)) { |
| 213 |
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."); |
| 214 | } | |
| 215 | ||
| 216 | Date startDateAsDate = toDate(startDate); | |
| 217 |
1
1. editAnnouncement : negated conditional → KILLED |
Date endDateAsDate = endDate == null ? null : toDate(endDate); |
| 218 | ||
| 219 | Optional<Announcement> announcementLookup = announcementRepository.findByAnnouncementId(id); | |
| 220 | ||
| 221 |
1
1. editAnnouncement : negated conditional → KILLED |
if (!announcementLookup.isPresent()) { |
| 222 |
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."); |
| 223 | } | |
| 224 | ||
| 225 | // Create the announcement | |
| 226 | Announcement announcementObj = announcementLookup.get(); | |
| 227 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setStartDate → KILLED |
announcementObj.setStartDate(startDateAsDate); |
| 228 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setEndDate → KILLED |
announcementObj.setEndDate(endDateAsDate); |
| 229 |
1
1. editAnnouncement : removed call to edu/ucsb/cs156/happiercows/entities/Announcement::setAnnouncementText → KILLED |
announcementObj.setAnnouncementText(announcementText); |
| 230 | ||
| 231 | // Save the announcement | |
| 232 | announcementRepository.save(announcementObj); | |
| 233 |
1
1. editAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::editAnnouncement → KILLED |
return ResponseEntity.ok(announcementObj); |
| 234 | } | |
| 235 | ||
| 236 | ||
| 237 | @Operation(summary = "Delete an announcement", description = "Delete an announcement associated with an id") | |
| 238 | @PreAuthorize("hasAnyRole('ROLE_ADMIN')") | |
| 239 | @DeleteMapping("/delete") | |
| 240 | public ResponseEntity<Object> deleteAnnouncement(@Parameter(description = "The id of the chat message") @RequestParam Long id) { | |
| 241 | ||
| 242 | // Try to get the chat message | |
| 243 | Optional<Announcement> announcementLookup = announcementRepository.findByAnnouncementId(id); | |
| 244 |
1
1. deleteAnnouncement : negated conditional → KILLED |
if (!announcementLookup.isPresent()) { |
| 245 |
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."); |
| 246 | } | |
| 247 | Announcement announcementObj = announcementLookup.get(); | |
| 248 | ||
| 249 | User user = getCurrentUser().getUser(); | |
| 250 | Long userId = user.getId(); | |
| 251 | ||
| 252 | // Hide the message | |
| 253 |
1
1. deleteAnnouncement : removed call to edu/ucsb/cs156/happiercows/repositories/AnnouncementRepository::delete → KILLED |
announcementRepository.delete(announcementObj); |
| 254 |
1
1. deleteAnnouncement : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncement → KILLED |
return ResponseEntity.ok(announcementObj); |
| 255 | } | |
| 256 | ||
| 257 | private Date toDate(LocalDateTime localDateTime) { | |
| 258 |
1
1. toDate : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::toDate → KILLED |
return Date.from(localDateTime.atZone(ANNOUNCEMENTS_TIME_ZONE).toInstant()); |
| 259 | } | |
| 260 | ||
| 261 | ||
| 262 | } | |
Mutations | ||
| 67 |
1.1 2.2 3.3 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |
|
| 76 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 84 |
1.1 2.2 |
|
| 85 |
1.1 |
|
| 88 |
1.1 2.2 |
|
| 89 |
1.1 |
|
| 93 |
1.1 |
|
| 106 |
1.1 |
|
| 116 |
1.1 2.2 3.3 |
|
| 122 |
1.1 |
|
| 123 |
1.1 |
|
| 129 |
1.1 |
|
| 142 |
1.1 2.2 3.3 |
|
| 146 |
1.1 |
|
| 147 |
1.1 |
|
| 159 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 172 |
1.1 |
|
| 190 |
1.1 2.2 3.3 |
|
| 194 |
1.1 |
|
| 195 |
1.1 |
|
| 199 |
1.1 |
|
| 200 |
1.1 |
|
| 202 |
1.1 2.2 |
|
| 203 |
1.1 |
|
| 207 |
1.1 |
|
| 212 |
1.1 2.2 |
|
| 213 |
1.1 |
|
| 217 |
1.1 |
|
| 221 |
1.1 |
|
| 222 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 233 |
1.1 |
|
| 244 |
1.1 |
|
| 245 |
1.1 |
|
| 253 |
1.1 |
|
| 254 |
1.1 |
|
| 258 |
1.1 |