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 : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
114 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setAboveCapacityHealthUpdateStrategy → KILLED |
updated.setAboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); |
115 | } | |
116 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
117 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setBelowCapacityHealthUpdateStrategy → KILLED |
updated.setBelowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); |
118 | } | |
119 | ||
120 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
121 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
122 | } | |
123 | ||
124 | commonsRepository.save(updated); | |
125 | ||
126 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
127 | } | |
128 | ||
129 | @Operation(summary = "Get a specific commons") | |
130 | @PreAuthorize("hasRole('ROLE_USER')") | |
131 | @GetMapping("") | |
132 | public Commons getCommonsById( | |
133 | @Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
134 | ||
135 | Commons commons = commonsRepository.findById(id) | |
136 |
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)); |
137 | ||
138 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
139 | } | |
140 | ||
141 | @Operation(summary = "Create a new commons") | |
142 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
143 | @PostMapping(value = "/new", produces = "application/json") | |
144 | public ResponseEntity<String> createCommons( | |
145 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
146 | ) throws JsonProcessingException { | |
147 | ||
148 | var builder = Commons.builder() | |
149 | .name(params.getName()) | |
150 | .cowPrice(params.getCowPrice()) | |
151 | .milkPrice(params.getMilkPrice()) | |
152 | .startingBalance(params.getStartingBalance()) | |
153 | .startingDate(params.getStartingDate()) | |
154 | .degradationRate(params.getDegradationRate()) | |
155 | .showLeaderboard(params.getShowLeaderboard()) | |
156 | .carryingCapacity(params.getCarryingCapacity()); | |
157 | ||
158 | // ok to set null values for these, so old backend still works | |
159 |
1
1. createCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
160 | builder.aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); | |
161 | } | |
162 |
1
1. createCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
163 | builder.belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); | |
164 | } | |
165 | ||
166 | Commons commons = builder.build(); | |
167 | ||
168 | ||
169 | // throw exception for degradation rate | |
170 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
171 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
172 | } | |
173 | ||
174 | Commons saved = commonsRepository.save(commons); | |
175 | String body = mapper.writeValueAsString(saved); | |
176 | ||
177 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
178 | } | |
179 | ||
180 | ||
181 | @Operation(summary = "List all cow health update strategies") | |
182 | @PreAuthorize("hasRole('ROLE_USER')") | |
183 | @GetMapping("/all-health-update-strategies") | |
184 | public ResponseEntity<String> listCowHealthUpdateStrategies() throws JsonProcessingException { | |
185 | var result = HealthUpdateStrategyList.create(); | |
186 | String body = mapper.writeValueAsString(result); | |
187 |
1
1. listCowHealthUpdateStrategies : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::listCowHealthUpdateStrategies → KILLED |
return ResponseEntity.ok().body(body); |
188 | } | |
189 | ||
190 | @Operation(summary = "Join a commons") | |
191 | @PreAuthorize("hasRole('ROLE_USER')") | |
192 | @PostMapping(value = "/join", produces = "application/json") | |
193 | public ResponseEntity<String> joinCommon( | |
194 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws Exception { | |
195 | ||
196 | User u = getCurrentUser().getUser(); | |
197 | Long userId = u.getId(); | |
198 | String username = u.getFullName(); | |
199 | ||
200 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
201 |
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)); |
202 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
203 | ||
204 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
205 | // user is already a member of this commons | |
206 | String body = mapper.writeValueAsString(joinedCommons); | |
207 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
208 | } | |
209 | ||
210 | UserCommons uc = UserCommons.builder() | |
211 | .user(u) | |
212 | .commons(joinedCommons) | |
213 | .username(username) | |
214 | .totalWealth(joinedCommons.getStartingBalance()) | |
215 | .numOfCows(0) | |
216 | .cowHealth(100) | |
217 | .cowsBought(0) | |
218 | .cowsSold(0) | |
219 | .cowDeaths(0) | |
220 | .build(); | |
221 | ||
222 | userCommonsRepository.save(uc); | |
223 | ||
224 | String body = mapper.writeValueAsString(joinedCommons); | |
225 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
226 | } | |
227 | ||
228 | @Operation(summary = "Delete a Commons") | |
229 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
230 | @DeleteMapping("") | |
231 | public Object deleteCommons( | |
232 | @Parameter(name="id") @RequestParam Long id) { | |
233 | ||
234 | commonsRepository.findById(id) | |
235 |
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)); |
236 | ||
237 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
238 | ||
239 | String responseString = String.format("commons with id %d deleted", id); | |
240 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
241 | ||
242 | } | |
243 | ||
244 | @Operation(summary="Delete a user from a commons") | |
245 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
246 | @DeleteMapping("/{commonsId}/users/{userId}") | |
247 | public Object deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
248 | @PathVariable("userId") Long userId) throws Exception { | |
249 | ||
250 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
251 |
1
1. lambda$deleteUserFromCommon$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteUserFromCommon$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException( |
252 | UserCommons.class, "commonsId", commonsId, "userId", userId) | |
253 | ); | |
254 | ||
255 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::delete → KILLED |
userCommonsRepository.delete(userCommons); |
256 | ||
257 | String responseString = String.format("user with id %d deleted from commons with id %d, %d users remain", userId, commonsId, commonsRepository.getNumUsers(commonsId).orElse(0)); | |
258 | ||
259 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return genericMessage(responseString); |
260 | } | |
261 | ||
262 | public CommonsPlus toCommonsPlus(Commons c) { | |
263 | Optional<Integer> numCows = commonsRepository.getNumCows(c.getId()); | |
264 | Optional<Integer> numUsers = commonsRepository.getNumUsers(c.getId()); | |
265 | ||
266 |
1
1. toCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::toCommonsPlus → KILLED |
return CommonsPlus.builder() |
267 | .commons(c) | |
268 | .totalCows(numCows.orElse(0)) | |
269 | .totalUsers(numUsers.orElse(0)) | |
270 | .build(); | |
271 | } | |
272 | } | |
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 |
|
116 |
1.1 |
|
117 |
1.1 |
|
120 |
1.1 2.2 |
|
126 |
1.1 |
|
136 |
1.1 |
|
138 |
1.1 |
|
159 |
1.1 |
|
162 |
1.1 |
|
170 |
1.1 2.2 |
|
177 |
1.1 |
|
187 |
1.1 |
|
201 |
1.1 |
|
204 |
1.1 |
|
207 |
1.1 |
|
225 |
1.1 |
|
235 |
1.1 |
|
237 |
1.1 |
|
240 |
1.1 |
|
251 |
1.1 |
|
255 |
1.1 |
|
259 |
1.1 |
|
266 |
1.1 |