| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | ||
| 8 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | import org.springframework.web.bind.annotation.GetMapping; | |
| 10 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 11 | import org.springframework.web.bind.annotation.RequestParam; | |
| 12 | import org.springframework.web.bind.annotation.RestController; | |
| 13 | ||
| 14 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 16 | import edu.ucsb.cs156.happiercows.repositories.HerdRepository; | |
| 17 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 18 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 19 | import edu.ucsb.cs156.happiercows.entities.FarmerActivity; | |
| 20 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 21 | import edu.ucsb.cs156.happiercows.entities.Herd; | |
| 22 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 23 | import edu.ucsb.cs156.happiercows.errors.InvalidNumberOfCowsException; | |
| 24 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
| 25 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
| 26 | import edu.ucsb.cs156.happiercows.errors.GameHiddenException; | |
| 27 | import edu.ucsb.cs156.happiercows.errors.NotEnrolledInCourseAssociatedWithGameException; | |
| 28 | import edu.ucsb.cs156.happiercows.models.FarmerWithStudentStatus; | |
| 29 | import edu.ucsb.cs156.happiercows.services.CourseAccessService; | |
| 30 | import edu.ucsb.cs156.happiercows.services.FarmerActivityService; | |
| 31 | ||
| 32 | import java.util.ArrayList; | |
| 33 | import java.util.HashMap; | |
| 34 | import java.util.Map; | |
| 35 | import java.util.List; | |
| 36 | ||
| 37 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 38 | import io.swagger.v3.oas.annotations.Operation; | |
| 39 | import io.swagger.v3.oas.annotations.Parameter; | |
| 40 | ||
| 41 | import org.springframework.http.ResponseEntity; | |
| 42 | import org.springframework.web.bind.annotation.PutMapping; | |
| 43 | import org.springframework.web.bind.annotation.RequestBody; | |
| 44 | import org.springframework.transaction.annotation.Transactional; | |
| 45 | ||
| 46 | @Tag(name = "User Game") | |
| 47 | @RequestMapping("/api/farmer") | |
| 48 | @RestController | |
| 49 | public class FarmerController extends ApiController { | |
| 50 | ||
| 51 | @Autowired | |
| 52 | private FarmerRepository farmerRepository; | |
| 53 | ||
| 54 | @Autowired | |
| 55 | private GameRepository gameRepository; | |
| 56 | ||
| 57 | @Autowired | |
| 58 | private HerdRepository herdRepository; | |
| 59 | ||
| 60 | @Autowired | |
| 61 | ObjectMapper mapper; | |
| 62 | ||
| 63 | @Autowired | |
| 64 | private CourseAccessService courseAccessService; | |
| 65 | ||
| 66 | @Autowired | |
| 67 | private FarmerActivityService farmerActivityService; | |
| 68 | ||
| 69 | @Operation(summary = "Get a specific user game (admin only)") | |
| 70 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 | @GetMapping("") | |
| 72 | public Farmer getFarmerById( | |
| 73 | @Parameter(name="userId") @RequestParam Long userId, | |
| 74 | @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException { | |
| 75 | ||
| 76 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 77 | .orElseThrow( | |
| 78 |
1
1. lambda$getFarmerById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$getFarmerById$0 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 79 |
1
1. getFarmerById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmerById → KILLED |
return farmer; |
| 80 | } | |
| 81 | ||
| 82 | @Operation(summary = "Get a user game for current user") | |
| 83 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 84 | @GetMapping("/forcurrentuser") | |
| 85 | public Farmer getFarmerById( | |
| 86 | @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException { | |
| 87 | ||
| 88 | User u = getCurrentUser().getUser(); | |
| 89 | Long userId = u.getId(); | |
| 90 | Game game = gameRepository.findById(gameId) | |
| 91 |
1
1. lambda$getFarmerById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$getFarmerById$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", gameId)); |
| 92 |
1
1. getFarmerById : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 93 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 94 | .orElseThrow( | |
| 95 |
1
1. lambda$getFarmerById$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$getFarmerById$2 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 96 |
1
1. getFarmerById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmerById → KILLED |
return farmer; |
| 97 | } | |
| 98 | ||
| 99 | @Operation(summary = "Buy a herd of numCows cows (starting at 100 health), totalWealth updated") | |
| 100 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 101 | @PutMapping("/buy") | |
| 102 | public ResponseEntity<String> putFarmerByIdBuy( | |
| 103 | @Parameter(name="gameId") @RequestParam Long gameId, | |
| 104 | @Parameter(name="numCows") @RequestParam int numCows) throws NotEnoughMoneyException, InvalidNumberOfCowsException, JsonProcessingException{ | |
| 105 | ||
| 106 |
2
1. putFarmerByIdBuy : changed conditional boundary → KILLED 2. putFarmerByIdBuy : negated conditional → KILLED |
if (numCows < 1) { |
| 107 | throw new InvalidNumberOfCowsException("You must buy at least 1 cow!"); | |
| 108 | } | |
| 109 | ||
| 110 | User u = getCurrentUser().getUser(); | |
| 111 | Long userId = u.getId(); | |
| 112 | ||
| 113 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 114 |
1
1. lambda$putFarmerByIdBuy$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$putFarmerByIdBuy$0 → KILLED |
()->new EntityNotFoundException("Game", gameId)); |
| 115 | ||
| 116 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 117 | ||
| 118 |
1
1. putFarmerByIdBuy : negated conditional → KILLED |
if (game.isHidden()) { |
| 119 | throw new GameHiddenException(gameId); | |
| 120 | } | |
| 121 | ||
| 122 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 123 | .orElseThrow( | |
| 124 |
1
1. lambda$putFarmerByIdBuy$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$putFarmerByIdBuy$1 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 125 | ||
| 126 |
1
1. putFarmerByIdBuy : Replaced double addition with subtraction → KILLED |
double costPerCow = game.getCowPrice() + game.getPurchaseFee(); |
| 127 |
3
1. putFarmerByIdBuy : Replaced double multiplication with division → KILLED 2. putFarmerByIdBuy : negated conditional → KILLED 3. putFarmerByIdBuy : changed conditional boundary → KILLED |
if(farmer.getTotalWealth() >= (costPerCow * numCows)){ |
| 128 |
3
1. putFarmerByIdBuy : Replaced double multiplication with division → KILLED 2. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setTotalWealth → KILLED 3. putFarmerByIdBuy : Replaced double subtraction with addition → KILLED |
farmer.setTotalWealth(farmer.getTotalWealth() - (costPerCow * numCows)); |
| 129 |
2
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED 2. putFarmerByIdBuy : Replaced integer addition with subtraction → KILLED |
farmer.setNumOfCows(farmer.getNumOfCows() + numCows); |
| 130 |
2
1. putFarmerByIdBuy : Replaced integer addition with subtraction → KILLED 2. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowsBought → KILLED |
farmer.setCowsBought(farmer.getCowsBought() + numCows); |
| 131 | } | |
| 132 | else{ | |
| 133 | throw new NotEnoughMoneyException("You need more money!"); | |
| 134 | } | |
| 135 | farmerRepository.save(farmer); | |
| 136 | herdRepository.save(Herd.builder().farmer(farmer).numCows(numCows).health(100).build()); | |
| 137 | ||
| 138 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 139 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_BUY, numCows); | |
| 140 | ||
| 141 | String body = mapper.writeValueAsString(farmer); | |
| 142 |
1
1. putFarmerByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * Sells cows from specific herds. The request body maps each herd id to the | |
| 147 | * number of cows to sell from that herd, e.g. {"12": 3, "15": 1}. Every herd | |
| 148 | * must belong to the current user in this game, and each count must be | |
| 149 | * between 0 and the number of cows in that herd. Each cow sells for the cow | |
| 150 | * price times its herd's health (as a percentage), minus the selling fee. | |
| 151 | * Herds that reach 0 cows are removed. | |
| 152 | */ | |
| 153 | @Operation(summary = "Sell cows from specific herds, totalWealth updated") | |
| 154 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 155 | @PutMapping("/sell") | |
| 156 | @Transactional | |
| 157 | public ResponseEntity<String> putFarmerByIdSell( | |
| 158 | @Parameter(name="gameId") @RequestParam Long gameId, | |
| 159 | @RequestBody Map<Long, Integer> herdCounts) throws NoCowsException, InvalidNumberOfCowsException, JsonProcessingException { | |
| 160 | User u = getCurrentUser().getUser(); | |
| 161 | Long userId = u.getId(); | |
| 162 | ||
| 163 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 164 |
1
1. lambda$putFarmerByIdSell$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$putFarmerByIdSell$0 → KILLED |
()->new EntityNotFoundException("Game", gameId)); |
| 165 | ||
| 166 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 167 | ||
| 168 |
1
1. putFarmerByIdSell : negated conditional → KILLED |
if (game.isHidden()) { |
| 169 | throw new GameHiddenException(gameId); | |
| 170 | } | |
| 171 | ||
| 172 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 173 | .orElseThrow( | |
| 174 |
1
1. lambda$putFarmerByIdSell$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$putFarmerByIdSell$1 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 175 | ||
| 176 | // Only the current user's herds in this game can be sold from. | |
| 177 | Map<Long, Herd> herdsById = new HashMap<>(); | |
| 178 | for (Herd herd : herdRepository.findByGameIdAndUserId(gameId, userId)) { | |
| 179 | herdsById.put(herd.getId(), herd); | |
| 180 | } | |
| 181 | ||
| 182 | // Validate every requested count before changing anything. | |
| 183 | int totalCows = 0; | |
| 184 | double totalValue = 0; | |
| 185 | for (Map.Entry<Long, Integer> entry : herdCounts.entrySet()) { | |
| 186 | Herd herd = herdsById.get(entry.getKey()); | |
| 187 |
1
1. putFarmerByIdSell : negated conditional → KILLED |
if (herd == null) { |
| 188 | throw new EntityNotFoundException(Herd.class, entry.getKey()); | |
| 189 | } | |
| 190 | Integer count = entry.getValue(); | |
| 191 |
3
1. putFarmerByIdSell : negated conditional → KILLED 2. putFarmerByIdSell : negated conditional → KILLED 3. putFarmerByIdSell : changed conditional boundary → KILLED |
if (count == null || count < 0) { |
| 192 | throw new InvalidNumberOfCowsException("You cannot sell a negative number of cows!"); | |
| 193 | } | |
| 194 |
2
1. putFarmerByIdSell : negated conditional → KILLED 2. putFarmerByIdSell : changed conditional boundary → KILLED |
if (count > herd.getNumCows()) { |
| 195 | throw new NoCowsException("You do not have enough cows in that herd to sell!"); | |
| 196 | } | |
| 197 |
2
1. putFarmerByIdSell : Replaced double division with multiplication → KILLED 2. putFarmerByIdSell : Replaced double multiplication with division → KILLED |
double cowValue = game.getCowPrice() * herd.getHealth() / 100 |
| 198 |
1
1. putFarmerByIdSell : Replaced double subtraction with addition → KILLED |
- game.getSellingFee(); |
| 199 |
1
1. putFarmerByIdSell : Replaced integer addition with subtraction → KILLED |
totalCows += count; |
| 200 |
2
1. putFarmerByIdSell : Replaced double addition with subtraction → KILLED 2. putFarmerByIdSell : Replaced double multiplication with division → KILLED |
totalValue += cowValue * count; |
| 201 | } | |
| 202 |
2
1. putFarmerByIdSell : changed conditional boundary → KILLED 2. putFarmerByIdSell : negated conditional → KILLED |
if (totalCows < 1) { |
| 203 | throw new InvalidNumberOfCowsException("You must sell at least 1 cow!"); | |
| 204 | } | |
| 205 | ||
| 206 | for (Map.Entry<Long, Integer> entry : herdCounts.entrySet()) { | |
| 207 | Herd herd = herdsById.get(entry.getKey()); | |
| 208 |
1
1. putFarmerByIdSell : Replaced integer subtraction with addition → KILLED |
int remaining = herd.getNumCows() - entry.getValue(); |
| 209 |
1
1. putFarmerByIdSell : negated conditional → KILLED |
if (remaining == 0) { |
| 210 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/repositories/HerdRepository::delete → KILLED |
herdRepository.delete(herd); |
| 211 |
2
1. putFarmerByIdSell : negated conditional → KILLED 2. putFarmerByIdSell : changed conditional boundary → KILLED |
} else if (remaining < herd.getNumCows()) { |
| 212 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Herd::setNumCows → KILLED |
herd.setNumCows(remaining); |
| 213 | herdRepository.save(herd); | |
| 214 | } | |
| 215 | } | |
| 216 | ||
| 217 |
2
1. putFarmerByIdSell : Replaced double addition with subtraction → KILLED 2. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setTotalWealth → KILLED |
farmer.setTotalWealth(farmer.getTotalWealth() + totalValue); |
| 218 |
2
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED 2. putFarmerByIdSell : Replaced integer subtraction with addition → KILLED |
farmer.setNumOfCows(farmer.getNumOfCows() - totalCows); |
| 219 |
2
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowsSold → KILLED 2. putFarmerByIdSell : Replaced integer addition with subtraction → KILLED |
farmer.setCowsSold(farmer.getCowsSold() + totalCows); |
| 220 | farmerRepository.save(farmer); | |
| 221 | ||
| 222 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 223 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_SELL, totalCows); | |
| 224 | ||
| 225 | String body = mapper.writeValueAsString(farmer); | |
| 226 |
1
1. putFarmerByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
| 227 | } | |
| 228 | ||
| 229 | | |
| 230 | ||
| 231 | @Operation(summary = "Get all user game for a specific game") | |
| 232 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 233 | @GetMapping("/game/all") | |
| 234 | public List<FarmerWithStudentStatus> getFarmersByGameId( | |
| 235 | @Parameter(name="gameId") @RequestParam Long gameId) { | |
| 236 | ||
| 237 | Game game = gameRepository.findById(gameId) | |
| 238 |
1
1. lambda$getFarmersByGameId$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::lambda$getFarmersByGameId$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", gameId)); |
| 239 | ||
| 240 | List<FarmerWithStudentStatus> result = new ArrayList<>(); | |
| 241 | for (Farmer farmer : farmerRepository.findByGameId(gameId)) { | |
| 242 | boolean isStudent = courseAccessService | |
| 243 | .findMatchingStudentForCourseLinkedGame(farmer.getUser(), game) | |
| 244 | .isPresent(); | |
| 245 | ||
| 246 | result.add(FarmerWithStudentStatus.builder() | |
| 247 | .userId(farmer.getUserId()) | |
| 248 | .gameId(farmer.getGameId()) | |
| 249 | .username(farmer.getUsername()) | |
| 250 | .totalWealth(farmer.getTotalWealth()) | |
| 251 | .numOfCows(farmer.getNumOfCows()) | |
| 252 | .cowHealth(farmer.getCowHealth()) | |
| 253 | .cowsBought(farmer.getCowsBought()) | |
| 254 | .cowsSold(farmer.getCowsSold()) | |
| 255 | .cowDeaths(farmer.getCowDeaths()) | |
| 256 | .student(isStudent) | |
| 257 | .build()); | |
| 258 | } | |
| 259 |
1
1. getFarmersByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmersByGameId → KILLED |
return result; |
| 260 | } | |
| 261 | ||
| 262 | private void ensureUserCanAccessCourseLinkedGame(User user, Game game) { | |
| 263 |
2
1. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED 2. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED |
if (game.getCourseId() != null && !courseAccessService.isEligibleForGame(user, game)) { |
| 264 | throw new NotEnrolledInCourseAssociatedWithGameException(); | |
| 265 | } | |
| 266 | } | |
| 267 | ||
| 268 | } | |
Mutations | ||
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 106 |
1.1 2.2 |
|
| 114 |
1.1 |
|
| 116 |
1.1 |
|
| 118 |
1.1 |
|
| 124 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 2.2 3.3 |
|
| 128 |
1.1 2.2 3.3 |
|
| 129 |
1.1 2.2 |
|
| 130 |
1.1 2.2 |
|
| 138 |
1.1 |
|
| 142 |
1.1 |
|
| 164 |
1.1 |
|
| 166 |
1.1 |
|
| 168 |
1.1 |
|
| 174 |
1.1 |
|
| 187 |
1.1 |
|
| 191 |
1.1 2.2 3.3 |
|
| 194 |
1.1 2.2 |
|
| 197 |
1.1 2.2 |
|
| 198 |
1.1 |
|
| 199 |
1.1 |
|
| 200 |
1.1 2.2 |
|
| 202 |
1.1 2.2 |
|
| 208 |
1.1 |
|
| 209 |
1.1 |
|
| 210 |
1.1 |
|
| 211 |
1.1 2.2 |
|
| 212 |
1.1 |
|
| 217 |
1.1 2.2 |
|
| 218 |
1.1 2.2 |
|
| 219 |
1.1 2.2 |
|
| 222 |
1.1 |
|
| 226 |
1.1 |
|
| 238 |
1.1 |
|
| 259 |
1.1 |
|
| 263 |
1.1 2.2 |