| 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 | ||
| 40 | import edu.ucsb.cs156.happiercows.entities.CowHerd; | |
| 41 | import edu.ucsb.cs156.happiercows.repositories.CowHerdRepository; | |
| 42 | ||
| 43 | ||
| 44 | ||
| 45 | @Tag(name = "User Game") | |
| 46 | @RequestMapping("/api/farmer") | |
| 47 | @RestController | |
| 48 | public class FarmerController extends ApiController { | |
| 49 | ||
| 50 | @Autowired | |
| 51 | private FarmerRepository farmerRepository; | |
| 52 | ||
| 53 | ||
| 54 | @Autowired | |
| 55 | private CowHerdRepository cowHerdRepository; | |
| 56 | ||
| 57 | @Autowired | |
| 58 | private GameRepository gameRepository; | |
| 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 cow, 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, JsonProcessingException{ | |
| 105 | ||
| 106 | User u = getCurrentUser().getUser(); | |
| 107 | Long userId = u.getId(); | |
| 108 | ||
| 109 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 110 |
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)); |
| 111 | ||
| 112 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 113 | ||
| 114 |
1
1. putFarmerByIdBuy : negated conditional → KILLED |
if (game.isHidden()) { |
| 115 | throw new GameHiddenException(gameId); | |
| 116 | } | |
| 117 | ||
| 118 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 119 | .orElseThrow( | |
| 120 |
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)); |
| 121 | ||
| 122 |
3
1. putFarmerByIdBuy : negated conditional → KILLED 2. putFarmerByIdBuy : changed conditional boundary → KILLED 3. putFarmerByIdBuy : Replaced double multiplication with division → KILLED |
if(farmer.getTotalWealth() >= (game.getCowPrice() * numCows)){ |
| 123 |
3
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setTotalWealth → KILLED 2. putFarmerByIdBuy : Replaced double multiplication with division → KILLED 3. putFarmerByIdBuy : Replaced double subtraction with addition → KILLED |
farmer.setTotalWealth(farmer.getTotalWealth() - (game.getCowPrice() * numCows)); |
| 124 |
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); |
| 125 |
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); |
| 126 | } | |
| 127 | else{ | |
| 128 | throw new NotEnoughMoneyException("You need more money!"); | |
| 129 | } | |
| 130 | farmerRepository.save(farmer); | |
| 131 | CowHerd herd = CowHerd.builder() | |
| 132 | .farmer(farmer) | |
| 133 | .numOfCows(numCows) | |
| 134 | .cowHealth(100) | |
| 135 | .build(); | |
| 136 | ||
| 137 | cowHerdRepository.save(herd); | |
| 138 | ||
| 139 |
1
1. putFarmerByIdBuy : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 140 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_BUY, numCows); | |
| 141 | ||
| 142 | String body = mapper.writeValueAsString(farmer); | |
| 143 |
1
1. putFarmerByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
| 144 | } | |
| 145 | ||
| 146 | @Operation(summary = "Sell a cow, totalWealth updated") | |
| 147 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 148 | @PutMapping("/sell") | |
| 149 | public ResponseEntity<String> putFarmerByIdSell( | |
| 150 | @Parameter(name="gameId") @RequestParam Long gameId, | |
| 151 | @Parameter(name="numCows") @RequestParam int numCows) throws NoCowsException, JsonProcessingException { | |
| 152 | User u = getCurrentUser().getUser(); | |
| 153 | Long userId = u.getId(); | |
| 154 | ||
| 155 | Game game = gameRepository.findById(gameId).orElseThrow( | |
| 156 |
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)); |
| 157 | ||
| 158 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/controllers/FarmerController::ensureUserCanAccessCourseLinkedGame → KILLED |
ensureUserCanAccessCourseLinkedGame(u, game); |
| 159 | ||
| 160 |
1
1. putFarmerByIdSell : negated conditional → KILLED |
if (game.isHidden()) { |
| 161 | throw new GameHiddenException(gameId); | |
| 162 | } | |
| 163 | ||
| 164 | Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 165 | .orElseThrow( | |
| 166 |
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)); |
| 167 | ||
| 168 | ||
| 169 |
2
1. putFarmerByIdSell : negated conditional → KILLED 2. putFarmerByIdSell : changed conditional boundary → KILLED |
if(farmer.getNumOfCows() >= numCows ){ |
| 170 |
2
1. putFarmerByIdSell : Replaced double division with multiplication → KILLED 2. putFarmerByIdSell : Replaced double multiplication with division → KILLED |
double cowValue = game.getCowPrice() * farmer.getCowHealth() / 100; |
| 171 |
3
1. putFarmerByIdSell : Replaced double addition with subtraction → KILLED 2. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setTotalWealth → KILLED 3. putFarmerByIdSell : Replaced double multiplication with division → KILLED |
farmer.setTotalWealth(farmer.getTotalWealth() + (cowValue * numCows)); |
| 172 |
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() - numCows); |
| 173 |
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() + numCows); |
| 174 | } | |
| 175 | else{ | |
| 176 | throw new NoCowsException("You do not have enough cows to sell!"); | |
| 177 | } | |
| 178 | ||
| 179 | farmerRepository.save(farmer); | |
| 180 | ||
| 181 |
1
1. putFarmerByIdSell : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED |
farmerActivityService.recordActivityIfStudentMatch( |
| 182 | u, game, farmer, FarmerActivity.ACTIVITY_TYPE_SELL, numCows); | |
| 183 | ||
| 184 | String body = mapper.writeValueAsString(farmer); | |
| 185 |
1
1. putFarmerByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerController::putFarmerByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
| 186 | } | |
| 187 | ||
| 188 | | |
| 189 | ||
| 190 | @Operation(summary = "Get all user game for a specific game") | |
| 191 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 192 | @GetMapping("/game/all") | |
| 193 | public List<FarmerWithStudentStatus> getFarmersByGameId( | |
| 194 | @Parameter(name="gameId") @RequestParam Long gameId) { | |
| 195 | ||
| 196 | Game game = gameRepository.findById(gameId) | |
| 197 |
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)); |
| 198 | ||
| 199 | List<FarmerWithStudentStatus> result = new ArrayList<>(); | |
| 200 | for (Farmer farmer : farmerRepository.findByGameId(gameId)) { | |
| 201 | boolean isStudent = courseAccessService | |
| 202 | .findMatchingStudentForCourseLinkedGame(farmer.getUser(), game) | |
| 203 | .isPresent(); | |
| 204 | ||
| 205 | result.add(FarmerWithStudentStatus.builder() | |
| 206 | .userId(farmer.getUserId()) | |
| 207 | .gameId(farmer.getGameId()) | |
| 208 | .username(farmer.getUsername()) | |
| 209 | .totalWealth(farmer.getTotalWealth()) | |
| 210 | .numOfCows(farmer.getNumOfCows()) | |
| 211 | .cowHealth(farmer.getCowHealth()) | |
| 212 | .cowsBought(farmer.getCowsBought()) | |
| 213 | .cowsSold(farmer.getCowsSold()) | |
| 214 | .cowDeaths(farmer.getCowDeaths()) | |
| 215 | .student(isStudent) | |
| 216 | .build()); | |
| 217 | } | |
| 218 |
1
1. getFarmersByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerController::getFarmersByGameId → KILLED |
return result; |
| 219 | } | |
| 220 | ||
| 221 | private void ensureUserCanAccessCourseLinkedGame(User user, Game game) { | |
| 222 |
2
1. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED 2. ensureUserCanAccessCourseLinkedGame : negated conditional → KILLED |
if (game.getCourseId() != null && !courseAccessService.isEligibleForGame(user, game)) { |
| 223 | throw new NotEnrolledInCourseAssociatedWithGameException(); | |
| 224 | } | |
| 225 | } | |
| 226 | ||
| 227 | } | |
Mutations | ||
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 114 |
1.1 |
|
| 120 |
1.1 |
|
| 122 |
1.1 2.2 3.3 |
|
| 123 |
1.1 2.2 3.3 |
|
| 124 |
1.1 2.2 |
|
| 125 |
1.1 2.2 |
|
| 139 |
1.1 |
|
| 143 |
1.1 |
|
| 156 |
1.1 |
|
| 158 |
1.1 |
|
| 160 |
1.1 |
|
| 166 |
1.1 |
|
| 169 |
1.1 2.2 |
|
| 170 |
1.1 2.2 |
|
| 171 |
1.1 2.2 3.3 |
|
| 172 |
1.1 2.2 |
|
| 173 |
1.1 2.2 |
|
| 181 |
1.1 |
|
| 185 |
1.1 |
|
| 197 |
1.1 |
|
| 218 |
1.1 |
|
| 222 |
1.1 2.2 |