| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Announcements; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 5 | import edu.ucsb.cs156.happiercows.repositories.AnnouncementsRepository; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 7 | ||
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 12 | ||
| 13 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 14 | ||
| 15 | import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | import org.springframework.format.annotation.DateTimeFormat; | |
| 17 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 18 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 19 | import org.springframework.web.bind.annotation.GetMapping; | |
| 20 | import org.springframework.web.bind.annotation.PostMapping; | |
| 21 | import org.springframework.web.bind.annotation.PutMapping; | |
| 22 | import org.springframework.web.bind.annotation.RequestBody; | |
| 23 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestParam; | |
| 25 | import org.springframework.web.bind.annotation.RestController; | |
| 26 | ||
| 27 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 28 | ||
| 29 | import javax.validation.Valid; | |
| 30 | ||
| 31 | import java.time.LocalDateTime; | |
| 32 | ||
| 33 | @Tag(name = "Announcements") | |
| 34 | @RequestMapping("/api/announcements") | |
| 35 | @RestController | |
| 36 | @Slf4j | |
| 37 | public class AnnouncementsController extends ApiController { | |
| 38 | ||
| 39 | @Autowired | |
| 40 | AnnouncementsRepository announcementsRepository; | |
| 41 | ||
| 42 | @Autowired | |
| 43 | CommonsRepository commonsRepository; | |
| 44 | ||
| 45 | @Operation(summary = "Create a new announcement") | |
| 46 | @PreAuthorize("hasRole('ADMIN')") | |
| 47 | @PostMapping("/post") | |
| 48 | public Announcements createAnnouncements( | |
| 49 | @Parameter(name="commonsId") @RequestParam Long commonsId, | |
| 50 | @Parameter(name="startTime",description="in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime, | |
| 51 | @Parameter(name="endTime",description="in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601") @RequestParam(value = "endTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime, | |
| 52 | @Parameter(name="announcement") @RequestParam String announcement | |
| 53 | ) | |
| 54 | throws JsonProcessingException { | |
| 55 | ||
| 56 | var builder = Announcements.builder() | |
| 57 | .commonsId(commonsId) | |
| 58 | .startTime(startTime) | |
| 59 | .endTime(endTime) | |
| 60 | .announcement(announcement); | |
| 61 | | |
| 62 | Announcements announcements = builder.build(); | |
| 63 | ||
| 64 | // Commons exists | |
| 65 | Commons commons = commonsRepository.findById(commonsId) | |
| 66 |
1
1. lambda$createAnnouncements$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$createAnnouncements$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, commonsId)); |
| 67 | ||
| 68 | // End is after Start | |
| 69 |
2
1. createAnnouncements : negated conditional → KILLED 2. createAnnouncements : negated conditional → KILLED |
if (endTime != null && endTime.isBefore(startTime)) { |
| 70 | throw new IllegalArgumentException("End cannot be before start"); | |
| 71 | } | |
| 72 | | |
| 73 | // Announcement is not empty | |
| 74 |
1
1. createAnnouncements : negated conditional → KILLED |
if (announcement.isEmpty()) { |
| 75 | throw new IllegalArgumentException("Announcement cannot be empty"); | |
| 76 | } | |
| 77 | ||
| 78 | // Save announcement | |
| 79 |
1
1. createAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::createAnnouncements → KILLED |
return announcementsRepository.save(announcements); |
| 80 | } | |
| 81 | ||
| 82 | @Operation(summary = "Get announcement by id") | |
| 83 | @PreAuthorize("hasAnyRole('ROLE_USER')") | |
| 84 | @GetMapping("/id") | |
| 85 | public Announcements getAnnouncementById(@Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
| 86 | Announcements announcement = announcementsRepository.findById(id) | |
| 87 | .orElseThrow( | |
| 88 |
1
1. lambda$getAnnouncementById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getAnnouncementById$1 → KILLED |
() -> new EntityNotFoundException(Announcements.class, id)); |
| 89 |
1
1. getAnnouncementById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementById → KILLED |
return announcement; |
| 90 | } | |
| 91 | ||
| 92 | @Operation(summary = "Get announcement by commons") | |
| 93 | @PreAuthorize("hasAnyRole('ROLE_USER')") | |
| 94 | @GetMapping("/commons") | |
| 95 | public Iterable<Announcements> getAnnouncementsByCommons(@Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 96 | // Commons exists | |
| 97 | Commons commons = commonsRepository.findById(commonsId) | |
| 98 |
1
1. lambda$getAnnouncementsByCommons$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$getAnnouncementsByCommons$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, commonsId)); |
| 99 | | |
| 100 |
1
1. getAnnouncementsByCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::getAnnouncementsByCommons → KILLED |
return announcementsRepository.findByCommonsId(commonsId); |
| 101 | } | |
| 102 | ||
| 103 | @Operation(summary = "Delete an announcement") | |
| 104 | @PreAuthorize("hasAnyRole('ROLE_ADMIN')") | |
| 105 | @DeleteMapping("") | |
| 106 | public Object deleteAnnouncements( | |
| 107 | @Parameter(description = "The id of the announcement") @RequestParam Long id) { | |
| 108 | ||
| 109 | // Get the announcement | |
| 110 | Announcements announcement = announcementsRepository.findById(id) | |
| 111 | .orElseThrow( | |
| 112 |
1
1. lambda$deleteAnnouncements$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$deleteAnnouncements$3 → KILLED |
() -> new EntityNotFoundException(Announcements.class, id)); |
| 113 | ||
| 114 | // Delete the announcement | |
| 115 |
1
1. deleteAnnouncements : removed call to edu/ucsb/cs156/happiercows/repositories/AnnouncementsRepository::delete → KILLED |
announcementsRepository.delete(announcement); |
| 116 | String responseString = String.format("announcement with id %d deleted", id); | |
| 117 |
1
1. deleteAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::deleteAnnouncements → KILLED |
return genericMessage(responseString); |
| 118 | } | |
| 119 | ||
| 120 | ||
| 121 | @Operation(summary = "Update an announcement") | |
| 122 | @PreAuthorize("hasAnyRole('ROLE_ADMIN')") | |
| 123 | @PutMapping("") | |
| 124 | public Announcements updateAnnouncements( | |
| 125 | @Parameter(name="id") @RequestParam Long id, | |
| 126 | @RequestBody @Valid Announcements incoming | |
| 127 | ) | |
| 128 | throws JsonProcessingException { | |
| 129 | | |
| 130 | Long commonsId = incoming.getCommonsId(); | |
| 131 | LocalDateTime startTime = incoming.getStartTime(); | |
| 132 | LocalDateTime endTime = incoming.getEndTime(); | |
| 133 | String announcement = incoming.getAnnouncement(); | |
| 134 | | |
| 135 | // Get the announcement | |
| 136 | Announcements announcements = announcementsRepository.findById(id) | |
| 137 | .orElseThrow( | |
| 138 |
1
1. lambda$updateAnnouncements$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$updateAnnouncements$4 → KILLED |
() -> new EntityNotFoundException(Announcements.class, id)); |
| 139 | ||
| 140 | // Commons exists | |
| 141 | Commons commons = commonsRepository.findById(commonsId) | |
| 142 |
1
1. lambda$updateAnnouncements$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::lambda$updateAnnouncements$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, commonsId)); |
| 143 | ||
| 144 | // End is after Start | |
| 145 |
2
1. updateAnnouncements : negated conditional → KILLED 2. updateAnnouncements : negated conditional → KILLED |
if (endTime != null && endTime.isBefore(startTime)) { |
| 146 | throw new IllegalArgumentException("End cannot be before start"); | |
| 147 | } | |
| 148 | | |
| 149 | // Announcement is not empty | |
| 150 |
1
1. updateAnnouncements : negated conditional → KILLED |
if (announcement.isEmpty()) { |
| 151 | throw new IllegalArgumentException("Announcement cannot be empty"); | |
| 152 | } | |
| 153 | ||
| 154 | // Update | |
| 155 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setCommonsId → KILLED |
announcements.setCommonsId(commonsId); |
| 156 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setStartTime → KILLED |
announcements.setStartTime(startTime); |
| 157 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setEndTime → KILLED |
announcements.setEndTime(endTime); |
| 158 |
1
1. updateAnnouncements : removed call to edu/ucsb/cs156/happiercows/entities/Announcements::setAnnouncement → KILLED |
announcements.setAnnouncement(announcement); |
| 159 | ||
| 160 | // Save announcement | |
| 161 | announcementsRepository.save(announcements); | |
| 162 |
1
1. updateAnnouncements : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/AnnouncementsController::updateAnnouncements → KILLED |
return announcements; |
| 163 | } | |
| 164 | | |
| 165 | ||
| 166 | ||
| 167 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 69 |
1.1 2.2 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 98 |
1.1 |
|
| 100 |
1.1 |
|
| 112 |
1.1 |
|
| 115 |
1.1 |
|
| 117 |
1.1 |
|
| 138 |
1.1 |
|
| 142 |
1.1 |
|
| 145 |
1.1 2.2 |
|
| 150 |
1.1 |
|
| 155 |
1.1 |
|
| 156 |
1.1 |
|
| 157 |
1.1 |
|
| 158 |
1.1 |
|
| 162 |
1.1 |