| 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.entities.User; | |
| 17 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 18 | import edu.ucsb.cs156.happiercows.entities.FarmerActivity; | |
| 19 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 20 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 21 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
| 22 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
| 23 | import edu.ucsb.cs156.happiercows.errors.GameHiddenException; | |
| 24 | import edu.ucsb.cs156.happiercows.errors.NotEnrolledInCourseAssociatedWithGameException; | |
| 25 | import edu.ucsb.cs156.happiercows.models.FarmerWithStudentStatus; | |
| 26 | import edu.ucsb.cs156.happiercows.services.CourseAccessService; | |
| 27 | import edu.ucsb.cs156.happiercows.services.FarmerActivityService; | |
| 28 | ||
| 29 | import java.util.ArrayList; | |
| 30 | import java.util.List; | |
| 31 | ||
| 32 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 33 | import io.swagger.v3.oas.annotations.Operation; | |
| 34 | import io.swagger.v3.oas.annotations.Parameter; | |
| 35 | ||
| 36 | import org.springframework.http.ResponseEntity; | |
| 37 | import org.springframework.web.bind.annotation.PutMapping; | |
| 38 | ||
| 39 | @Tag(name = "User Game") | |
| 40 | @RequestMapping("/api/farmer") | |
| 41 | @RestController | |
| 42 | public class FarmerController extends ApiController { | |
| 43 | ||
| 44 | @Autowired | |
| 45 | private FarmerRepository farmerRepository; | |
| 46 | ||
| 47 | @Autowired | |
| 48 | private GameRepository gameRepository; | |
| 49 | ||
| 50 | @Autowired | |
| 51 | ObjectMapper mapper; | |
| 52 | ||
| 53 | @Autowired | |
| 54 | private CourseAccessService courseAccessService; | |
| 55 | ||
| 56 | @Autowired | |
| 57 | private FarmerActivityService farmerActivityService; | |
| 58 | ||
| 59 | @Operation(summary = "Get a specific user game (admin only)") | |
| 60 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 | @GetMapping("") | |
| 62 | public Farmer getFarmerById( | |
| 63 | @Parameter(name="userId") @RequestParam Long userId, | |
| 64 | @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException { | |
| 65 | ||
| 66 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 67 | .orElseThrow( | |
| 68 |
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)); |
| 69 |
1
1. getFarmerById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmerById → KILLED |
return farmer; |
| 70 | } | |
| 71 | ||
| 72 | @Operation(summary = "Get a user game for current user") | |
| 73 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 74 | @GetMapping("/forcurrentuser") | |
| 75 | public Farmer getFarmerById( | |
| 76 | @Parameter(name="gameId") @RequestParam Long gameId) throws JsonProcessingException { | |
| 77 | ||
| 78 | User u = getCurrentUser().getUser(); | |
| 79 | Long userId = u.getId(); | |
| 80 | Game game = gameRepository.findById(gameId) | |
| 81 |
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)); |
| 82 |
1
1. getFarmerById : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 83 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 84 | .orElseThrow( | |
| 85 |
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)); |
| 86 |
1
1. getFarmerById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmerById → KILLED |
return farmer; |
| 87 | } | |
| 88 | ||
| 89 | @Operation(summary = "Buy a cow, totalWealth updated") | |
| 90 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 91 | @PutMapping("/buy") | |
| 92 | public ResponseEntity<String> putFarmerByIdBuy( | |
| 93 | @Parameter(name="gameId") @RequestParam Long gameId, | |
| 94 | @Parameter(name="numCows") @RequestParam int numCows) throws NotEnoughMoneyException, JsonProcessingException{ | |
| 95 | ||
| 96 | User u = getCurrentUser().getUser(); | |
| 97 | Long userId = u.getId(); | |
| 98 | ||
| 99 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 100 |
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)); |
| 101 | ||
| 102 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 103 | ||
| 104 |
1
1. putFarmerByIdBuy : negated conditional → KILLED |
if (game.isHidden()) { |
| 105 | throw new GameHiddenException(gameId); | |
| 106 | } | |
| 107 | ||
| 108 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 109 | .orElseThrow( | |
| 110 |
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)); |
| 111 | ||
| 112 |
1
1. putFarmerByIdBuy : Replaced double addition with subtraction → KILLED |
double costPerCow = game.getCowPrice() + game.getPurchaseFee(); |
| 113 |
3
1. putFarmerByIdBuy : changed conditional boundary → KILLED 2. putFarmerByIdBuy : Replaced double multiplication with division → KILLED 3. putFarmerByIdBuy : negated conditional → KILLED |
if(farmer.getTotalWealth() >= (costPerCow * numCows)){ |
| 114 |
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)); |
| 115 |
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); |
| 116 |
2
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowsBought → KILLED 2. putFarmerByIdBuy : Replaced integer addition with subtraction → KILLED |
farmer.setCowsBought(farmer.getCowsBought() + numCows); |
| 117 | } | |
| 118 | else{ | |
| 119 | throw new NotEnoughMoneyException("You need more money!"); | |
| 120 | } | |
| 121 | farmerRepository.save(farmer); | |
| 122 | ||
| 123 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 124 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_BUY, numCows); | |
| 125 | ||
| 126 | String body = mapper.writeValueAsString(farmer); | |
| 127 |
1
1. putFarmerByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
| 128 | } | |
| 129 | ||
| 130 | @Operation(summary = "Sell a cow, totalWealth updated") | |
| 131 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 132 | @PutMapping("/sell") | |
| 133 | public ResponseEntity<String> putFarmerByIdSell( | |
| 134 | @Parameter(name="gameId") @RequestParam Long gameId, | |
| 135 | @Parameter(name="numCows") @RequestParam int numCows) throws NoCowsException, JsonProcessingException { | |
| 136 | User u = getCurrentUser().getUser(); | |
| 137 | Long userId = u.getId(); | |
| 138 | ||
| 139 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 140 |
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)); |
| 141 | ||
| 142 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 143 | ||
| 144 |
1
1. putFarmerByIdSell : negated conditional → KILLED |
if (game.isHidden()) { |
| 145 | throw new GameHiddenException(gameId); | |
| 146 | } | |
| 147 | ||
| 148 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 149 | .orElseThrow( | |
| 150 |
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)); |
| 151 | ||
| 152 | ||
| 153 |
2
1. putFarmerByIdSell : negated conditional → KILLED 2. putFarmerByIdSell : changed conditional boundary → KILLED |
if(farmer.getNumOfCows() >= numCows ){ |
| 154 |
2
1. putFarmerByIdSell : Replaced double division with multiplication → KILLED 2. putFarmerByIdSell : Replaced double multiplication with division → KILLED |
double cowValue = game.getCowPrice() * farmer.getCowHealth() / 100 |
| 155 |
1
1. putFarmerByIdSell : Replaced double subtraction with addition → KILLED |
- game.getSellingFee(); |
| 156 |
3
1. putFarmerByIdSell : Replaced double multiplication with division → KILLED 2. putFarmerByIdSell : Replaced double addition with subtraction → KILLED 3. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setTotalWealth → KILLED |
farmer.setTotalWealth(farmer.getTotalWealth() + (cowValue * numCows)); |
| 157 |
2
1. putFarmerByIdSell : Replaced integer subtraction with addition → KILLED 2. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED |
farmer.setNumOfCows(farmer.getNumOfCows() - numCows); |
| 158 |
2
1. putFarmerByIdSell : Replaced integer addition with subtraction → KILLED 2. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowsSold → KILLED |
farmer.setCowsSold(farmer.getCowsSold() + numCows); |
| 159 | } | |
| 160 | else{ | |
| 161 | throw new NoCowsException("You do not have enough cows to sell!"); | |
| 162 | } | |
| 163 | farmerRepository.save(farmer); | |
| 164 | ||
| 165 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 166 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_SELL, numCows); | |
| 167 | ||
| 168 | String body = mapper.writeValueAsString(farmer); | |
| 169 |
1
1. putFarmerByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
| 170 | } | |
| 171 | ||
| 172 | | |
| 173 | ||
| 174 | @Operation(summary = "Get all user game for a specific game") | |
| 175 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 176 | @GetMapping("/game/all") | |
| 177 | public List<FarmerWithStudentStatus> getFarmersByGameId( | |
| 178 | @Parameter(name="gameId") @RequestParam Long gameId) { | |
| 179 | ||
| 180 | Game game = gameRepository.findById(gameId) | |
| 181 |
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)); |
| 182 | ||
| 183 | List<FarmerWithStudentStatus> result = new ArrayList<>(); | |
| 184 | for (Farmer farmer : farmerRepository.findByGameId(gameId)) { | |
| 185 | boolean isStudent = courseAccessService | |
| 186 | .findMatchingStudentForCourseLinkedGame(farmer.getUser(), game) | |
| 187 | .isPresent(); | |
| 188 | ||
| 189 | result.add(FarmerWithStudentStatus.builder() | |
| 190 | .userId(farmer.getUserId()) | |
| 191 | .gameId(farmer.getGameId()) | |
| 192 | .username(farmer.getUsername()) | |
| 193 | .totalWealth(farmer.getTotalWealth()) | |
| 194 | .numOfCows(farmer.getNumOfCows()) | |
| 195 | .cowHealth(farmer.getCowHealth()) | |
| 196 | .cowsBought(farmer.getCowsBought()) | |
| 197 | .cowsSold(farmer.getCowsSold()) | |
| 198 | .cowDeaths(farmer.getCowDeaths()) | |
| 199 | .student(isStudent) | |
| 200 | .build()); | |
| 201 | } | |
| 202 |
1
1. getFarmersByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmersByGameId → KILLED |
return result; |
| 203 | } | |
| 204 | ||
| 205 | private void ensureUserCanAccessCourseLinkedGame(User user, Game game) { | |
| 206 |
2
1. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED 2. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED |
if (game.getCourseId() != null && !courseAccessService.isEligibleForGame(user, game)) { |
| 207 | throw new NotEnrolledInCourseAssociatedWithGameException(); | |
| 208 | } | |
| 209 | } | |
| 210 | ||
| 211 | } | |
Mutations | ||
| 68 |
1.1 |
|
| 69 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 100 |
1.1 |
|
| 102 |
1.1 |
|
| 104 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 2.2 3.3 |
|
| 114 |
1.1 2.2 3.3 |
|
| 115 |
1.1 2.2 |
|
| 116 |
1.1 2.2 |
|
| 123 |
1.1 |
|
| 127 |
1.1 |
|
| 140 |
1.1 |
|
| 142 |
1.1 |
|
| 144 |
1.1 |
|
| 150 |
1.1 |
|
| 153 |
1.1 2.2 |
|
| 154 |
1.1 2.2 |
|
| 155 |
1.1 |
|
| 156 |
1.1 2.2 3.3 |
|
| 157 |
1.1 2.2 |
|
| 158 |
1.1 2.2 |
|
| 165 |
1.1 |
|
| 169 |
1.1 |
|
| 181 |
1.1 |
|
| 202 |
1.1 |
|
| 206 |
1.1 2.2 |