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::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
114 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
115 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
116 | ||
117 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
118 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
119 | } | |
120 | ||
121 | commonsRepository.save(updated); | |
122 | ||
123 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
124 | } | |
125 | ||
126 | @ApiOperation(value = "Get a specific commons") | |
127 | @PreAuthorize("hasRole('ROLE_USER')") | |
128 | @GetMapping("") | |
129 | public Commons getCommonsById( | |
130 | @ApiParam("id") @RequestParam Long id) throws JsonProcessingException { | |
131 | ||
132 | Commons commons = commonsRepository.findById(id) | |
133 |
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)); |
134 | ||
135 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
136 | } | |
137 | ||
138 | @ApiOperation(value = "Create a new commons") | |
139 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
140 | @PostMapping(value = "/new", produces = "application/json") | |
141 | public ResponseEntity<String> createCommons( | |
142 | ||
143 | @ApiParam("request body") @RequestBody CreateCommonsParams params) throws JsonProcessingException { | |
144 | Commons commons = Commons.builder() | |
145 | .name(params.getName()) | |
146 | .cowPrice(params.getCowPrice()) | |
147 | .milkPrice(params.getMilkPrice()) | |
148 | .startingBalance(params.getStartingBalance()) | |
149 | .startingDate(params.getStartingDate()) | |
150 | .degradationRate(params.getDegradationRate()) | |
151 | .showLeaderboard(params.getShowLeaderboard()) | |
152 | .carryingCapacity(params.getCarryingCapacity()) | |
153 | .build(); | |
154 | ||
155 | // throw exception for degradation rate | |
156 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
157 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
158 | } | |
159 | ||
160 | Commons saved = commonsRepository.save(commons); | |
161 | String body = mapper.writeValueAsString(saved); | |
162 | ||
163 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
164 | } | |
165 | ||
166 | @ApiOperation(value = "Join a commons") | |
167 | @PreAuthorize("hasRole('ROLE_USER')") | |
168 | @PostMapping(value = "/join", produces = "application/json") | |
169 | public ResponseEntity<String> joinCommon( | |
170 | @ApiParam("commonsId") @RequestParam Long commonsId) throws Exception { | |
171 | ||
172 | User u = getCurrentUser().getUser(); | |
173 | Long userId = u.getId(); | |
174 | String username = u.getFullName(); | |
175 | ||
176 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
177 |
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)); |
178 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
179 | ||
180 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
181 | // user is already a member of this commons | |
182 | String body = mapper.writeValueAsString(joinedCommons); | |
183 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
184 | } | |
185 | ||
186 | UserCommons uc = UserCommons.builder() | |
187 | .commonsId(commonsId) | |
188 | .userId(userId) | |
189 | .username(username) | |
190 | .totalWealth(joinedCommons.getStartingBalance()) | |
191 | .numOfCows(0) | |
192 | .cowHealth(100) | |
193 | .build(); | |
194 | ||
195 | userCommonsRepository.save(uc); | |
196 | ||
197 | String body = mapper.writeValueAsString(joinedCommons); | |
198 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
199 | } | |
200 | ||
201 | @ApiOperation(value = "Delete a Commons") | |
202 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
203 | @DeleteMapping("") | |
204 | public Object deleteCommons( | |
205 | @ApiParam("id") @RequestParam Long id) { | |
206 | ||
207 | Commons foundCommons = commonsRepository.findById(id) | |
208 |
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)); |
209 | ||
210 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
211 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::deleteAllByCommonsId → KILLED |
userCommonsRepository.deleteAllByCommonsId(id); |
212 | ||
213 | String responseString = String.format("commons with id %d deleted", id); | |
214 | ||
215 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
216 | ||
217 | } | |
218 | ||
219 | @ApiOperation("Delete a user from a commons") | |
220 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
221 | @DeleteMapping("/{commonsId}/users/{userId}") | |
222 | public ResponseEntity<Commons> deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
223 | @PathVariable("userId") Long userId) throws Exception { | |
224 | ||
225 | Optional<UserCommons> uc = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
226 |
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( |
227 | String.format("UserCommons with commonsId=%d and userId=%d not found.", commonsId, userId))); | |
228 | ||
229 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::deleteById → KILLED |
userCommonsRepository.deleteById(userCommons.getId()); |
230 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); |
231 | } | |
232 | ||
233 | public CommonsPlus toCommonsPlus(Commons c) { | |
234 | Optional<Integer> numCows = commonsRepository.getNumCows(c.getId()); | |
235 | Optional<Integer> numUsers = commonsRepository.getNumUsers(c.getId()); | |
236 | ||
237 |
1
1. toCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::toCommonsPlus → KILLED |
return CommonsPlus.builder() |
238 | .commons(c) | |
239 | .totalCows(numCows.orElse(0)) | |
240 | .totalUsers(numUsers.orElse(0)) | |
241 | .build(); | |
242 | } | |
243 | } | |
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 |
|
117 |
1.1 2.2 |
|
123 |
1.1 |
|
133 |
1.1 |
|
135 |
1.1 |
|
156 |
1.1 2.2 |
|
163 |
1.1 |
|
177 |
1.1 |
|
180 |
1.1 |
|
183 |
1.1 |
|
198 |
1.1 |
|
208 |
1.1 |
|
210 |
1.1 |
|
211 |
1.1 |
|
215 |
1.1 |
|
226 |
1.1 |
|
229 |
1.1 |
|
230 |
1.1 |
|
237 |
1.1 |