1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
6 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
7 | import edu.ucsb.cs156.happiercows.entities.User; | |
8 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
9 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
10 | import edu.ucsb.cs156.happiercows.models.CreateCommonsParams; | |
11 | import edu.ucsb.cs156.happiercows.models.HealthUpdateStrategyList; | |
12 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
13 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
14 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategies; | |
15 | import io.swagger.v3.oas.annotations.tags.Tag; | |
16 | import io.swagger.v3.oas.annotations.Operation; | |
17 | import io.swagger.v3.oas.annotations.Parameter; | |
18 | import lombok.extern.slf4j.Slf4j; | |
19 | import org.springframework.beans.factory.annotation.Autowired; | |
20 | import org.springframework.http.HttpStatus; | |
21 | import org.springframework.http.ResponseEntity; | |
22 | import org.springframework.security.access.prepost.PreAuthorize; | |
23 | import org.springframework.web.bind.annotation.*; | |
24 | import edu.ucsb.cs156.happiercows.services.CommonsPlusBuilderService; | |
25 | ||
26 | ||
27 | import java.util.Optional; | |
28 | ||
29 | ||
30 | @Slf4j | |
31 | @Tag(name = "Commons") | |
32 | @RequestMapping("/api/commons") | |
33 | @RestController | |
34 | public class CommonsController extends ApiController { | |
35 | @Autowired | |
36 | private CommonsRepository commonsRepository; | |
37 | ||
38 | @Autowired | |
39 | private UserCommonsRepository userCommonsRepository; | |
40 | ||
41 | @Autowired | |
42 | ObjectMapper mapper; | |
43 | ||
44 | @Autowired | |
45 | CommonsPlusBuilderService commonsPlusBuilderService; | |
46 | ||
47 | ||
48 | ||
49 | @Operation(summary = "Get a list of all commons") | |
50 | @GetMapping("/all") | |
51 | public ResponseEntity<String> getCommons() throws JsonProcessingException { | |
52 | log.info("getCommons()..."); | |
53 | Iterable<Commons> commons = commonsRepository.findAll(); | |
54 | String body = mapper.writeValueAsString(commons); | |
55 |
1
1. getCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommons → KILLED |
return ResponseEntity.ok().body(body); |
56 | } | |
57 | ||
58 | @Operation(summary = "Get a list of all commons and number of cows/users") | |
59 | @GetMapping("/allplus") | |
60 | public ResponseEntity<String> getCommonsPlus() throws JsonProcessingException { | |
61 | log.info("getCommonsPlus()..."); | |
62 | Iterable<Commons> commonsListIter = commonsRepository.findAll(); | |
63 | ||
64 | // convert Iterable to List for the purposes of using a Java Stream & lambda | |
65 | // below | |
66 | Iterable<CommonsPlus> commonsPlusList = commonsPlusBuilderService.convertToCommonsPlus(commonsListIter); | |
67 | ||
68 | String body = mapper.writeValueAsString(commonsPlusList); | |
69 |
1
1. getCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlus → KILLED |
return ResponseEntity.ok().body(body); |
70 | } | |
71 | ||
72 | @Operation(summary = "Get the number of cows/users in a commons") | |
73 | @PreAuthorize("hasRole('ROLE_USER')") | |
74 | @GetMapping("/plus") | |
75 | public CommonsPlus getCommonsPlusById( | |
76 | @Parameter(name="id") @RequestParam long id) throws JsonProcessingException { | |
77 | CommonsPlus commonsPlus = commonsPlusBuilderService.toCommonsPlus(commonsRepository.findById(id) | |
78 |
1
1. lambda$getCommonsPlusById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsPlusById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id))); |
79 | ||
80 |
1
1. getCommonsPlusById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlusById → KILLED |
return commonsPlus; |
81 | } | |
82 | ||
83 | @Operation(summary = "Update a commons") | |
84 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
85 | @PutMapping("/update") | |
86 | public ResponseEntity<String> updateCommons( | |
87 | @Parameter(name="commons identifier") @RequestParam long id, | |
88 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
89 | ) { | |
90 | Optional<Commons> existing = commonsRepository.findById(id); | |
91 | ||
92 | Commons updated; | |
93 | HttpStatus status; | |
94 | ||
95 |
1
1. updateCommons : negated conditional → KILLED |
if (existing.isPresent()) { |
96 | updated = existing.get(); | |
97 | status = HttpStatus.NO_CONTENT; | |
98 | } else { | |
99 | updated = new Commons(); | |
100 | status = HttpStatus.CREATED; | |
101 | } | |
102 | ||
103 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setName → KILLED |
updated.setName(params.getName()); |
104 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCowPrice → KILLED |
updated.setCowPrice(params.getCowPrice()); |
105 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setMilkPrice → KILLED |
updated.setMilkPrice(params.getMilkPrice()); |
106 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingBalance → KILLED |
updated.setStartingBalance(params.getStartingBalance()); |
107 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingDate → KILLED |
updated.setStartingDate(params.getStartingDate()); |
108 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
109 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
110 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCapacityPerUser → KILLED |
updated.setCapacityPerUser(params.getCapacityPerUser()); |
111 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
112 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
113 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setAboveCapacityHealthUpdateStrategy → KILLED |
updated.setAboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); |
114 | } | |
115 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
116 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setBelowCapacityHealthUpdateStrategy → KILLED |
updated.setBelowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); |
117 | } | |
118 | ||
119 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
120 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
121 | } | |
122 | ||
123 | // Reference: frontend/src/main/components/Commons/CommonsForm.js | |
124 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getName().equals("")) { |
125 | throw new IllegalArgumentException("Name cannot be empty"); | |
126 | } | |
127 | ||
128 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getCowPrice() < 0.01) { |
129 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
130 | } | |
131 | ||
132 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getMilkPrice() < 0.01) { |
133 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
134 | } | |
135 | ||
136 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getStartingBalance() < 0) { |
137 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
138 | } | |
139 | ||
140 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getCarryingCapacity() < 1) { |
141 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
142 | } | |
143 | commonsRepository.save(updated); | |
144 | ||
145 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
146 | } | |
147 | ||
148 | @Operation(summary = "Get a specific commons") | |
149 | @PreAuthorize("hasRole('ROLE_USER')") | |
150 | @GetMapping("") | |
151 | public Commons getCommonsById( | |
152 | @Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
153 | ||
154 | Commons commons = commonsRepository.findById(id) | |
155 |
1
1. lambda$getCommonsById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsById$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id)); |
156 | ||
157 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
158 | } | |
159 | ||
160 | @Operation(summary = "Create a new commons") | |
161 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
162 | @PostMapping(value = "/new", produces = "application/json") | |
163 | public ResponseEntity<String> createCommons( | |
164 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
165 | ) throws JsonProcessingException { | |
166 | ||
167 | var builder = Commons.builder() | |
168 | .name(params.getName()) | |
169 | .cowPrice(params.getCowPrice()) | |
170 | .milkPrice(params.getMilkPrice()) | |
171 | .startingBalance(params.getStartingBalance()) | |
172 | .startingDate(params.getStartingDate()) | |
173 | .degradationRate(params.getDegradationRate()) | |
174 | .showLeaderboard(params.getShowLeaderboard()) | |
175 | .capacityPerUser(params.getCapacityPerUser()) | |
176 | .carryingCapacity(params.getCarryingCapacity()); | |
177 | ||
178 | // ok to set null values for these, so old backend still works | |
179 |
1
1. createCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
180 | builder.aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); | |
181 | } | |
182 |
1
1. createCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
183 | builder.belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); | |
184 | } | |
185 | ||
186 | Commons commons = builder.build(); | |
187 | ||
188 | // Reference: frontend/src/main/components/Commons/CommonsForm.js | |
189 |
1
1. createCommons : negated conditional → KILLED |
if (params.getName().equals("")) { |
190 | throw new IllegalArgumentException("Name cannot be empty"); | |
191 | } | |
192 | ||
193 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getCowPrice() < 0.01) { |
194 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
195 | } | |
196 | ||
197 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getMilkPrice() < 0.01) { |
198 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
199 | } | |
200 | ||
201 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getStartingBalance() < 0) { |
202 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
203 | } | |
204 | ||
205 | // throw exception for degradation rate | |
206 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
207 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
208 | } | |
209 | ||
210 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getCarryingCapacity() < 1) { |
211 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
212 | } | |
213 | ||
214 | Commons saved = commonsRepository.save(commons); | |
215 | String body = mapper.writeValueAsString(saved); | |
216 | ||
217 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
218 | } | |
219 | ||
220 | ||
221 | @Operation(summary = "List all cow health update strategies") | |
222 | @PreAuthorize("hasRole('ROLE_USER')") | |
223 | @GetMapping("/all-health-update-strategies") | |
224 | public ResponseEntity<String> listCowHealthUpdateStrategies() throws JsonProcessingException { | |
225 | var result = HealthUpdateStrategyList.create(); | |
226 | String body = mapper.writeValueAsString(result); | |
227 |
1
1. listCowHealthUpdateStrategies : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::listCowHealthUpdateStrategies → KILLED |
return ResponseEntity.ok().body(body); |
228 | } | |
229 | ||
230 | @Operation(summary = "Join a commons") | |
231 | @PreAuthorize("hasRole('ROLE_USER')") | |
232 | @PostMapping(value = "/join", produces = "application/json") | |
233 | public ResponseEntity<String> joinCommon( | |
234 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws Exception { | |
235 | ||
236 | User u = getCurrentUser().getUser(); | |
237 | Long userId = u.getId(); | |
238 | String username = u.getFullName(); | |
239 | ||
240 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
241 |
1
1. lambda$joinCommon$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$joinCommon$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, commonsId)); |
242 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
243 | ||
244 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
245 | // user is already a member of this commons | |
246 | String body = mapper.writeValueAsString(joinedCommons); | |
247 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
248 | } | |
249 | ||
250 | UserCommons uc = UserCommons.builder() | |
251 | .user(u) | |
252 | .commons(joinedCommons) | |
253 | .username(username) | |
254 | .totalWealth(joinedCommons.getStartingBalance()) | |
255 | .numOfCows(0) | |
256 | .cowHealth(100) | |
257 | .cowsBought(0) | |
258 | .cowsSold(0) | |
259 | .cowDeaths(0) | |
260 | .build(); | |
261 | ||
262 | userCommonsRepository.save(uc); | |
263 | ||
264 | String body = mapper.writeValueAsString(joinedCommons); | |
265 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
266 | } | |
267 | ||
268 | @Operation(summary = "Delete a Commons") | |
269 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
270 | @DeleteMapping("") | |
271 | public Object deleteCommons( | |
272 | @Parameter(name="id") @RequestParam Long id) { | |
273 | ||
274 | commonsRepository.findById(id) | |
275 |
1
1. lambda$deleteCommons$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteCommons$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id)); |
276 | ||
277 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
278 | ||
279 | String responseString = String.format("commons with id %d deleted", id); | |
280 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
281 | ||
282 | } | |
283 | ||
284 | @Operation(summary="Delete a user from a commons") | |
285 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
286 | @DeleteMapping("/{commonsId}/users/{userId}") | |
287 | public Object deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
288 | @PathVariable("userId") Long userId) throws Exception { | |
289 | ||
290 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
291 |
1
1. lambda$deleteUserFromCommon$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteUserFromCommon$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException( |
292 | UserCommons.class, "commonsId", commonsId, "userId", userId) | |
293 | ); | |
294 | ||
295 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::delete → KILLED |
userCommonsRepository.delete(userCommons); |
296 | ||
297 | String responseString = String.format("user with id %d deleted from commons with id %d, %d users remain", userId, commonsId, commonsRepository.getNumUsers(commonsId).orElse(0)); | |
298 | ||
299 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return genericMessage(responseString); |
300 | } | |
301 | ||
302 | | |
303 | } | |
Mutations | ||
55 |
1.1 |
|
69 |
1.1 |
|
78 |
1.1 |
|
80 |
1.1 |
|
95 |
1.1 |
|
103 |
1.1 |
|
104 |
1.1 |
|
105 |
1.1 |
|
106 |
1.1 |
|
107 |
1.1 |
|
108 |
1.1 |
|
109 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
112 |
1.1 |
|
113 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
119 |
1.1 2.2 |
|
124 |
1.1 |
|
128 |
1.1 2.2 |
|
132 |
1.1 2.2 |
|
136 |
1.1 2.2 |
|
140 |
1.1 2.2 |
|
145 |
1.1 |
|
155 |
1.1 |
|
157 |
1.1 |
|
179 |
1.1 |
|
182 |
1.1 |
|
189 |
1.1 |
|
193 |
1.1 2.2 |
|
197 |
1.1 2.2 |
|
201 |
1.1 2.2 |
|
206 |
1.1 2.2 |
|
210 |
1.1 2.2 |
|
217 |
1.1 |
|
227 |
1.1 |
|
241 |
1.1 |
|
244 |
1.1 |
|
247 |
1.1 |
|
265 |
1.1 |
|
275 |
1.1 |
|
277 |
1.1 |
|
280 |
1.1 |
|
291 |
1.1 |
|
295 |
1.1 |
|
299 |
1.1 |