| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.User; | |
| 4 | import edu.ucsb.cs156.dining.entities.Reviews; | |
| 5 | import edu.ucsb.cs156.dining.entities.MenuItem; | |
| 6 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 7 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 8 | import edu.ucsb.cs156.dining.repositories.ReviewsRepository; | |
| 9 | import edu.ucsb.cs156.dining.repositories.MenuItemRepository; | |
| 10 | ||
| 11 | import io.swagger.v3.oas.annotations.Operation; | |
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | ||
| 16 | import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | import org.springframework.format.annotation.DateTimeFormat; | |
| 18 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 19 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 20 | import org.springframework.web.bind.annotation.GetMapping; | |
| 21 | import org.springframework.web.bind.annotation.PostMapping; | |
| 22 | import org.springframework.web.bind.annotation.PutMapping; | |
| 23 | import org.springframework.web.bind.annotation.RequestBody; | |
| 24 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 25 | import org.springframework.web.bind.annotation.RequestParam; | |
| 26 | import org.springframework.web.bind.annotation.RestController; | |
| 27 | import org.springframework.security.core.userdetails.UserDetails; | |
| 28 | import org.springframework.http.HttpStatus; | |
| 29 | import org.springframework.web.server.ResponseStatusException; | |
| 30 | ||
| 31 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 32 | ||
| 33 | import org.springframework.security.web.bind.annotation.AuthenticationPrincipal; | |
| 34 | import org.springframework.web.bind.annotation.PathVariable; | |
| 35 | import org.hibernate.annotations.CreationTimestamp; | |
| 36 | import org.hibernate.annotations.UpdateTimestamp; | |
| 37 | ||
| 38 | import java.time.LocalDate; | |
| 39 | import java.time.LocalDateTime; | |
| 40 | import java.util.ArrayList; | |
| 41 | import jakarta.validation.Valid; | |
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | /** | |
| 46 | * This is a REST controller for Reviews | |
| 47 | */ | |
| 48 | ||
| 49 | @Tag(name = "Reviews") | |
| 50 | @RequestMapping("/api/reviews") | |
| 51 | @RestController | |
| 52 | @Slf4j | |
| 53 | public class ReviewsController extends ApiController { | |
| 54 | ||
| 55 | @Autowired | |
| 56 | ReviewsRepository reviewsRepository; | |
| 57 | ||
| 58 | @Autowired | |
| 59 | MenuItemRepository menuItemRepository; | |
| 60 | ||
| 61 | /** | |
| 62 | * THis method returns a list of all reviews. | |
| 63 | * @return a list of all reviews | |
| 64 | */ | |
| 65 | @Operation(summary= "List all ucsb reviews of dining commons") | |
| 66 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 67 | @GetMapping("/all") | |
| 68 | public Iterable<Reviews> allReviews() { | |
| 69 | Iterable<Reviews> reviews = reviewsRepository.findAll(); | |
| 70 |
1
1. allReviews : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewsController::allReviews → KILLED |
return reviews; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * This method creates a new review. Accessible only to users with the role "ROLE_ADMIN". | |
| 75 | * @param item_id itemID of the review | |
| 76 | * @param date_served date served | |
| 77 | * @return the save review | |
| 78 | */ | |
| 79 | @Operation(summary= "Create a new review") | |
| 80 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 81 | @PostMapping("/post") | |
| 82 | public Reviews postReview( | |
| 83 | @Parameter(name="item_id") @RequestParam int item_id, | |
| 84 | @Parameter(name="rating") @RequestParam int rating, | |
| 85 | @Parameter(name="comments") @RequestParam String comments, | |
| 86 | @Parameter(name="date_served", description="date (in iso format, e.g. 2024-08-24T11:11:11; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("date_served") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date_served) | |
| 87 | throws JsonProcessingException | |
| 88 | ||
| 89 | { | |
| 90 | ||
| 91 |
4
1. postReview : negated conditional → KILLED 2. postReview : negated conditional → KILLED 3. postReview : changed conditional boundary → KILLED 4. postReview : changed conditional boundary → KILLED |
if (rating <= 0 || rating >= 6) { |
| 92 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Rating must be between 0 and 5"); | |
| 93 | } | |
| 94 | ||
| 95 | MenuItem menuItem = menuItemRepository.findById((long)item_id) | |
| 96 |
1
1. lambda$postReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewsController::lambda$postReview$0 → KILLED |
.orElseThrow(() -> new ResponseStatusException( |
| 97 | HttpStatus.NOT_FOUND, "No MenuItem with itemId: " + item_id + " found")); | |
| 98 | ||
| 99 | Reviews reviews = new Reviews(); | |
| 100 | CurrentUser user = getCurrentUser(); | |
| 101 | | |
| 102 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setItem_id → KILLED |
reviews.setItem_id(item_id); |
| 103 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setRating → KILLED |
reviews.setRating(rating); |
| 104 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setComments → KILLED |
reviews.setComments(comments); |
| 105 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setDate_served → KILLED |
reviews.setDate_served(date_served); |
| 106 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setStatus → KILLED |
reviews.setStatus("Awaiting Moderation"); |
| 107 |
1
1. postReview : removed call to edu/ucsb/cs156/dining/entities/Reviews::setUserId → KILLED |
reviews.setUserId(user.getUser().getId()); |
| 108 | ||
| 109 | Reviews savedReviews = reviewsRepository.save(reviews); | |
| 110 | ||
| 111 |
1
1. postReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/ReviewsController::postReview → KILLED |
return savedReviews; |
| 112 | } | |
| 113 | | |
| 114 | /** | |
| 115 | * This method returns all reviews from current user. | |
| 116 | * @return all reviews from current user. | |
| 117 | */ | |
| 118 | @Operation(summary = "Get reviews from an user") | |
| 119 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 120 | @GetMapping("") | |
| 121 | public Iterable<Reviews> getByCurrUserId() { | |
| 122 | CurrentUser user = getCurrentUser(); | |
| 123 | long currUserId = user.getUser().getId(); | |
| 124 | Iterable<Reviews> reviews = reviewsRepository.findByUserId(currUserId); | |
| 125 |
1
1. getByCurrUserId : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/controllers/ReviewsController::getByCurrUserId → KILLED |
return reviews; |
| 126 | } | |
| 127 | ||
| 128 | } | |
Mutations | ||
| 70 |
1.1 |
|
| 91 |
1.1 2.2 3.3 4.4 |
|
| 96 |
1.1 |
|
| 102 |
1.1 |
|
| 103 |
1.1 |
|
| 104 |
1.1 |
|
| 105 |
1.1 |
|
| 106 |
1.1 |
|
| 107 |
1.1 |
|
| 111 |
1.1 |
|
| 125 |
1.1 |