| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.dining.entities.MenuItem; | |
| 5 | import edu.ucsb.cs156.dining.entities.Review; | |
| 6 | import edu.ucsb.cs156.dining.entities.User; | |
| 7 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 8 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 9 | import edu.ucsb.cs156.dining.models.EditedReview; | |
| 10 | import edu.ucsb.cs156.dining.repositories.MenuItemRepository; | |
| 11 | import edu.ucsb.cs156.dining.repositories.ReviewRepository; | |
| 12 | import edu.ucsb.cs156.dining.services.ModeratorNotificationService; | |
| 13 | import edu.ucsb.cs156.dining.statuses.ModerationStatus; | |
| 14 | import io.swagger.v3.oas.annotations.Operation; | |
| 15 | import io.swagger.v3.oas.annotations.Parameter; | |
| 16 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 17 | import jakarta.validation.Valid; | |
| 18 | import java.time.LocalDateTime; | |
| 19 | import java.util.HashMap; | |
| 20 | import java.util.Map; | |
| 21 | import lombok.extern.slf4j.Slf4j; | |
| 22 | import org.springframework.beans.factory.annotation.Autowired; | |
| 23 | import org.springframework.format.annotation.DateTimeFormat; | |
| 24 | import org.springframework.http.ResponseEntity; | |
| 25 | import org.springframework.security.access.AccessDeniedException; | |
| 26 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 27 | import org.springframework.security.core.GrantedAuthority; | |
| 28 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 29 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 30 | import org.springframework.web.bind.annotation.GetMapping; | |
| 31 | import org.springframework.web.bind.annotation.PathVariable; | |
| 32 | import org.springframework.web.bind.annotation.PostMapping; | |
| 33 | import org.springframework.web.bind.annotation.PutMapping; | |
| 34 | import org.springframework.web.bind.annotation.RequestBody; | |
| 35 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 36 | import org.springframework.web.bind.annotation.RequestParam; | |
| 37 | import org.springframework.web.bind.annotation.RestController; | |
| 38 | ||
| 39 | /** This is a REST controller for Reviews */ | |
| 40 | @Tag(name = "Review") | |
| 41 | @RequestMapping("/api/reviews") | |
| 42 | @RestController | |
| 43 | @Slf4j | |
| 44 | public class ReviewController extends ApiController { | |
| 45 | ||
| 46 | private boolean hasReviewerComments(String reviewerComments) { | |
| 47 |
3
1. hasReviewerComments : replaced boolean return with true for edu/ucsb/cs156/dining/controllers/ReviewController::hasReviewerComments → KILLED 2. hasReviewerComments : negated conditional → KILLED 3. hasReviewerComments : negated conditional → KILLED |
return reviewerComments != null && !reviewerComments.trim().isEmpty(); |
| 48 | } | |
| 49 | ||
| 50 | @ExceptionHandler(IllegalArgumentException.class) | |
| 51 | public ResponseEntity<Map<String, String>> handleValidationExceptions( | |
| 52 | IllegalArgumentException ex) { | |
| 53 | Map<String, String> errors = new HashMap<>(); | |
| 54 | errors.put("error", ex.getMessage()); | |
| 55 |
1
1. handleValidationExceptions : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::handleValidationExceptions → KILLED |
return ResponseEntity.badRequest().body(errors); |
| 56 | } | |
| 57 | ||
| 58 | @Autowired ReviewRepository reviewRepository; | |
| 59 | ||
| 60 | @Autowired MenuItemRepository menuItemRepository; | |
| 61 | ||
| 62 | @Autowired ModeratorNotificationService moderatorNotificationService; | |
| 63 | ||
| 64 | /** | |
| 65 | * This method returns a list of all Reviews. | |
| 66 | * | |
| 67 | * @return a list of all Reviews | |
| 68 | */ | |
| 69 | @Operation(summary = "List all Reviews") | |
| 70 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 | @GetMapping("/all") | |
| 72 | public Iterable<Review> allReviews() { | |
| 73 | log.info("Attempting to log all reviews"); | |
| 74 | Iterable<Review> reviews = reviewRepository.findAll(); | |
| 75 | log.info("all reviews found, ", reviews); | |
| 76 |
1
1. allReviews : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewController::allReviews → KILLED |
return reviews; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * This method returns a list of all Reviews. | |
| 81 | * | |
| 82 | * @return a list of all Reviews | |
| 83 | */ | |
| 84 | @Operation(summary = "List all approved reviews for a specific item") | |
| 85 | @GetMapping("/approved/forItem/{itemId}") | |
| 86 | public Iterable<Review> allApprovedReviewsForItem( | |
| 87 | @Parameter(name = "itemId") @PathVariable("itemId") long itemId) { | |
| 88 | log.info("Attempting to log all approved reviews for item with id: {}", itemId); | |
| 89 | MenuItem item = | |
| 90 | menuItemRepository | |
| 91 | .findById(itemId) | |
| 92 |
1
1. lambda$allApprovedReviewsForItem$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$allApprovedReviewsForItem$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(MenuItem.class, itemId)); |
| 93 | Iterable<Review> reviews = | |
| 94 | reviewRepository.findByItemAndStatus(item, ModerationStatus.APPROVED); | |
| 95 | log.info("all approved reviews for item found, ", reviews); | |
| 96 |
1
1. allApprovedReviewsForItem : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewController::allApprovedReviewsForItem → KILLED |
return reviews; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * This method allows a user to submit a review | |
| 101 | * | |
| 102 | * @return message that says an review was added to the database | |
| 103 | * @param itemId id of the item | |
| 104 | * @param dateItemServed localDataTime All others params must not be parameters and instead | |
| 105 | * derived from data sources that are dynamic (Date), or set to be null or some other | |
| 106 | * signifier | |
| 107 | */ | |
| 108 | @Operation(summary = "Create a new review") | |
| 109 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 110 | @PostMapping("/post") | |
| 111 | public Review postReview( | |
| 112 | @Parameter(name = "itemId") @RequestParam long itemId, | |
| 113 | @Parameter(description = "Comments by the reviewer, can be blank") | |
| 114 | @RequestParam(required = false) | |
| 115 | String reviewerComments, | |
| 116 | @Parameter(name = "itemsStars") @RequestParam Long itemsStars, | |
| 117 | @Parameter( | |
| 118 | name = "dateItemServed", | |
| 119 | description = | |
| 120 | "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") | |
| 121 | @RequestParam("dateItemServed") | |
| 122 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 123 | LocalDateTime dateItemServed) // For | |
| 124 | throws JsonProcessingException { | |
| 125 | LocalDateTime now = LocalDateTime.now(); | |
| 126 | Review review = new Review(); | |
| 127 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setDateItemServed → KILLED |
review.setDateItemServed(dateItemServed); |
| 128 | ||
| 129 |
1
1. postReview : negated conditional → KILLED |
if (hasReviewerComments(reviewerComments)) { |
| 130 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setReviewerComments → KILLED |
review.setReviewerComments(reviewerComments); |
| 131 | } else { | |
| 132 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setStatus → KILLED |
review.setStatus(ModerationStatus.APPROVED); |
| 133 | } | |
| 134 | ||
| 135 | // Ensure user inputs rating 1-5 | |
| 136 |
4
1. postReview : negated conditional → KILLED 2. postReview : changed conditional boundary → KILLED 3. postReview : negated conditional → KILLED 4. postReview : changed conditional boundary → KILLED |
if (itemsStars < 1 || itemsStars > 5) { |
| 137 | throw new IllegalArgumentException("Items stars must be between 1 and 5."); | |
| 138 | } | |
| 139 | ||
| 140 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setItemsStars → KILLED |
review.setItemsStars(itemsStars); |
| 141 | ||
| 142 | MenuItem reviewedItem = | |
| 143 | menuItemRepository | |
| 144 | .findById(itemId) | |
| 145 |
1
1. lambda$postReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$postReview$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(MenuItem.class, itemId)); |
| 146 | ||
| 147 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setItem → KILLED |
review.setItem(reviewedItem); |
| 148 | CurrentUser user = getCurrentUser(); | |
| 149 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Review::setReviewer → KILLED |
review.setReviewer(user.getUser()); |
| 150 | log.info("reviews={}", review); | |
| 151 |
1
1. postReview : negated conditional → KILLED |
boolean needsModeration = review.getStatus() == ModerationStatus.AWAITING_REVIEW; |
| 152 | review = reviewRepository.save(review); | |
| 153 |
1
1. postReview : negated conditional → KILLED |
if (needsModeration) { |
| 154 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/services/ModeratorNotificationService::notifyModerators → KILLED |
moderatorNotificationService.notifyModerators(review); |
| 155 | } | |
| 156 |
1
1. postReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::postReview → KILLED |
return review; |
| 157 | } | |
| 158 | ||
| 159 | /** | |
| 160 | * This method allows a user to get a list of reviews that they have previously made. Only user | |
| 161 | * can only get a list of their own reviews, and you cant request another persons reviews | |
| 162 | * | |
| 163 | * @return a list of reviews sent by a given user | |
| 164 | */ | |
| 165 | @Operation(summary = "Get all reviews a user has sent: only callable by the user") | |
| 166 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 167 | @GetMapping("/userReviews") | |
| 168 | public Iterable<Review> get_all_review_by_user_id() { | |
| 169 | CurrentUser user = getCurrentUser(); | |
| 170 | Iterable<Review> reviews = reviewRepository.findByReviewer(user.getUser()); | |
| 171 |
1
1. get_all_review_by_user_id : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewController::get_all_review_by_user_id → KILLED |
return reviews; |
| 172 | } | |
| 173 | ||
| 174 | @Operation(summary = "Edit a review") | |
| 175 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 176 | @PutMapping("/reviewer") | |
| 177 | public Review editReview(@Parameter Long id, @RequestBody @Valid EditedReview incoming) { | |
| 178 | ||
| 179 | Review oldReview = | |
| 180 | reviewRepository | |
| 181 | .findById(id) | |
| 182 |
1
1. lambda$editReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$editReview$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Review.class, id)); |
| 183 | User current = getCurrentUser().getUser(); | |
| 184 |
1
1. editReview : negated conditional → KILLED |
if (current.getId() != oldReview.getReviewer().getId()) { |
| 185 | throw new AccessDeniedException("No permission to edit review"); | |
| 186 | } | |
| 187 | ||
| 188 |
4
1. editReview : negated conditional → KILLED 2. editReview : changed conditional boundary → KILLED 3. editReview : negated conditional → KILLED 4. editReview : changed conditional boundary → KILLED |
if (incoming.getItemStars() < 1 || incoming.getItemStars() > 5) { |
| 189 | throw new IllegalArgumentException("Items stars must be between 1 and 5."); | |
| 190 | } else { | |
| 191 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setItemsStars → KILLED |
oldReview.setItemsStars(incoming.getItemStars()); |
| 192 | } | |
| 193 | ||
| 194 |
1
1. editReview : negated conditional → KILLED |
if (hasReviewerComments(incoming.getReviewerComments())) { |
| 195 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setReviewerComments → KILLED |
oldReview.setReviewerComments(incoming.getReviewerComments()); |
| 196 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setStatus → KILLED |
oldReview.setStatus(ModerationStatus.AWAITING_REVIEW); |
| 197 | } else { | |
| 198 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setReviewerComments → KILLED |
oldReview.setReviewerComments(null); |
| 199 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setStatus → KILLED |
oldReview.setStatus(ModerationStatus.APPROVED); |
| 200 | } | |
| 201 | ||
| 202 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setDateItemServed → KILLED |
oldReview.setDateItemServed(incoming.getDateItemServed()); |
| 203 | ||
| 204 |
1
1. editReview : removed call to edu/ucsb/cs156/dining/entities/Review::setModeratorComments → KILLED |
oldReview.setModeratorComments(null); |
| 205 | ||
| 206 | Review review = reviewRepository.save(oldReview); | |
| 207 | ||
| 208 |
1
1. editReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::editReview → KILLED |
return review; |
| 209 | } | |
| 210 | ||
| 211 | @Operation(summary = "Delete a review") | |
| 212 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 213 | @DeleteMapping("/reviewer") | |
| 214 | public Object deleteReview(@Parameter Long id) { | |
| 215 | Review review = | |
| 216 | reviewRepository | |
| 217 | .findById(id) | |
| 218 |
1
1. lambda$deleteReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$deleteReview$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Review.class, id)); |
| 219 | ||
| 220 | CurrentUser currentUser = getCurrentUser(); | |
| 221 | User current = currentUser.getUser(); | |
| 222 | boolean currentUserIsAdmin = | |
| 223 | currentUser.getRoles().stream() | |
| 224 | .map(GrantedAuthority::getAuthority) | |
| 225 | .anyMatch("ROLE_ADMIN"::equals); | |
| 226 |
2
1. deleteReview : negated conditional → KILLED 2. deleteReview : negated conditional → KILLED |
if (current.getId() != review.getReviewer().getId() && !currentUserIsAdmin) { |
| 227 | throw new AccessDeniedException("No permission to delete review"); | |
| 228 | } | |
| 229 | ||
| 230 |
1
1. deleteReview : removed call to edu/ucsb/cs156/dining/repositories/ReviewRepository::delete → KILLED |
reviewRepository.delete(review); |
| 231 |
1
1. deleteReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::deleteReview → KILLED |
return genericMessage("Review with id %s deleted".formatted(id)); |
| 232 | } | |
| 233 | ||
| 234 | @Operation(summary = "Moderate a review") | |
| 235 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_MODERATOR')") | |
| 236 | @PutMapping("/moderate") | |
| 237 | public Review moderateReview( | |
| 238 | @Parameter Long id, @Parameter ModerationStatus status, @Parameter String moderatorComments) { | |
| 239 | Review review = | |
| 240 | reviewRepository | |
| 241 | .findById(id) | |
| 242 |
1
1. lambda$moderateReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$moderateReview$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Review.class, id)); |
| 243 | ||
| 244 |
1
1. moderateReview : removed call to edu/ucsb/cs156/dining/entities/Review::setModeratorComments → KILLED |
review.setModeratorComments(moderatorComments); |
| 245 |
1
1. moderateReview : removed call to edu/ucsb/cs156/dining/entities/Review::setStatus → KILLED |
review.setStatus(status); |
| 246 | ||
| 247 | review = reviewRepository.save(review); | |
| 248 |
1
1. moderateReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::moderateReview → KILLED |
return review; |
| 249 | } | |
| 250 | ||
| 251 | @Operation(summary = "See reviews that need moderation") | |
| 252 | @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_MODERATOR')") | |
| 253 | @GetMapping("/needsmoderation") | |
| 254 | public Iterable<Review> needsmoderation() { | |
| 255 | Iterable<Review> reviewsList = reviewRepository.findByStatus(ModerationStatus.AWAITING_REVIEW); | |
| 256 |
1
1. needsmoderation : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewController::needsmoderation → KILLED |
return reviewsList; |
| 257 | } | |
| 258 | ||
| 259 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 260 | @GetMapping("/{id}") | |
| 261 | public Review getReviewById(@PathVariable Long id) { | |
| 262 | ||
| 263 | // 1. Look up the review or 404 | |
| 264 | Review review = | |
| 265 | reviewRepository | |
| 266 | .findById(id) | |
| 267 |
1
1. lambda$getReviewById$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$getReviewById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Review.class, id)); |
| 268 | ||
| 269 | // 2. Get current requester | |
| 270 | User requester = getCurrentUser().getUser(); | |
| 271 | ||
| 272 | // 3. Evaluate permissions | |
| 273 |
1
1. getReviewById : negated conditional → KILLED |
boolean isOwner = (review.getReviewer().getId() == requester.getId()); |
| 274 | boolean isAdmin = | |
| 275 |
2
1. lambda$getReviewById$1 : replaced boolean return with false for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$getReviewById$1 → KILLED 2. lambda$getReviewById$1 : replaced boolean return with true for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$getReviewById$1 → KILLED |
getCurrentUser().getRoles().stream().anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN")); |
| 276 | boolean isModerator = | |
| 277 | getCurrentUser().getRoles().stream() | |
| 278 |
2
1. lambda$getReviewById$2 : replaced boolean return with false for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$getReviewById$2 → KILLED 2. lambda$getReviewById$2 : replaced boolean return with true for edu/ucsb/cs156/dining/controllers/ReviewController::lambda$getReviewById$2 → KILLED |
.anyMatch(r -> r.getAuthority().equals("ROLE_MODERATOR")); |
| 279 | ||
| 280 | // 4. Enforce access rules | |
| 281 |
3
1. getReviewById : negated conditional → KILLED 2. getReviewById : negated conditional → KILLED 3. getReviewById : negated conditional → KILLED |
if (!(isOwner || isAdmin || isModerator)) { |
| 282 | throw new AccessDeniedException("No permission to view this review"); | |
| 283 | } | |
| 284 | ||
| 285 | // 5. Return the review | |
| 286 |
1
1. getReviewById : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewController::getReviewById → KILLED |
return review; |
| 287 | } | |
| 288 | } | |
Mutations | ||
| 47 |
1.1 2.2 3.3 |
|
| 55 |
1.1 |
|
| 76 |
1.1 |
|
| 92 |
1.1 |
|
| 96 |
1.1 |
|
| 127 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 132 |
1.1 |
|
| 136 |
1.1 2.2 3.3 4.4 |
|
| 140 |
1.1 |
|
| 145 |
1.1 |
|
| 147 |
1.1 |
|
| 149 |
1.1 |
|
| 151 |
1.1 |
|
| 153 |
1.1 |
|
| 154 |
1.1 |
|
| 156 |
1.1 |
|
| 171 |
1.1 |
|
| 182 |
1.1 |
|
| 184 |
1.1 |
|
| 188 |
1.1 2.2 3.3 4.4 |
|
| 191 |
1.1 |
|
| 194 |
1.1 |
|
| 195 |
1.1 |
|
| 196 |
1.1 |
|
| 198 |
1.1 |
|
| 199 |
1.1 |
|
| 202 |
1.1 |
|
| 204 |
1.1 |
|
| 208 |
1.1 |
|
| 218 |
1.1 |
|
| 226 |
1.1 2.2 |
|
| 230 |
1.1 |
|
| 231 |
1.1 |
|
| 242 |
1.1 |
|
| 244 |
1.1 |
|
| 245 |
1.1 |
|
| 248 |
1.1 |
|
| 256 |
1.1 |
|
| 267 |
1.1 |
|
| 273 |
1.1 |
|
| 275 |
1.1 2.2 |
|
| 278 |
1.1 2.2 |
|
| 281 |
1.1 2.2 3.3 |
|
| 286 |
1.1 |