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