1 | package edu.ucsb.cs156.example.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.example.entities.Restaurant; | |
4 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
5 | import edu.ucsb.cs156.example.repositories.RestaurantRepository; | |
6 | import io.swagger.v3.oas.annotations.Operation; | |
7 | import io.swagger.v3.oas.annotations.Parameter; | |
8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
9 | import jakarta.validation.Valid; | |
10 | import org.springframework.beans.factory.annotation.Autowired; | |
11 | import org.springframework.security.access.prepost.PreAuthorize; | |
12 | import org.springframework.web.bind.annotation.DeleteMapping; | |
13 | import org.springframework.web.bind.annotation.GetMapping; | |
14 | import org.springframework.web.bind.annotation.PostMapping; | |
15 | import org.springframework.web.bind.annotation.PutMapping; | |
16 | import org.springframework.web.bind.annotation.RequestBody; | |
17 | import org.springframework.web.bind.annotation.RequestMapping; | |
18 | import org.springframework.web.bind.annotation.RequestParam; | |
19 | import org.springframework.web.bind.annotation.RestController; | |
20 | ||
21 | /** This is a REST controller for Restaurants */ | |
22 | @Tag(name = "Restaurants") | |
23 | @RequestMapping("/api/restaurants") | |
24 | @RestController | |
25 | public class RestaurantsController extends ApiController { | |
26 | ||
27 | @Autowired RestaurantRepository restaurantRepository; | |
28 | ||
29 | /** | |
30 | * This method returns a list of all restaurants. | |
31 | * | |
32 | * @return a list of all restaurants | |
33 | */ | |
34 | @Operation(summary = "List all restaurants") | |
35 | @PreAuthorize("hasRole('ROLE_USER')") | |
36 | @GetMapping("/all") | |
37 | public Iterable<Restaurant> allRestaurants() { | |
38 | Iterable<Restaurant> restaurants = restaurantRepository.findAll(); | |
39 |
1
1. allRestaurants : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/RestaurantsController::allRestaurants → KILLED |
return restaurants; |
40 | } | |
41 | ||
42 | /** | |
43 | * This method returns a single restaurant. | |
44 | * | |
45 | * @param id id of the restaurant to get | |
46 | * @return a single restaurant | |
47 | */ | |
48 | @Operation(summary = "Get a single restaurant") | |
49 | @PreAuthorize("hasRole('ROLE_USER')") | |
50 | @GetMapping("") | |
51 | public Restaurant getById(@Parameter(name = "id") @RequestParam Long id) { | |
52 | Restaurant restaurant = | |
53 | restaurantRepository | |
54 | .findById(id) | |
55 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id)); |
56 | ||
57 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::getById → KILLED |
return restaurant; |
58 | } | |
59 | ||
60 | /** | |
61 | * This method creates a new restaurant. Accessible only to users with the role "ROLE_ADMIN". | |
62 | * | |
63 | * @param name name of the restaurant | |
64 | * @param description description of the restaurant | |
65 | * @return the save restaurant (with it's id field set by the database) | |
66 | */ | |
67 | @Operation(summary = "Create a new restaurant") | |
68 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
69 | @PostMapping("/post") | |
70 | public Restaurant postRestaurant( | |
71 | @Parameter(name = "name") @RequestParam String name, | |
72 | @Parameter(name = "description") @RequestParam String description) { | |
73 | Restaurant restaurant = new Restaurant(); | |
74 | ||
75 |
1
1. postRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setName → KILLED |
restaurant.setName(name); |
76 |
1
1. postRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setDescription → KILLED |
restaurant.setDescription(description); |
77 | ||
78 | Restaurant savedrestaurant = restaurantRepository.save(restaurant); | |
79 |
1
1. postRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::postRestaurant → KILLED |
return savedrestaurant; |
80 | } | |
81 | ||
82 | /** | |
83 | * Deletes a restaurant. Accessible only to users with the role "ROLE_ADMIN". | |
84 | * | |
85 | * @param id id of the restaurant to delete | |
86 | * @return a message indicating that the restaurant was deleted | |
87 | */ | |
88 | @Operation(summary = "Delete a Restaurant") | |
89 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
90 | @DeleteMapping("") | |
91 | public Object deleteRestaurant(@Parameter(name = "id") @RequestParam Long id) { | |
92 | Restaurant restaurant = | |
93 | restaurantRepository | |
94 | .findById(id) | |
95 |
1
1. lambda$deleteRestaurant$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$deleteRestaurant$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id)); |
96 | ||
97 |
1
1. deleteRestaurant : removed call to edu/ucsb/cs156/example/repositories/RestaurantRepository::delete → KILLED |
restaurantRepository.delete(restaurant); |
98 |
1
1. deleteRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::deleteRestaurant → KILLED |
return genericMessage("Restaurant with id %s deleted".formatted(id)); |
99 | } | |
100 | ||
101 | /** | |
102 | * Update a single restaurant. Accessible only to users with the role "ROLE_ADMIN". | |
103 | * | |
104 | * @param id id of the restaurant to update | |
105 | * @param incoming the new restaurant contents | |
106 | * @return the updated restaurant object | |
107 | */ | |
108 | @Operation(summary = "Update a single restaurant") | |
109 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
110 | @PutMapping("") | |
111 | public Restaurant updateRestaurant( | |
112 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Restaurant incoming) { | |
113 | ||
114 | Restaurant restaurant = | |
115 | restaurantRepository | |
116 | .findById(id) | |
117 |
1
1. lambda$updateRestaurant$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::lambda$updateRestaurant$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Restaurant.class, id)); |
118 | ||
119 |
1
1. updateRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setName → KILLED |
restaurant.setName(incoming.getName()); |
120 |
1
1. updateRestaurant : removed call to edu/ucsb/cs156/example/entities/Restaurant::setDescription → KILLED |
restaurant.setDescription(incoming.getDescription()); |
121 | ||
122 | restaurantRepository.save(restaurant); | |
123 | ||
124 |
1
1. updateRestaurant : replaced return value with null for edu/ucsb/cs156/example/controllers/RestaurantsController::updateRestaurant → KILLED |
return restaurant; |
125 | } | |
126 | } | |
Mutations | ||
39 |
1.1 |
|
55 |
1.1 |
|
57 |
1.1 |
|
75 |
1.1 |
|
76 |
1.1 |
|
79 |
1.1 |
|
95 |
1.1 |
|
97 |
1.1 |
|
98 |
1.1 |
|
117 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
124 |
1.1 |