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