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