1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | ||
3 | import java.time.LocalDateTime; | |
4 | import java.util.Optional; | |
5 | import java.util.ArrayList; | |
6 | import java.util.List; | |
7 | import java.util.Iterator; | |
8 | import java.util.*; | |
9 | import java.util.stream.*; | |
10 | ||
11 | import com.fasterxml.jackson.core.JsonProcessingException; | |
12 | import com.fasterxml.jackson.databind.ObjectMapper; | |
13 | ||
14 | import io.swagger.annotations.Api; | |
15 | import io.swagger.annotations.ApiOperation; | |
16 | import io.swagger.annotations.ApiParam; | |
17 | ||
18 | import lombok.extern.slf4j.Slf4j; | |
19 | ||
20 | import org.springframework.beans.factory.annotation.Autowired; | |
21 | import org.springframework.format.annotation.DateTimeFormat; | |
22 | import org.springframework.http.HttpStatus; | |
23 | import org.springframework.http.ResponseEntity; | |
24 | import org.springframework.security.access.prepost.PreAuthorize; | |
25 | import org.springframework.web.bind.annotation.DeleteMapping; | |
26 | import org.springframework.web.bind.annotation.GetMapping; | |
27 | import org.springframework.web.bind.annotation.PathVariable; | |
28 | import org.springframework.web.bind.annotation.PostMapping; | |
29 | import org.springframework.web.bind.annotation.PutMapping; | |
30 | import org.springframework.web.bind.annotation.RequestBody; | |
31 | import org.springframework.web.bind.annotation.RequestMapping; | |
32 | import org.springframework.web.bind.annotation.RequestParam; | |
33 | import org.springframework.web.bind.annotation.RestController; | |
34 | ||
35 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
36 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
37 | import edu.ucsb.cs156.happiercows.entities.User; | |
38 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
39 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
40 | import edu.ucsb.cs156.happiercows.models.CreateCommonsParams; | |
41 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
42 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
43 | import edu.ucsb.cs156.happiercows.controllers.ApiController; | |
44 | ||
45 | @Slf4j | |
46 | @Api(description = "Commons") | |
47 | @RequestMapping("/api/commons") | |
48 | @RestController | |
49 | public class CommonsController extends ApiController { | |
50 | @Autowired | |
51 | private CommonsRepository commonsRepository; | |
52 | ||
53 | @Autowired | |
54 | private UserCommonsRepository userCommonsRepository; | |
55 | ||
56 | @Autowired | |
57 | ObjectMapper mapper; | |
58 | ||
59 | @ApiOperation(value = "Get a list of all commons") | |
60 | @GetMapping("/all") | |
61 | public ResponseEntity<String> getCommons() throws JsonProcessingException { | |
62 | log.info("getCommons()..."); | |
63 | Iterable<Commons> commons = commonsRepository.findAll(); | |
64 | String body = mapper.writeValueAsString(commons); | |
65 |
1
1. getCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommons → KILLED |
return ResponseEntity.ok().body(body); |
66 | } | |
67 | ||
68 | @ApiOperation(value = "Get a list of all commons and number of cows/users") | |
69 | @GetMapping("/allplus") | |
70 | public ResponseEntity<String> getCommonsPlus() throws JsonProcessingException { | |
71 | log.info("getCommonsPlus()..."); | |
72 | Iterable<Commons> commonsListIter = commonsRepository.findAll(); | |
73 | ||
74 | // convert Iterable to List for the purposes of using a Java Stream & lambda | |
75 | // below | |
76 | List<Commons> commonsList = new ArrayList<Commons>(); | |
77 |
1
1. getCommonsPlus : removed call to java/lang/Iterable::forEach → KILLED |
commonsListIter.forEach(commonsList::add); |
78 | ||
79 | List<CommonsPlus> commonsPlusList1 = commonsList.stream() | |
80 |
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)) |
81 | .collect(Collectors.toList()); | |
82 | ||
83 | ArrayList<CommonsPlus> commonsPlusList = new ArrayList<CommonsPlus>(commonsPlusList1); | |
84 | ||
85 | String body = mapper.writeValueAsString(commonsPlusList); | |
86 |
1
1. getCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlus → KILLED |
return ResponseEntity.ok().body(body); |
87 | } | |
88 | ||
89 | @ApiOperation(value = "Update a commons") | |
90 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
91 | @PutMapping("/update") | |
92 | public ResponseEntity<String> updateCommons( | |
93 | @ApiParam("commons identifier") @RequestParam long id, | |
94 | @ApiParam("request body") @RequestBody CreateCommonsParams params) { | |
95 | Optional<Commons> existing = commonsRepository.findById(id); | |
96 | ||
97 | Commons updated; | |
98 | HttpStatus status; | |
99 | ||
100 |
1
1. updateCommons : negated conditional → KILLED |
if (existing.isPresent()) { |
101 | updated = existing.get(); | |
102 | status = HttpStatus.NO_CONTENT; | |
103 | } else { | |
104 | updated = new Commons(); | |
105 | status = HttpStatus.CREATED; | |
106 | } | |
107 | ||
108 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setName → KILLED |
updated.setName(params.getName()); |
109 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCowPrice → KILLED |
updated.setCowPrice(params.getCowPrice()); |
110 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setMilkPrice → KILLED |
updated.setMilkPrice(params.getMilkPrice()); |
111 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingBalance → KILLED |
updated.setStartingBalance(params.getStartingBalance()); |
112 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingDate → KILLED |
updated.setStartingDate(params.getStartingDate()); |
113 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setScaleCowSalePrice → KILLED |
updated.setScaleCowSalePrice(params.getScaleCowSalePrice()); |
114 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
115 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
116 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
117 | ||
118 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
119 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
120 | } | |
121 | ||
122 | commonsRepository.save(updated); | |
123 | ||
124 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
125 | } | |
126 | ||
127 | @ApiOperation(value = "Get a specific commons") | |
128 | @PreAuthorize("hasRole('ROLE_USER')") | |
129 | @GetMapping("") | |
130 | public Commons getCommonsById( | |
131 | @ApiParam("id") @RequestParam Long id) throws JsonProcessingException { | |
132 | ||
133 | Commons commons = commonsRepository.findById(id) | |
134 |
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)); |
135 | ||
136 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
137 | } | |
138 | ||
139 | @ApiOperation(value = "Create a new commons") | |
140 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
141 | @PostMapping(value = "/new", produces = "application/json") | |
142 | public ResponseEntity<String> createCommons( | |
143 | ||
144 | @ApiParam("request body") @RequestBody CreateCommonsParams params) throws JsonProcessingException { | |
145 | Commons commons = Commons.builder() | |
146 | .name(params.getName()) | |
147 | .cowPrice(params.getCowPrice()) | |
148 | .milkPrice(params.getMilkPrice()) | |
149 | .startingBalance(params.getStartingBalance()) | |
150 | .startingDate(params.getStartingDate()) | |
151 | .degradationRate(params.getDegradationRate()) | |
152 | .scaleCowSalePrice(params.getScaleCowSalePrice()) | |
153 | .showLeaderboard(params.getShowLeaderboard()) | |
154 | .carryingCapacity(params.getCarryingCapacity()) | |
155 | .build(); | |
156 | ||
157 | // throw exception for degradation rate | |
158 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
159 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
160 | } | |
161 | ||
162 | Commons saved = commonsRepository.save(commons); | |
163 | String body = mapper.writeValueAsString(saved); | |
164 | ||
165 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
166 | } | |
167 | ||
168 | @ApiOperation(value = "Join a commons") | |
169 | @PreAuthorize("hasRole('ROLE_USER')") | |
170 | @PostMapping(value = "/join", produces = "application/json") | |
171 | public ResponseEntity<String> joinCommon( | |
172 | @ApiParam("commonsId") @RequestParam Long commonsId) throws Exception { | |
173 | ||
174 | User u = getCurrentUser().getUser(); | |
175 | Long userId = u.getId(); | |
176 | String username = u.getFullName(); | |
177 | ||
178 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
179 |
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)); |
180 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
181 | ||
182 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
183 | // user is already a member of this commons | |
184 | String body = mapper.writeValueAsString(joinedCommons); | |
185 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
186 | } | |
187 | ||
188 | UserCommons uc = UserCommons.builder() | |
189 | .commonsId(commonsId) | |
190 | .userId(userId) | |
191 | .username(username) | |
192 | .totalWealth(joinedCommons.getStartingBalance()) | |
193 | .numOfCows(0) | |
194 | .cowHealth(100) | |
195 | .totalCowsBought(0) | |
196 | .totalCowsSold(0) | |
197 | .build(); | |
198 | ||
199 | userCommonsRepository.save(uc); | |
200 | ||
201 | String body = mapper.writeValueAsString(joinedCommons); | |
202 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
203 | } | |
204 | ||
205 | @ApiOperation(value = "Delete a Commons") | |
206 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
207 | @DeleteMapping("") | |
208 | public Object deleteCommons( | |
209 | @ApiParam("id") @RequestParam Long id) { | |
210 | ||
211 | Commons foundCommons = commonsRepository.findById(id) | |
212 |
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)); |
213 | ||
214 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
215 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::deleteAllByCommonsId → KILLED |
userCommonsRepository.deleteAllByCommonsId(id); |
216 | ||
217 | String responseString = String.format("commons with id %d deleted", id); | |
218 | ||
219 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
220 | ||
221 | } | |
222 | ||
223 | @ApiOperation("Delete a user from a commons") | |
224 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
225 | @DeleteMapping("/{commonsId}/users/{userId}") | |
226 | public ResponseEntity<Commons> deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
227 | @PathVariable("userId") Long userId) throws Exception { | |
228 | ||
229 | Optional<UserCommons> uc = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
230 |
1
1. lambda$deleteUserFromCommon$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteUserFromCommon$4 → KILLED |
UserCommons userCommons = uc.orElseThrow(() -> new Exception( |
231 | String.format("UserCommons with commonsId=%d and userId=%d not found.", commonsId, userId))); | |
232 | ||
233 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::deleteById → KILLED |
userCommonsRepository.deleteById(userCommons.getId()); |
234 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); |
235 | } | |
236 | ||
237 | public CommonsPlus toCommonsPlus(Commons c) { | |
238 | Optional<Integer> numCows = commonsRepository.getNumCows(c.getId()); | |
239 | Optional<Integer> numUsers = commonsRepository.getNumUsers(c.getId()); | |
240 | ||
241 |
1
1. toCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::toCommonsPlus → KILLED |
return CommonsPlus.builder() |
242 | .commons(c) | |
243 | .totalCows(numCows.orElse(0)) | |
244 | .totalUsers(numUsers.orElse(0)) | |
245 | .build(); | |
246 | } | |
247 | } | |
Mutations | ||
65 |
1.1 |
|
77 |
1.1 |
|
80 |
1.1 |
|
86 |
1.1 |
|
100 |
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 |
|
116 |
1.1 |
|
118 |
1.1 2.2 |
|
124 |
1.1 |
|
134 |
1.1 |
|
136 |
1.1 |
|
158 |
1.1 2.2 |
|
165 |
1.1 |
|
179 |
1.1 |
|
182 |
1.1 |
|
185 |
1.1 |
|
202 |
1.1 |
|
212 |
1.1 |
|
214 |
1.1 |
|
215 |
1.1 |
|
219 |
1.1 |
|
230 |
1.1 |
|
233 |
1.1 |
|
234 |
1.1 |
|
241 |
1.1 |