| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 6 | import lombok.extern.slf4j.Slf4j; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import io.swagger.v3.oas.annotations.Parameter; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.http.HttpStatus; | |
| 11 | import org.springframework.http.ResponseEntity; | |
| 12 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 13 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 14 | import org.springframework.web.bind.annotation.*; | |
| 15 | import org.springframework.data.domain.Sort; | |
| 16 | import org.springframework.format.annotation.DateTimeFormat; | |
| 17 | ||
| 18 | import edu.ucsb.cs156.happiercows.entities.Announcements; | |
| 19 | import edu.ucsb.cs156.happiercows.repositories.AnnouncementsRepository; | |
| 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 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 25 | ||
| 26 | import org.springframework.security.core.Authentication; | |
| 27 | ||
| 28 | import java.util.Optional; | |
| 29 | ||
| 30 | import java.util.ArrayList; | |
| 31 | import java.util.List; | |
| 32 | import java.time.LocalDateTime; | |
| 33 | ||
| 34 | @Tag(name = "Announcements") | |
| 35 | @RequestMapping("/api/announcements") | |
| 36 | @RestController | |
| 37 | @Slf4j | |
| 38 | public class AnnouncementsController extends ApiController { | |
| 39 |     @Autowired | |
| 40 |     CommonsRepository commonsRepository; | |
| 41 | ||
| 42 |     @Autowired | |
| 43 |     UserCommonsRepository userCommonsRepository; | |
| 44 | ||
| 45 |     @Autowired | |
| 46 |     AnnouncementsRepository announcementsRepository; | |
| 47 | ||
| 48 |     @Autowired | |
| 49 |     ObjectMapper mapper; | |
| 50 | ||
| 51 |     @Operation(summary = "Get all announcements", description = "Get all announcements associated with a specific commons.") | |
| 52 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 53 |     @GetMapping("/get") | |
| 54 |     public ResponseEntity<String> getAnnouncements(@Parameter(description = "The id of the common") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 55 | ||
| 56 |         // Return the list of announcements | |
| 57 |         Iterable<Announcements> messages = announcementsRepository.findAllByCommonsId(commonsId); | |
| 58 |         String body = mapper.writeValueAsString(messages); | |
| 59 | 
1
1. getAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncements → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 60 |     } | |
| 61 | ||
| 62 |     @Operation(summary = "Get announcement by id", description = "Get announcement associated with a specific id.") | |
| 63 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 64 |     @GetMapping("/get/by-id") | |
| 65 |     public ResponseEntity<String> getAnnouncementsById(@Parameter(description = "The id of the announcement") @RequestParam Long announcementId) throws JsonProcessingException { | |
| 66 | ||
| 67 |         // Try to get the announcement | |
| 68 |         Optional<Announcements> announcementLookup = announcementsRepository.findById(announcementId); | |
| 69 | 
1
1. getAnnouncementsById : negated conditional → KILLED | 
        if (!announcementLookup.isPresent()) { | 
| 70 |             String responseString = String.format("Announcement with id %d not found", announcementId); | |
| 71 | 
1
1. getAnnouncementsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementsById → KILLED | 
            return ResponseEntity.badRequest().body(responseString); | 
| 72 |         } | |
| 73 | ||
| 74 |         // Return the announcement | |
| 75 |         String body = mapper.writeValueAsString(announcementLookup); | |
| 76 | 
1
1. getAnnouncementsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementsById → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 77 |     }   | |
| 78 |      | |
| 79 |     @Operation(summary = "Create an announcement", description = "Create an announcement associated with a specific commons") | |
| 80 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 81 |     @PostMapping("/post") | |
| 82 |     public ResponseEntity<String> createAnnouncements(@Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 83 |                                             @Parameter(name="start", description="Start time (in iso format, e.g. YYYY-mm-ddTHH:MM:SS) ex: 2024-03-06T04:26:58.76") @RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, | |
| 84 |                                             @Parameter(name="end", description="End time (in iso format)") @RequestParam(name = "end", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end, | |
| 85 |                                             @Parameter(name="announcement") @RequestParam String announcement) { | |
| 86 |          | |
| 87 |         User user = getCurrentUser().getUser(); | |
| 88 |         Long userId = user.getId(); | |
| 89 | ||
| 90 |         // Make sure the commons exists | |
| 91 | 
1
1. createAnnouncements : negated conditional → KILLED | 
        if (!commonsRepository.findById(commonsId).isPresent()) { | 
| 92 |             String responseString = String.format("Commons with id %d not found", commonsId); | |
| 93 | 
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED | 
            return ResponseEntity.badRequest().body(responseString); | 
| 94 |         } | |
| 95 | ||
| 96 |         // Make sure the user is part of the commons or is an admin | |
| 97 |         Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 98 | 
3
1. createAnnouncements : negated conditional → KILLED 2. lambda$createAnnouncements$0 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncements$0 → KILLED 3. lambda$createAnnouncements$0 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncements$0 → KILLED  | 
        if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ | 
| 99 |             log.info("User is not an admin"); | |
| 100 |             Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 101 | ||
| 102 | 
1
1. createAnnouncements : negated conditional → KILLED | 
            if (!userCommonsLookup.isPresent()) { | 
| 103 | 
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 104 |             } | |
| 105 |         } | |
| 106 | ||
| 107 |         // Ensure start date is before end date | |
| 108 | 
1
1. createAnnouncements : negated conditional → KILLED | 
        if (end != null) { | 
| 109 | 
1
1. createAnnouncements : negated conditional → KILLED | 
            if (end.isBefore(start)) { | 
| 110 | 
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED | 
                return ResponseEntity.badRequest().body("End time must be after start time"); | 
| 111 |             } | |
| 112 |         } | |
| 113 | ||
| 114 |         // Check if announcements is empty | |
| 115 | 
1
1. createAnnouncements : negated conditional → KILLED | 
        if (announcement.isEmpty()) { | 
| 116 | 
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED | 
            return ResponseEntity.badRequest().body("Announcement cannot be empty"); | 
| 117 |         } | |
| 118 | ||
| 119 |         // Create the announcement | |
| 120 |         Announcements announcements = Announcements.builder() | |
| 121 |         .commonsId(commonsId) | |
| 122 |         .start(start) | |
| 123 |         .end(end) | |
| 124 |         .announcement(announcement) | |
| 125 |         .build(); | |
| 126 | ||
| 127 |         // Save the announcement | |
| 128 |         announcementsRepository.save(announcements); | |
| 129 |         String responseString = String.format("Added announcement to commons with id %d", commonsId); | |
| 130 | ||
| 131 | 
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED | 
        return ResponseEntity.ok().body(responseString); | 
| 132 |     } | |
| 133 | ||
| 134 |     @Operation(summary = "Delete an announcement", description = "Delete an announcement associated with a specific commons") | |
| 135 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 136 |     @DeleteMapping("") | |
| 137 |     public Object deleteAnnouncements( | |
| 138 |         @Parameter(description = "The id of the announcement") @RequestParam Long announcementId) { | |
| 139 | ||
| 140 |         // Try to get the announcement | |
| 141 |         Optional<Announcements> announcementLookup = announcementsRepository.findById(announcementId); | |
| 142 | 
1
1. deleteAnnouncements : negated conditional → KILLED | 
        if (!announcementLookup.isPresent()) { | 
| 143 | 
1
1. deleteAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncements → KILLED | 
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | 
| 144 |         } | |
| 145 |         Announcements announcement = announcementLookup.get(); | |
| 146 | ||
| 147 |         User user = getCurrentUser().getUser(); | |
| 148 |         Long userId = user.getId(); | |
| 149 | ||
| 150 |         // Check if the user is an admin | |
| 151 |         Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | |
| 152 | 
3
1. deleteAnnouncements : negated conditional → KILLED 2. lambda$deleteAnnouncements$1 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$deleteAnnouncements$1 → KILLED 3. lambda$deleteAnnouncements$1 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$deleteAnnouncements$1 → KILLED  | 
        if (!auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"))){ | 
| 153 | 
1
1. deleteAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncements → KILLED | 
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 154 |         } | |
| 155 | ||
| 156 |         // Delete the announcement | |
| 157 | 
1
1. deleteAnnouncements : removed call to edu/ucsb/cs156/happiercows/repositories/AnnouncementsRepository::delete → KILLED | 
        announcementsRepository.delete(announcement); | 
| 158 |         String responseString = String.format("announcement with id %d deleted", announcementId); | |
| 159 | 
1
1. deleteAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncements → KILLED | 
        return genericMessage(responseString); | 
| 160 |     } | |
| 161 | ||
| 162 |     @Operation(summary = "Update an announcement", description = "Update an announcement associated with a specific id") | |
| 163 |     @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 164 |     @PutMapping("") | |
| 165 |     public ResponseEntity<String> updateAnnouncements(@Parameter(description = "The id of the announcement") @RequestParam Long announcementId, | |
| 166 |                                             @Parameter(description = "The id of the common") @RequestParam Long commonsId, | |
| 167 |                                             @Parameter(name="start", description="Start time (in iso format, e.g. YYYY-mm-ddTHH:MM:SS) ex: 2024-03-06T04:26:58.76") @RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, | |
| 168 |                                             @Parameter(name="end", description="End time (in iso format)") @RequestParam(name = "end", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end, | |
| 169 |                                             @Parameter(name="announcement") @RequestParam String announcement) { | |
| 170 |          | |
| 171 |         User user = getCurrentUser().getUser(); | |
| 172 |         Long userId = user.getId(); | |
| 173 | ||
| 174 |         // Try to get the announcement | |
| 175 |         Optional<Announcements> exists = announcementsRepository.findById(announcementId); | |
| 176 | 
1
1. updateAnnouncements : negated conditional → KILLED | 
        if (!exists.isPresent()) { | 
| 177 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); | 
| 178 |         } | |
| 179 | ||
| 180 |         Announcements announcements = exists.get(); | |
| 181 | ||
| 182 |         // Make sure the commons exists | |
| 183 | 
1
1. updateAnnouncements : negated conditional → KILLED | 
        if (!commonsRepository.findById(commonsId).isPresent()) { | 
| 184 |             String responseString = String.format("Commons with id %d not found", commonsId); | |
| 185 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
            return ResponseEntity.badRequest().body(responseString); | 
| 186 |         } | |
| 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$updateAnnouncements$2 : replaced boolean return with false for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$updateAnnouncements$2 → KILLED 2. lambda$updateAnnouncements$2 : replaced boolean return with true for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$updateAnnouncements$2 → KILLED 3. updateAnnouncements : 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. updateAnnouncements : negated conditional → KILLED | 
            if (!userCommonsLookup.isPresent()) { | 
| 195 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
                return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | 
| 196 |             } | |
| 197 |         } | |
| 198 | ||
| 199 |         // Ensure start date is before end date | |
| 200 | 
1
1. updateAnnouncements : negated conditional → KILLED | 
        if (end != null) { | 
| 201 | 
1
1. updateAnnouncements : negated conditional → KILLED | 
            if (end.isBefore(start)) { | 
| 202 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
                return ResponseEntity.badRequest().body("End time must be after start time"); | 
| 203 |             } | |
| 204 |         } | |
| 205 | ||
| 206 |         // Check if announcements is empty | |
| 207 | 
1
1. updateAnnouncements : negated conditional → KILLED | 
        if (announcement.isEmpty()) { | 
| 208 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
            return ResponseEntity.badRequest().body("Announcement cannot be empty"); | 
| 209 |         } | |
| 210 | ||
| 211 |         // Update the announcement | |
| 212 | 
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setCommonsId → KILLED | 
        announcements.setCommonsId(commonsId); | 
| 213 | 
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setStart → KILLED | 
        announcements.setStart(start); | 
| 214 | 
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setEnd → KILLED | 
        announcements.setEnd(end); | 
| 215 | 
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setAnnouncement → KILLED | 
        announcements.setAnnouncement(announcement); | 
| 216 | ||
| 217 |         // Save the announcement | |
| 218 |         announcementsRepository.save(announcements); | |
| 219 |         String responseString = String.format("Updated announcement with id %d", announcementId); | |
| 220 | 
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED | 
        return ResponseEntity.ok().body(responseString); | 
| 221 |     } | |
| 222 | } | |
Mutations | ||
| 59 | 
 
 1.1  | 
