MenuItemReviewController.java

1
package edu.ucsb.cs156.dining.controllers;
2
3
import edu.ucsb.cs156.dining.entities.MenuItemReview;
4
import edu.ucsb.cs156.dining.entities.MenuItem;
5
import edu.ucsb.cs156.dining.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.dining.repositories.MenuItemReviewRepository;
7
import edu.ucsb.cs156.dining.services.CurrentUserService;
8
import edu.ucsb.cs156.dining.repositories.MenuItemRepository;
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import lombok.extern.slf4j.Slf4j;
13
14
import com.fasterxml.jackson.core.JsonProcessingException;
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.web.server.ResponseStatusException;
28
import org.springframework.http.HttpStatus;
29
30
import java.time.LocalDateTime;
31
32
33
34
/**
35
 * This is a REST controller for MenuItemReview
36
 */
37
38
@Tag(name = "MenuItemReview")
39
@RequestMapping("/api/menuitemreviews")
40
@RestController
41
@Slf4j
42
public class MenuItemReviewController extends ApiController {
43
44
    @Autowired
45
    MenuItemReviewRepository menuItemReviewRepository;
46
47
    @Autowired
48
    MenuItemRepository menuItemRepository;
49
50
    @Autowired
51
    private CurrentUserService currentUserService;
52
    
53
    /**
54
     * Create a new menu item review -> all users
55
     */
56
57
    @Operation(summary= "Create a new menu item review")
58
    @PreAuthorize("hasRole('ROLE_USER')")
59
    @PostMapping("/post")
60
    public MenuItemReview postMenuItemReview(
61
            @Parameter(name="itemId") @RequestParam long itemId,
62
            @Parameter(name="itemServedDate", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS") @RequestParam("itemServedDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime itemServedDate,
63
            @Parameter(name="rating", description="Leave a rating numerbed 1-5") @RequestParam int rating,
64
            @Parameter(name="reviewText") @RequestParam String reviewText)
65
    
66
            throws JsonProcessingException {
67
68
            MenuItem menuItem = menuItemRepository.findById(itemId).orElseThrow(() -> 
69 1 1. lambda$postMenuItemReview$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/MenuItemReviewController::lambda$postMenuItemReview$0 → KILLED
                new ResponseStatusException(HttpStatus.NOT_FOUND, "MenuItem with ID " + itemId + " not found"));
70
71
        log.info("itemServedDate={}", itemServedDate);
72
73
        long studentUserId = currentUserService.getCurrentUser().getUser().getId();
74
75
        MenuItemReview menuItemReview = new MenuItemReview();
76 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setStudentUserId → KILLED
        menuItemReview.setStudentUserId(studentUserId);
77 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setMenuItem → KILLED
        menuItemReview.setMenuItem(menuItem);
78 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setItemServedDate → KILLED
        menuItemReview.setItemServedDate(itemServedDate);
79 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setStatus → KILLED
        menuItemReview.setStatus("Awaiting Moderation");
80 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setRating → KILLED
        menuItemReview.setRating(rating);
81 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setReviewText → KILLED
        menuItemReview.setReviewText(reviewText);
82
83
        LocalDateTime now = LocalDateTime.now();
84 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setCreatedDate → KILLED
        menuItemReview.setCreatedDate(now);
85 1 1. postMenuItemReview : removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setLastEditedDate → KILLED
        menuItemReview.setLastEditedDate(now);
86
87
        MenuItemReview savedMenuItemReview = menuItemReviewRepository.save(menuItemReview);
88
89 1 1. postMenuItemReview : replaced return value with null for edu/ucsb/cs156/dining/controllers/MenuItemReviewController::postMenuItemReview → KILLED
        return savedMenuItemReview;
90
    }
91
92
}

Mutations

69

1.1
Location : lambda$postMenuItemReview$0
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_cannot_post_if_menu_item_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/MenuItemReviewController::lambda$postMenuItemReview$0 → KILLED

76

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setStudentUserId → KILLED

77

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setMenuItem → KILLED

78

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setItemServedDate → KILLED

79

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setStatus → KILLED

80

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setRating → KILLED

81

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setReviewText → KILLED

84

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setCreatedDate → KILLED

85

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_verify_setters()]
removed call to edu/ucsb/cs156/dining/entities/MenuItemReview::setLastEditedDate → KILLED

89

1.1
Location : postMenuItemReview
Killed by : edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.dining.controllers.MenuItemReviewControllerTests]/[method:logged_in_user_can_post_valid_menu_item_review()]
replaced return value with null for edu/ucsb/cs156/dining/controllers/MenuItemReviewController::postMenuItemReview → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0