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