|
| 69 | 
 
 1.1  | 
|
| 71 | 
 
 1.1  | 
|
| 76 | 
 
 1.1  | 
|
| 91 | 
 
 1.1  | 
|
| 93 | 
 
 1.1  | 
|
| 98 | 
 
 1.1 2.2 3.3  | 
|
| 102 | 
 
 1.1  | 
|
| 103 | 
 
 1.1  | 
|
| 108 | 
 
 1.1  | 
|
| 109 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  | 
|
| 115 | 
 
 1.1  | 
|
| 116 | 
 
 1.1  | 
|
| 131 | 
 
 1.1  | 
|
| 142 | 
 
 1.1  | 
|
| 143 | 
 
 1.1  | 
|
| 152 | 
 
 1.1 2.2 3.3  | 
|
| 153 | 
 
 1.1  | 
|
| 157 | 
 
 1.1  | 
|
| 159 | 
 
 1.1  | 
|
| 176 | 
 
 1.1  | 
|
| 177 | 
 
 1.1  | 
|
| 183 | 
 
 1.1  | 
|
| 185 | 
 
 1.1  | 
|
| 190 | 
 
 1.1 2.2 3.3  | 
|
| 194 | 
 
 1.1  | 
|
| 195 | 
 
 1.1  | 
|
| 200 | 
 
 1.1  | 
|
| 201 | 
 
 1.1  | 
|
| 202 | 
 
 1.1  | 
|
| 207 | 
 
 1.1  | 
|
| 208 | 
 
 1.1  | 
|
| 212 | 
 
 1.1  | 
|
| 213 | 
 
 1.1  | 
|
| 214 | 
 
 1.1  | 
|
| 215 | 
 
 1.1  | 
|
| 220 | 
 
 1.1  |