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