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