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 | ||
25 | import java.util.ArrayList; | |
26 | import java.util.List; | |
27 | import java.util.Optional; | |
28 | import java.util.stream.Collectors; | |
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 | @Operation(summary = "Get a list of all commons") | |
45 | @GetMapping("/all") | |
46 | public ResponseEntity<String> getCommons() throws JsonProcessingException { | |
47 | log.info("getCommons()..."); | |
48 | Iterable<Commons> commons = commonsRepository.findAll(); | |
49 | String body = mapper.writeValueAsString(commons); | |
50 |
1
1. getCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommons → KILLED |
return ResponseEntity.ok().body(body); |
51 | } | |
52 | ||
53 | @Operation(summary = "Get a list of all commons and number of cows/users") | |
54 | @GetMapping("/allplus") | |
55 | public ResponseEntity<String> getCommonsPlus() throws JsonProcessingException { | |
56 | log.info("getCommonsPlus()..."); | |
57 | Iterable<Commons> commonsListIter = commonsRepository.findAll(); | |
58 | ||
59 | // convert Iterable to List for the purposes of using a Java Stream & lambda | |
60 | // below | |
61 | List<Commons> commonsList = new ArrayList<Commons>(); | |
62 |
1
1. getCommonsPlus : removed call to java/lang/Iterable::forEach → KILLED |
commonsListIter.forEach(commonsList::add); |
63 | ||
64 | List<CommonsPlus> commonsPlusList1 = commonsList.stream() | |
65 |
1
1. lambda$getCommonsPlus$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsPlus$0 → KILLED |
.map(c -> toCommonsPlus(c)) |
66 | .collect(Collectors.toList()); | |
67 | ||
68 | ArrayList<CommonsPlus> commonsPlusList = new ArrayList<CommonsPlus>(commonsPlusList1); | |
69 | ||
70 | String body = mapper.writeValueAsString(commonsPlusList); | |
71 |
1
1. getCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlus → KILLED |
return ResponseEntity.ok().body(body); |
72 | } | |
73 | ||
74 | @Operation(summary = "Get the number of cows/users in a commons") | |
75 | @PreAuthorize("hasRole('ROLE_USER')") | |
76 | @GetMapping("/plus") | |
77 | public CommonsPlus getCommonsPlusById( | |
78 | @Parameter(name="id") @RequestParam long id) throws JsonProcessingException { | |
79 | CommonsPlus commonsPlus = toCommonsPlus(commonsRepository.findById(id) | |
80 |
1
1. lambda$getCommonsPlusById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsPlusById$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id))); |
81 | ||
82 |
1
1. getCommonsPlusById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlusById → KILLED |
return commonsPlus; |
83 | } | |
84 | ||
85 | @Operation(summary = "Update a commons") | |
86 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
87 | @PutMapping("/update") | |
88 | public ResponseEntity<String> updateCommons( | |
89 | @Parameter(name="commons identifier") @RequestParam long id, | |
90 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
91 | ) { | |
92 | Optional<Commons> existing = commonsRepository.findById(id); | |
93 | ||
94 | Commons updated; | |
95 | HttpStatus status; | |
96 | ||
97 |
1
1. updateCommons : negated conditional → KILLED |
if (existing.isPresent()) { |
98 | updated = existing.get(); | |
99 | status = HttpStatus.NO_CONTENT; | |
100 | } else { | |
101 | updated = new Commons(); | |
102 | status = HttpStatus.CREATED; | |
103 | } | |
104 | ||
105 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setName → KILLED |
updated.setName(params.getName()); |
106 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCowPrice → KILLED |
updated.setCowPrice(params.getCowPrice()); |
107 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setMilkPrice → KILLED |
updated.setMilkPrice(params.getMilkPrice()); |
108 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingBalance → KILLED |
updated.setStartingBalance(params.getStartingBalance()); |
109 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingDate → KILLED |
updated.setStartingDate(params.getStartingDate()); |
110 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
111 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
112 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
113 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCapacityPerUser → KILLED |
updated.setCapacityPerUser(params.getCapacityPerUser()); |
114 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
115 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setAboveCapacityHealthUpdateStrategy → KILLED |
updated.setAboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); |
116 | } | |
117 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
118 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setBelowCapacityHealthUpdateStrategy → KILLED |
updated.setBelowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); |
119 | } | |
120 | ||
121 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
122 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
123 | } | |
124 | ||
125 | commonsRepository.save(updated); | |
126 | ||
127 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
128 | } | |
129 | ||
130 | @Operation(summary = "Get a specific commons") | |
131 | @PreAuthorize("hasRole('ROLE_USER')") | |
132 | @GetMapping("") | |
133 | public Commons getCommonsById( | |
134 | @Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
135 | ||
136 | Commons commons = commonsRepository.findById(id) | |
137 |
1
1. lambda$getCommonsById$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsById$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id)); |
138 | ||
139 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
140 | } | |
141 | ||
142 | @Operation(summary = "Create a new commons") | |
143 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
144 | @PostMapping(value = "/new", produces = "application/json") | |
145 | public ResponseEntity<String> createCommons( | |
146 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
147 | ) throws JsonProcessingException { | |
148 | ||
149 | var builder = Commons.builder() | |
150 | .name(params.getName()) | |
151 | .cowPrice(params.getCowPrice()) | |
152 | .milkPrice(params.getMilkPrice()) | |
153 | .startingBalance(params.getStartingBalance()) | |
154 | .startingDate(params.getStartingDate()) | |
155 | .degradationRate(params.getDegradationRate()) | |
156 | .showLeaderboard(params.getShowLeaderboard()) | |
157 | .carryingCapacity(params.getCarryingCapacity()) | |
158 | .capacityPerUser(params.getCapacityPerUser()); | |
159 | ||
160 | // ok to set null values for these, so old backend still works | |
161 |
1
1. createCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
162 | builder.aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); | |
163 | } | |
164 |
1
1. createCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
165 | builder.belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); | |
166 | } | |
167 | ||
168 | Commons commons = builder.build(); | |
169 | ||
170 | ||
171 | // throw exception for degradation rate | |
172 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
173 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
174 | } | |
175 | ||
176 | Commons saved = commonsRepository.save(commons); | |
177 | String body = mapper.writeValueAsString(saved); | |
178 | ||
179 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
180 | } | |
181 | ||
182 | ||
183 | @Operation(summary = "List all cow health update strategies") | |
184 | @PreAuthorize("hasRole('ROLE_USER')") | |
185 | @GetMapping("/all-health-update-strategies") | |
186 | public ResponseEntity<String> listCowHealthUpdateStrategies() throws JsonProcessingException { | |
187 | var result = HealthUpdateStrategyList.create(); | |
188 | String body = mapper.writeValueAsString(result); | |
189 |
1
1. listCowHealthUpdateStrategies : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::listCowHealthUpdateStrategies → KILLED |
return ResponseEntity.ok().body(body); |
190 | } | |
191 | ||
192 | @Operation(summary = "Join a commons") | |
193 | @PreAuthorize("hasRole('ROLE_USER')") | |
194 | @PostMapping(value = "/join", produces = "application/json") | |
195 | public ResponseEntity<String> joinCommon( | |
196 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws Exception { | |
197 | ||
198 | User u = getCurrentUser().getUser(); | |
199 | Long userId = u.getId(); | |
200 | String username = u.getFullName(); | |
201 | ||
202 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
203 |
1
1. lambda$joinCommon$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$joinCommon$3 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, commonsId)); |
204 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
205 | ||
206 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
207 | // user is already a member of this commons | |
208 | String body = mapper.writeValueAsString(joinedCommons); | |
209 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
210 | } | |
211 | ||
212 | UserCommons uc = UserCommons.builder() | |
213 | .user(u) | |
214 | .commons(joinedCommons) | |
215 | .username(username) | |
216 | .totalWealth(joinedCommons.getStartingBalance()) | |
217 | .numOfCows(0) | |
218 | .cowHealth(100) | |
219 | .cowsBought(0) | |
220 | .cowsSold(0) | |
221 | .cowDeaths(0) | |
222 | .build(); | |
223 | ||
224 | userCommonsRepository.save(uc); | |
225 | ||
226 | String body = mapper.writeValueAsString(joinedCommons); | |
227 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
228 | } | |
229 | ||
230 | @Operation(summary = "Delete a Commons") | |
231 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
232 | @DeleteMapping("") | |
233 | public Object deleteCommons( | |
234 | @Parameter(name="id") @RequestParam Long id) { | |
235 | ||
236 | commonsRepository.findById(id) | |
237 |
1
1. lambda$deleteCommons$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteCommons$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id)); |
238 | ||
239 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
240 | ||
241 | String responseString = String.format("commons with id %d deleted", id); | |
242 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
243 | ||
244 | } | |
245 | ||
246 | @Operation(summary="Delete a user from a commons") | |
247 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
248 | @DeleteMapping("/{commonsId}/users/{userId}") | |
249 | public Object deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
250 | @PathVariable("userId") Long userId) throws Exception { | |
251 | ||
252 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
253 |
1
1. lambda$deleteUserFromCommon$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteUserFromCommon$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException( |
254 | UserCommons.class, "commonsId", commonsId, "userId", userId) | |
255 | ); | |
256 | ||
257 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::delete → KILLED |
userCommonsRepository.delete(userCommons); |
258 | ||
259 | String responseString = String.format("user with id %d deleted from commons with id %d, %d users remain", userId, commonsId, commonsRepository.getNumUsers(commonsId).orElse(0)); | |
260 | ||
261 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return genericMessage(responseString); |
262 | } | |
263 | ||
264 | public CommonsPlus toCommonsPlus(Commons c) { | |
265 | Optional<Integer> numCows = commonsRepository.getNumCows(c.getId()); | |
266 | Optional<Integer> numUsers = commonsRepository.getNumUsers(c.getId()); | |
267 | ||
268 |
1
1. toCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::toCommonsPlus → KILLED |
return CommonsPlus.builder() |
269 | .commons(c) | |
270 | .totalCows(numCows.orElse(0)) | |
271 | .totalUsers(numUsers.orElse(0)) | |
272 | .build(); | |
273 | } | |
274 | } | |
Mutations | ||
50 |
1.1 |
|
62 |
1.1 |
|
65 |
1.1 |
|
71 |
1.1 |
|
80 |
1.1 |
|
82 |
1.1 |
|
97 |
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 |
|
114 |
1.1 |
|
115 |
1.1 |
|
117 |
1.1 |
|
118 |
1.1 |
|
121 |
1.1 2.2 |
|
127 |
1.1 |
|
137 |
1.1 |
|
139 |
1.1 |
|
161 |
1.1 |
|
164 |
1.1 |
|
172 |
1.1 2.2 |
|
179 |
1.1 |
|
189 |
1.1 |
|
203 |
1.1 |
|
206 |
1.1 |
|
209 |
1.1 |
|
227 |
1.1 |
|
237 |
1.1 |
|
239 |
1.1 |
|
242 |
1.1 |
|
253 |
1.1 |
|
257 |
1.1 |
|
261 |
1.1 |
|
268 |
1.1 |