| 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.CommonStats; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 7 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
| 8 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 9 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 10 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 11 | import edu.ucsb.cs156.happiercows.models.CreateCommonsParams; | |
| 12 | import edu.ucsb.cs156.happiercows.models.DashboardSettingsParams; | |
| 13 | import edu.ucsb.cs156.happiercows.models.HealthUpdateStrategyList; | |
| 14 | import edu.ucsb.cs156.happiercows.errors.CourseAccessDeniedException; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.CommonStatsRepository; | |
| 16 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 17 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 18 | import edu.ucsb.cs156.happiercows.services.CourseAccessService; | |
| 19 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategies; | |
| 20 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 21 | import io.swagger.v3.oas.annotations.Operation; | |
| 22 | import io.swagger.v3.oas.annotations.Parameter; | |
| 23 | import org.springframework.beans.factory.annotation.Value; | |
| 24 | import lombok.extern.slf4j.Slf4j; | |
| 25 | import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | import org.springframework.http.HttpStatus; | |
| 27 | import org.springframework.http.ResponseEntity; | |
| 28 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 29 | import org.springframework.web.bind.annotation.*; | |
| 30 | import edu.ucsb.cs156.happiercows.services.CommonsPlusBuilderService; | |
| 31 | ||
| 32 | ||
| 33 | import java.util.ArrayList; | |
| 34 | import java.util.Comparator; | |
| 35 | import java.util.List; | |
| 36 | import java.util.Map; | |
| 37 | import java.util.Optional; | |
| 38 | import java.util.stream.Collectors; | |
| 39 | import java.util.stream.StreamSupport; | |
| 40 | ||
| 41 | ||
| 42 | @Slf4j | |
| 43 | @Tag(name = "Commons") | |
| 44 | @RequestMapping("/api/commons") | |
| 45 | @RestController | |
| 46 | public class CommonsController extends ApiController { | |
| 47 | @Autowired | |
| 48 | private CommonsRepository commonsRepository; | |
| 49 | ||
| 50 | @Autowired | |
| 51 | private UserCommonsRepository userCommonsRepository; | |
| 52 | ||
| 53 | @Autowired | |
| 54 | ObjectMapper mapper; | |
| 55 | ||
| 56 | @Autowired | |
| 57 | CommonsPlusBuilderService commonsPlusBuilderService; | |
| 58 | ||
| 59 | @Autowired | |
| 60 | CommonStatsRepository commonStatsRepository; | |
| 61 | ||
| 62 | @Autowired | |
| 63 | CourseAccessService courseAccessService; | |
| 64 | ||
| 65 | @Value("${app.commons.default.startingBalance}") | |
| 66 | private double defaultStartingBalance; | |
| 67 | ||
| 68 | @Value("${app.commons.default.cowPrice}") | |
| 69 | private double defaultCowPrice; | |
| 70 | ||
| 71 | @Value("${app.commons.default.milkPrice}") | |
| 72 | private double defaultMilkPrice; | |
| 73 | ||
| 74 | @Value("${app.commons.default.degradationRate}") | |
| 75 | private double defaultDegradationRate; | |
| 76 | ||
| 77 | @Value("${app.commons.default.carryingCapacity}") | |
| 78 | private int defaultCarryingCapacity; | |
| 79 | ||
| 80 | @Value("${app.commons.default.capacityPerUser}") | |
| 81 | private int defaultCapacityPerUser; | |
| 82 | ||
| 83 | @Value("${app.commons.default.aboveCapacityHealthUpdateStrategy}") | |
| 84 | private String defaultAboveCapacityHealthUpdateStrategy; | |
| 85 | ||
| 86 | @Value("${app.commons.default.belowCapacityHealthUpdateStrategy}") | |
| 87 | private String defaultBelowCapacityHealthUpdateStrategy; | |
| 88 | ||
| 89 | @Operation(summary = "Get default common values") | |
| 90 | @GetMapping("/defaults") | |
| 91 | public ResponseEntity<Commons> getDefaultCommons() throws JsonProcessingException { | |
| 92 | log.info("getDefaultCommons()..."); | |
| 93 | ||
| 94 | Commons defaultCommons = Commons.builder() | |
| 95 | .startingBalance(defaultStartingBalance) | |
| 96 | .cowPrice(defaultCowPrice) | |
| 97 | .milkPrice(defaultMilkPrice) | |
| 98 | .degradationRate(defaultDegradationRate) | |
| 99 | .carryingCapacity(defaultCarryingCapacity) | |
| 100 | .capacityPerUser(defaultCapacityPerUser) | |
| 101 | .aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(defaultAboveCapacityHealthUpdateStrategy)) | |
| 102 | .belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(defaultBelowCapacityHealthUpdateStrategy)) | |
| 103 | .hidden(false) | |
| 104 | .build(); | |
| 105 | ||
| 106 |
1
1. getDefaultCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getDefaultCommons → KILLED |
return ResponseEntity.ok().body(defaultCommons); |
| 107 | } | |
| 108 | ||
| 109 | @Operation(summary = "Get a list of all commons") | |
| 110 | @GetMapping("/all") | |
| 111 | public ResponseEntity<String> getCommons() throws JsonProcessingException { | |
| 112 | log.info("getCommons()..."); | |
| 113 | Iterable<Commons> commons = commonsRepository.findAll(); | |
| 114 | String body = mapper.writeValueAsString(commons); | |
| 115 |
1
1. getCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommons → KILLED |
return ResponseEntity.ok().body(body); |
| 116 | } | |
| 117 | ||
| 118 | @Operation(summary = "Get a list of all commons and number of cows/users, newest first") | |
| 119 | @GetMapping("/allplus") | |
| 120 | public ResponseEntity<String> getCommonsPlus() throws JsonProcessingException { | |
| 121 | log.info("getCommonsPlus()..."); | |
| 122 | Iterable<Commons> commonsListIter = commonsRepository.findAll(); | |
| 123 | ||
| 124 | // findAll() has no defined order, so sort newest first for a deterministic response | |
| 125 | List<Commons> commonsList = new ArrayList<>(); | |
| 126 |
1
1. getCommonsPlus : removed call to java/lang/Iterable::forEach → KILLED |
commonsListIter.forEach(commonsList::add); |
| 127 |
1
1. getCommonsPlus : removed call to java/util/List::sort → KILLED |
commonsList.sort(Comparator.comparingLong(Commons::getId).reversed()); |
| 128 | ||
| 129 | Iterable<CommonsPlus> commonsPlusList = commonsPlusBuilderService.convertToCommonsPlus(commonsList); | |
| 130 | ||
| 131 | String body = mapper.writeValueAsString(commonsPlusList); | |
| 132 |
1
1. getCommonsPlus : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlus → KILLED |
return ResponseEntity.ok().body(body); |
| 133 | } | |
| 134 | ||
| 135 | @Operation(summary = "Get the number of cows/users in a commons") | |
| 136 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 137 | @GetMapping("/plus") | |
| 138 | public CommonsPlus getCommonsPlusById( | |
| 139 | @Parameter(name="id") @RequestParam long id) throws JsonProcessingException { | |
| 140 | CommonsPlus commonsPlus = commonsPlusBuilderService.toCommonsPlus(commonsRepository.findById(id) | |
| 141 |
1
1. lambda$getCommonsPlusById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsPlusById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id))); |
| 142 | ||
| 143 |
1
1. getCommonsPlusById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsPlusById → KILLED |
return commonsPlus; |
| 144 | } | |
| 145 | ||
| 146 | @Operation(summary = "Update a commons") | |
| 147 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 148 | @PutMapping("/update") | |
| 149 | public ResponseEntity<String> updateCommons( | |
| 150 | @Parameter(name="id") @RequestParam long id, | |
| 151 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
| 152 | ) { | |
| 153 | Optional<Commons> existing = commonsRepository.findById(id); | |
| 154 | ||
| 155 | Commons updated; | |
| 156 | HttpStatus status; | |
| 157 | ||
| 158 |
1
1. updateCommons : negated conditional → KILLED |
if (existing.isPresent()) { |
| 159 | updated = existing.get(); | |
| 160 | status = HttpStatus.NO_CONTENT; | |
| 161 | } else { | |
| 162 | updated = new Commons(); | |
| 163 | status = HttpStatus.CREATED; | |
| 164 | } | |
| 165 | ||
| 166 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setName → KILLED |
updated.setName(params.getName()); |
| 167 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCowPrice → KILLED |
updated.setCowPrice(params.getCowPrice()); |
| 168 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setMilkPrice → KILLED |
updated.setMilkPrice(params.getMilkPrice()); |
| 169 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingBalance → KILLED |
updated.setStartingBalance(params.getStartingBalance()); |
| 170 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setStartingDate → KILLED |
updated.setStartingDate(params.getStartingDate()); |
| 171 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setLastDate → KILLED |
updated.setLastDate(params.getLastDate()); |
| 172 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowLeaderboard → KILLED |
updated.setShowLeaderboard(params.getShowLeaderboard()); |
| 173 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowChat → KILLED |
updated.setShowChat(params.getShowChat()); |
| 174 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setDegradationRate → KILLED |
updated.setDegradationRate(params.getDegradationRate()); |
| 175 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCapacityPerUser → KILLED |
updated.setCapacityPerUser(params.getCapacityPerUser()); |
| 176 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCarryingCapacity → KILLED |
updated.setCarryingCapacity(params.getCarryingCapacity()); |
| 177 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
| 178 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setAboveCapacityHealthUpdateStrategy → KILLED |
updated.setAboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); |
| 179 | } | |
| 180 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
| 181 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setBelowCapacityHealthUpdateStrategy → KILLED |
updated.setBelowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); |
| 182 | } | |
| 183 | ||
| 184 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getDegradationRate() < 0) { |
| 185 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
| 186 | } | |
| 187 | ||
| 188 | // Reference: frontend/src/main/components/Commons/CommonsForm.js | |
| 189 |
1
1. updateCommons : negated conditional → KILLED |
if (params.getName().equals("")) { |
| 190 | throw new IllegalArgumentException("Name cannot be empty"); | |
| 191 | } | |
| 192 | ||
| 193 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getCowPrice() < 0.01) { |
| 194 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
| 195 | } | |
| 196 | ||
| 197 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getMilkPrice() < 0.01) { |
| 198 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
| 199 | } | |
| 200 | ||
| 201 |
2
1. updateCommons : negated conditional → KILLED 2. updateCommons : changed conditional boundary → KILLED |
if (params.getStartingBalance() < 0) { |
| 202 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
| 203 | } | |
| 204 | ||
| 205 |
2
1. updateCommons : changed conditional boundary → KILLED 2. updateCommons : negated conditional → KILLED |
if (params.getCarryingCapacity() < 1) { |
| 206 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
| 207 | } | |
| 208 | ||
| 209 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/controllers/CommonsController::validateDates → KILLED |
validateDates(params); |
| 210 | ||
| 211 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setHidden → KILLED |
updated.setHidden(params.isHidden()); |
| 212 |
1
1. updateCommons : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setCourseId → KILLED |
updated.setCourseId(params.getCourseId()); |
| 213 | commonsRepository.save(updated); | |
| 214 | ||
| 215 |
1
1. updateCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateCommons → KILLED |
return ResponseEntity.status(status).build(); |
| 216 | } | |
| 217 | ||
| 218 | @Operation(summary = "Get a specific commons") | |
| 219 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 220 | @GetMapping("") | |
| 221 | public Commons getCommonsById( | |
| 222 | @Parameter(name="id") @RequestParam Long id) throws JsonProcessingException { | |
| 223 | ||
| 224 | Commons commons = commonsRepository.findById(id) | |
| 225 |
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)); |
| 226 | ||
| 227 |
1
1. getCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsById → KILLED |
return commons; |
| 228 | } | |
| 229 | ||
| 230 | @Operation(summary = "Create a new commons") | |
| 231 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 232 | @PostMapping(value = "/new", produces = "application/json") | |
| 233 | public ResponseEntity<String> createCommons( | |
| 234 | @Parameter(name="request body") @RequestBody CreateCommonsParams params | |
| 235 | ) throws JsonProcessingException { | |
| 236 | ||
| 237 | var builder = Commons.builder() | |
| 238 | .name(params.getName()) | |
| 239 | .cowPrice(params.getCowPrice()) | |
| 240 | .milkPrice(params.getMilkPrice()) | |
| 241 | .startingBalance(params.getStartingBalance()) | |
| 242 | .startingDate(params.getStartingDate()) | |
| 243 | .lastDate(params.getLastDate()) | |
| 244 | .degradationRate(params.getDegradationRate()) | |
| 245 | .showLeaderboard(params.getShowLeaderboard()) | |
| 246 | .showChat(params.getShowChat()) | |
| 247 | .capacityPerUser(params.getCapacityPerUser()) | |
| 248 | .carryingCapacity(params.getCarryingCapacity()) | |
| 249 | .hidden(params.isHidden()) | |
| 250 | .courseId(params.getCourseId()); | |
| 251 | ||
| 252 | // ok to set null values for these, so old backend still works | |
| 253 |
1
1. createCommons : negated conditional → KILLED |
if (params.getAboveCapacityHealthUpdateStrategy() != null) { |
| 254 | builder.aboveCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getAboveCapacityHealthUpdateStrategy())); | |
| 255 | } | |
| 256 |
1
1. createCommons : negated conditional → KILLED |
if (params.getBelowCapacityHealthUpdateStrategy() != null) { |
| 257 | builder.belowCapacityHealthUpdateStrategy(CowHealthUpdateStrategies.valueOf(params.getBelowCapacityHealthUpdateStrategy())); | |
| 258 | } | |
| 259 | ||
| 260 | Commons commons = builder.build(); | |
| 261 | ||
| 262 | // Reference: frontend/src/main/components/Commons/CommonsForm.js | |
| 263 |
1
1. createCommons : negated conditional → KILLED |
if (params.getName().equals("")) { |
| 264 | throw new IllegalArgumentException("Name cannot be empty"); | |
| 265 | } | |
| 266 | ||
| 267 |
2
1. createCommons : negated conditional → KILLED 2. createCommons : changed conditional boundary → KILLED |
if (params.getCowPrice() < 0.01) { |
| 268 | throw new IllegalArgumentException("Cow Price cannot be less than 0.01"); | |
| 269 | } | |
| 270 | ||
| 271 |
2
1. createCommons : negated conditional → KILLED 2. createCommons : changed conditional boundary → KILLED |
if (params.getMilkPrice() < 0.01) { |
| 272 | throw new IllegalArgumentException("Milk Price cannot be less than 0.01"); | |
| 273 | } | |
| 274 | ||
| 275 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getStartingBalance() < 0) { |
| 276 | throw new IllegalArgumentException("Starting Balance cannot be negative"); | |
| 277 | } | |
| 278 | ||
| 279 | // throw exception for degradation rate | |
| 280 |
2
1. createCommons : negated conditional → KILLED 2. createCommons : changed conditional boundary → KILLED |
if (params.getDegradationRate() < 0) { |
| 281 | throw new IllegalArgumentException("Degradation Rate cannot be negative"); | |
| 282 | } | |
| 283 | ||
| 284 |
2
1. createCommons : changed conditional boundary → KILLED 2. createCommons : negated conditional → KILLED |
if (params.getCarryingCapacity() < 1) { |
| 285 | throw new IllegalArgumentException("Carrying Capacity cannot be less than 1"); | |
| 286 | } | |
| 287 | ||
| 288 |
1
1. createCommons : removed call to edu/ucsb/cs156/happiercows/controllers/CommonsController::validateDates → KILLED |
validateDates(params); |
| 289 | ||
| 290 | Commons saved = commonsRepository.save(commons); | |
| 291 | String body = mapper.writeValueAsString(saved); | |
| 292 | ||
| 293 |
1
1. createCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::createCommons → KILLED |
return ResponseEntity.ok().body(body); |
| 294 | } | |
| 295 | ||
| 296 | /** | |
| 297 | * Enforce that both dates are present and that the last date is strictly | |
| 298 | * after the starting date. (See issue #250; the frontend form enforces | |
| 299 | * the same rules, but the backend must not rely on that.) | |
| 300 | * | |
| 301 | * @param params the params to validate | |
| 302 | */ | |
| 303 | public static void validateDates(CreateCommonsParams params) { | |
| 304 |
1
1. validateDates : negated conditional → KILLED |
if (params.getStartingDate() == null) { |
| 305 | throw new IllegalArgumentException("Starting Date is required"); | |
| 306 | } | |
| 307 |
1
1. validateDates : negated conditional → KILLED |
if (params.getLastDate() == null) { |
| 308 | throw new IllegalArgumentException("Last Date is required"); | |
| 309 | } | |
| 310 |
1
1. validateDates : negated conditional → KILLED |
if (!params.getLastDate().isAfter(params.getStartingDate())) { |
| 311 | throw new IllegalArgumentException("Last Date must be after Starting Date"); | |
| 312 | } | |
| 313 | } | |
| 314 | ||
| 315 | ||
| 316 | @Operation(summary = "List all cow health update strategies") | |
| 317 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 318 | @GetMapping("/all-health-update-strategies") | |
| 319 | public ResponseEntity<String> listCowHealthUpdateStrategies() throws JsonProcessingException { | |
| 320 | var result = HealthUpdateStrategyList.create(); | |
| 321 | String body = mapper.writeValueAsString(result); | |
| 322 |
1
1. listCowHealthUpdateStrategies : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::listCowHealthUpdateStrategies → KILLED |
return ResponseEntity.ok().body(body); |
| 323 | } | |
| 324 | ||
| 325 | @Operation(summary = "Get the ids of the courses the current user belongs to as a student or staff member") | |
| 326 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 327 | @GetMapping("/mycourses") | |
| 328 | public ResponseEntity<List<Long>> getMyCourseIds() { | |
| 329 | User u = getCurrentUser().getUser(); | |
| 330 | List<Long> courseIds = courseAccessService.getCourseIdsForUser(u); | |
| 331 |
1
1. getMyCourseIds : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getMyCourseIds → KILLED |
return ResponseEntity.ok().body(courseIds); |
| 332 | } | |
| 333 | ||
| 334 | @Operation(summary = "Join a commons") | |
| 335 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 336 | @PostMapping(value = "/join", produces = "application/json") | |
| 337 | public ResponseEntity<String> joinCommon( | |
| 338 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws Exception { | |
| 339 | ||
| 340 | User u = getCurrentUser().getUser(); | |
| 341 | Long userId = u.getId(); | |
| 342 | String username = u.getFullName(); | |
| 343 | ||
| 344 | Commons joinedCommons = commonsRepository.findById(commonsId) | |
| 345 |
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)); |
| 346 | ||
| 347 |
2
1. joinCommon : negated conditional → KILLED 2. joinCommon : negated conditional → KILLED |
if (joinedCommons.getCourseId() != null && !courseAccessService.isEligibleForCommons(u, joinedCommons)) { |
| 348 | throw new CourseAccessDeniedException(commonsId); | |
| 349 | } | |
| 350 | ||
| 351 | Optional<UserCommons> userCommonsLookup = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId); | |
| 352 | ||
| 353 |
1
1. joinCommon : negated conditional → KILLED |
if (userCommonsLookup.isPresent()) { |
| 354 | // user is already a member of this commons | |
| 355 | String body = mapper.writeValueAsString(joinedCommons); | |
| 356 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
| 357 | } | |
| 358 | ||
| 359 | UserCommons uc = UserCommons.builder() | |
| 360 | .user(u) | |
| 361 | .commons(joinedCommons) | |
| 362 | .username(username) | |
| 363 | .totalWealth(joinedCommons.getStartingBalance()) | |
| 364 | .numOfCows(0) | |
| 365 | .cowHealth(100) | |
| 366 | .cowsBought(0) | |
| 367 | .cowsSold(0) | |
| 368 | .cowDeaths(0) | |
| 369 | .build(); | |
| 370 | ||
| 371 | userCommonsRepository.save(uc); | |
| 372 | ||
| 373 | String body = mapper.writeValueAsString(joinedCommons); | |
| 374 |
1
1. joinCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::joinCommon → KILLED |
return ResponseEntity.ok().body(body); |
| 375 | } | |
| 376 | ||
| 377 | @Operation(summary = "Delete a Commons") | |
| 378 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 379 | @DeleteMapping("") | |
| 380 | public Object deleteCommons( | |
| 381 | @Parameter(name="id") @RequestParam Long id) { | |
| 382 | | |
| 383 | Iterable<UserCommons> userCommons = userCommonsRepository.findByCommonsId(id); | |
| 384 | ||
| 385 | for (UserCommons commons : userCommons) { | |
| 386 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::delete → KILLED |
userCommonsRepository.delete(commons); |
| 387 | } | |
| 388 | ||
| 389 | commonsRepository.findById(id) | |
| 390 |
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)); |
| 391 | ||
| 392 |
1
1. deleteCommons : removed call to edu/ucsb/cs156/happiercows/repositories/CommonsRepository::deleteById → KILLED |
commonsRepository.deleteById(id); |
| 393 | ||
| 394 | String responseString = String.format("commons with id %d deleted", id); | |
| 395 |
1
1. deleteCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteCommons → KILLED |
return genericMessage(responseString); |
| 396 | ||
| 397 | } | |
| 398 | ||
| 399 | @Operation(summary="Delete a user from a commons") | |
| 400 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 401 | @DeleteMapping("/{commonsId}/users/{userId}") | |
| 402 | public Object deleteUserFromCommon(@PathVariable("commonsId") Long commonsId, | |
| 403 | @PathVariable("userId") Long userId) throws Exception { | |
| 404 | ||
| 405 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 406 |
1
1. lambda$deleteUserFromCommon$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$deleteUserFromCommon$4 → KILLED |
.orElseThrow(() -> new EntityNotFoundException( |
| 407 | UserCommons.class, "commonsId", commonsId, "userId", userId) | |
| 408 | ); | |
| 409 | ||
| 410 |
1
1. deleteUserFromCommon : removed call to edu/ucsb/cs156/happiercows/repositories/UserCommonsRepository::delete → KILLED |
userCommonsRepository.delete(userCommons); |
| 411 | ||
| 412 | String responseString = String.format("user with id %d deleted from commons with id %d, %d users remain", userId, commonsId, commonsRepository.getNumUsers(commonsId).orElse(0)); | |
| 413 | ||
| 414 |
1
1. deleteUserFromCommon : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::deleteUserFromCommon → KILLED |
return genericMessage(responseString); |
| 415 | } | |
| 416 | ||
| 417 | @Operation(summary = "Update the dashboard visibility settings for a commons (admin only)") | |
| 418 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 419 | @PutMapping("/dashboardSettings") | |
| 420 | public ResponseEntity<Commons> updateDashboardSettings( | |
| 421 | @Parameter(name="id") @RequestParam long id, | |
| 422 | @Parameter(name="request body") @RequestBody DashboardSettingsParams params | |
| 423 | ) { | |
| 424 | Commons commons = commonsRepository.findById(id) | |
| 425 |
1
1. lambda$updateDashboardSettings$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$updateDashboardSettings$5 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Commons.class, id)); |
| 426 | ||
| 427 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowLeaderboard → KILLED |
commons.setShowLeaderboard(params.isShowLeaderboard()); |
| 428 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowOverviewSection → KILLED |
commons.setShowOverviewSection(params.isShowOverviewSection()); |
| 429 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowCowsPerFarmerSection → KILLED |
commons.setShowCowsPerFarmerSection(params.isShowCowsPerFarmerSection()); |
| 430 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowHistogramSection → KILLED |
commons.setShowHistogramSection(params.isShowHistogramSection()); |
| 431 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowTrendsSection → KILLED |
commons.setShowTrendsSection(params.isShowTrendsSection()); |
| 432 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowHealthSection → KILLED |
commons.setShowHealthSection(params.isShowHealthSection()); |
| 433 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowTotalCowsSection → KILLED |
commons.setShowTotalCowsSection(params.isShowTotalCowsSection()); |
| 434 |
1
1. updateDashboardSettings : removed call to edu/ucsb/cs156/happiercows/entities/Commons::setShowFarmerLeaderboardSection → KILLED |
commons.setShowFarmerLeaderboardSection(params.isShowFarmerLeaderboardSection()); |
| 435 | ||
| 436 | Commons saved = commonsRepository.save(commons); | |
| 437 | ||
| 438 |
1
1. updateDashboardSettings : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::updateDashboardSettings → KILLED |
return ResponseEntity.ok().body(saved); |
| 439 | } | |
| 440 | ||
| 441 | @Operation(summary = "Get the number of cows for each farmer in a commons") | |
| 442 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 443 | @GetMapping("/numcows") | |
| 444 | public ResponseEntity<List<Integer>> getNumCowsForCommonsId( | |
| 445 | @Parameter(name="commonsId") @RequestParam Long commonsId) { | |
| 446 | Iterable<UserCommons> userCommonsList = userCommonsRepository.findByCommonsId(commonsId); | |
| 447 | List<Integer> numCowsList = StreamSupport.stream(userCommonsList.spliterator(), false) | |
| 448 | .map(UserCommons::getNumOfCows) | |
| 449 | .collect(Collectors.toList()); | |
| 450 |
1
1. getNumCowsForCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getNumCowsForCommonsId → KILLED |
return ResponseEntity.ok().body(numCowsList); |
| 451 | } | |
| 452 | ||
| 453 | @Operation(summary = "Get timeseries stats for a commons") | |
| 454 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 455 | @GetMapping("/timeseries") | |
| 456 | public ResponseEntity<List<Map<String, Object>>> getCommonsTimeSeries( | |
| 457 | @Parameter(name="commonId") @RequestParam Long commonId) { | |
| 458 | Iterable<CommonStats> commonStats = commonStatsRepository.findAllByCommonsId(commonId); | |
| 459 | List<CommonStats> sortedStats = StreamSupport.stream(commonStats.spliterator(), false) | |
| 460 | .sorted(Comparator.comparing(CommonStats::getCreateDate)) | |
| 461 | .collect(Collectors.toList()); | |
| 462 | ||
| 463 | List<Map<String, Object>> healthValues = sortedStats.stream() | |
| 464 |
1
1. lambda$getCommonsTimeSeries$6 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsTimeSeries$6 → KILLED |
.map(stat -> Map.<String, Object>of( |
| 465 | "date", stat.getCreateDate().toString(), | |
| 466 | "value", stat.getAvgHealth())) | |
| 467 | .collect(Collectors.toList()); | |
| 468 | ||
| 469 | List<Map<String, Object>> totalCowsValues = sortedStats.stream() | |
| 470 |
1
1. lambda$getCommonsTimeSeries$7 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/happiercows/controllers/CommonsController::lambda$getCommonsTimeSeries$7 → KILLED |
.map(stat -> Map.<String, Object>of( |
| 471 | "date", stat.getCreateDate().toString(), | |
| 472 | "value", stat.getNumCows())) | |
| 473 | .collect(Collectors.toList()); | |
| 474 | ||
| 475 | List<Map<String, Object>> timeSeries = List.of( | |
| 476 | Map.of( | |
| 477 | "name", "Health", | |
| 478 | "color", "#0088FE", | |
| 479 | "percentage", true, | |
| 480 | "values", healthValues), | |
| 481 | Map.of( | |
| 482 | "name", "Total Cows", | |
| 483 | "color", "#FF8042", | |
| 484 | "values", totalCowsValues)); | |
| 485 | ||
| 486 |
1
1. getCommonsTimeSeries : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonsController::getCommonsTimeSeries → KILLED |
return ResponseEntity.ok().body(timeSeries); |
| 487 | } | |
| 488 | ||
| 489 | | |
| 490 | } | |
Mutations | ||
| 106 |
1.1 |
|
| 115 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 132 |
1.1 |
|
| 141 |
1.1 |
|
| 143 |
1.1 |
|
| 158 |
1.1 |
|
| 166 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 |
|
| 173 |
1.1 |
|
| 174 |
1.1 |
|
| 175 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 180 |
1.1 |
|
| 181 |
1.1 |
|
| 184 |
1.1 2.2 |
|
| 189 |
1.1 |
|
| 193 |
1.1 2.2 |
|
| 197 |
1.1 2.2 |
|
| 201 |
1.1 2.2 |
|
| 205 |
1.1 2.2 |
|
| 209 |
1.1 |
|
| 211 |
1.1 |
|
| 212 |
1.1 |
|
| 215 |
1.1 |
|
| 225 |
1.1 |
|
| 227 |
1.1 |
|
| 253 |
1.1 |
|
| 256 |
1.1 |
|
| 263 |
1.1 |
|
| 267 |
1.1 2.2 |
|
| 271 |
1.1 2.2 |
|
| 275 |
1.1 2.2 |
|
| 280 |
1.1 2.2 |
|
| 284 |
1.1 2.2 |
|
| 288 |
1.1 |
|
| 293 |
1.1 |
|
| 304 |
1.1 |
|
| 307 |
1.1 |
|
| 310 |
1.1 |
|
| 322 |
1.1 |
|
| 331 |
1.1 |
|
| 345 |
1.1 |
|
| 347 |
1.1 2.2 |
|
| 353 |
1.1 |
|
| 356 |
1.1 |
|
| 374 |
1.1 |
|
| 386 |
1.1 |
|
| 390 |
1.1 |
|
| 392 |
1.1 |
|
| 395 |
1.1 |
|
| 406 |
1.1 |
|
| 410 |
1.1 |
|
| 414 |
1.1 |
|
| 425 |
1.1 |
|
| 427 |
1.1 |
|
| 428 |
1.1 |
|
| 429 |
1.1 |
|
| 430 |
1.1 |
|
| 431 |
1.1 |
|
| 432 |
1.1 |
|
| 433 |
1.1 |
|
| 434 |
1.1 |
|
| 438 |
1.1 |
|
| 450 |
1.1 |
|
| 464 |
1.1 |
|
| 470 |
1.1 |
|
| 486 |
1.1 |