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