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