| 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.UserCommonsRepository; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 16 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 17 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 18 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 19 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 20 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
| 21 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
| 22 | import edu.ucsb.cs156.happiercows.errors.NegativeSellNumberException; | |
| 23 | import edu.ucsb.cs156.happiercows.errors.NegativeBuyNumberException; | |
| 24 | ||
| 25 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 26 | import io.swagger.v3.oas.annotations.Operation; | |
| 27 | import io.swagger.v3.oas.annotations.Parameter; | |
| 28 | ||
| 29 | import org.springframework.http.ResponseEntity; | |
| 30 | import org.springframework.web.bind.annotation.PutMapping; | |
| 31 | ||
| 32 | @Tag(name = "User Commons") | |
| 33 | @RequestMapping("/api/usercommons") | |
| 34 | @RestController | |
| 35 | public class UserCommonsController extends ApiController { | |
| 36 | ||
| 37 | @Autowired | |
| 38 | private UserCommonsRepository userCommonsRepository; | |
| 39 | ||
| 40 | @Autowired | |
| 41 | private CommonsRepository commonsRepository; | |
| 42 | ||
| 43 | @Autowired | |
| 44 | ObjectMapper mapper; | |
| 45 | ||
| 46 | @Operation(summary = "Get a specific user commons (admin only)") | |
| 47 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 48 | @GetMapping("") | |
| 49 | public UserCommons getUserCommonsById( | |
| 50 | @Parameter(name="userId") @RequestParam Long userId, | |
| 51 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 52 | ||
| 53 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 54 | .orElseThrow( | |
| 55 |
1
1. lambda$getUserCommonsById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$0 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
| 56 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
| 57 | } | |
| 58 | ||
| 59 | @Operation(summary = "Get a user commons for current user") | |
| 60 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 61 | @GetMapping("/forcurrentuser") | |
| 62 | public UserCommons getUserCommonsById( | |
| 63 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 64 | ||
| 65 | User u = getCurrentUser().getUser(); | |
| 66 | Long userId = u.getId(); | |
| 67 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 68 | .orElseThrow( | |
| 69 |
1
1. lambda$getUserCommonsById$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$getUserCommonsById$1 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
| 70 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
| 71 | } | |
| 72 | ||
| 73 | @Operation(summary = "Buy a cow, totalWealth updated") | |
| 74 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 75 | @PutMapping("/buy") | |
| 76 | public ResponseEntity<String> putUserCommonsByIdBuy( | |
| 77 | @Parameter(name="commonsId") @RequestParam Long commonsId, | |
| 78 | @Parameter(name="numCows") @RequestParam int numCows) throws NotEnoughMoneyException, JsonProcessingException, NegativeBuyNumberException{ | |
| 79 | ||
| 80 | User u = getCurrentUser().getUser(); | |
| 81 | Long userId = u.getId(); | |
| 82 | ||
| 83 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
| 84 |
1
1. lambda$putUserCommonsByIdBuy$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$2 → KILLED |
()->new EntityNotFoundException(Commons.class, commonsId)); |
| 85 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 86 | .orElseThrow( | |
| 87 |
1
1. lambda$putUserCommonsByIdBuy$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$3 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
| 88 | | |
| 89 |
2
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : negated conditional → KILLED |
if(numCows < 0){ |
| 90 | throw new NegativeBuyNumberException("You cannot buy a negative number of cows!"); | |
| 91 | } | |
| 92 | ||
| 93 |
3
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED 3. putUserCommonsByIdBuy : negated conditional → KILLED |
else if(userCommons.getTotalWealth() >= (commons.getCowPrice() * numCows)){ |
| 94 |
3
1. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdBuy : Replaced double subtraction with addition → KILLED 3. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(userCommons.getTotalWealth() - (commons.getCowPrice() * numCows)); |
| 95 |
2
1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(userCommons.getNumOfCows() + numCows); |
| 96 |
2
1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsBought → KILLED |
userCommons.setCowsBought(userCommons.getCowsBought() + numCows); |
| 97 | } | |
| 98 | else{ | |
| 99 | throw new NotEnoughMoneyException("You need more money!"); | |
| 100 | } | |
| 101 | userCommonsRepository.save(userCommons); | |
| 102 | ||
| 103 | String body = mapper.writeValueAsString(userCommons); | |
| 104 |
1
1. putUserCommonsByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
| 105 | } | |
| 106 | ||
| 107 | @Operation(summary = "Sell a cow, totalWealth updated") | |
| 108 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 109 | @PutMapping("/sell") | |
| 110 | public ResponseEntity<String> putUserCommonsByIdSell( | |
| 111 | @Parameter(name="commonsId") @RequestParam Long commonsId, | |
| 112 | @Parameter(name="numCows") @RequestParam int numCows) throws NoCowsException, JsonProcessingException, NegativeSellNumberException { | |
| 113 | User u = getCurrentUser().getUser(); | |
| 114 | Long userId = u.getId(); | |
| 115 | ||
| 116 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
| 117 |
1
1. lambda$putUserCommonsByIdSell$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$4 → KILLED |
()->new EntityNotFoundException(Commons.class, commonsId)); |
| 118 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 119 | .orElseThrow( | |
| 120 |
1
1. lambda$putUserCommonsByIdSell$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$5 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
| 121 | ||
| 122 |
2
1. putUserCommonsByIdSell : changed conditional boundary → KILLED 2. putUserCommonsByIdSell : negated conditional → KILLED |
if (numCows < 0) { |
| 123 | throw new NegativeSellNumberException("You cannot sell a negative number of cows!"); | |
| 124 | } | |
| 125 |
2
1. putUserCommonsByIdSell : changed conditional boundary → KILLED 2. putUserCommonsByIdSell : negated conditional → KILLED |
else if(userCommons.getNumOfCows() >= numCows ){ |
| 126 |
2
1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdSell : Replaced double division with multiplication → KILLED |
double cowValue = commons.getCowPrice() * userCommons.getCowHealth() / 100; |
| 127 |
3
1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdSell : Replaced double addition with subtraction → KILLED 3. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(userCommons.getTotalWealth() + (cowValue * numCows)); |
| 128 |
2
1. putUserCommonsByIdSell : Replaced integer subtraction with addition → KILLED 2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(userCommons.getNumOfCows() - numCows); |
| 129 |
2
1. putUserCommonsByIdSell : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowsSold → KILLED |
userCommons.setCowsSold(userCommons.getCowsSold() + numCows); |
| 130 | } | |
| 131 | else{ | |
| 132 | throw new NoCowsException("You do not have enough cows to sell!"); | |
| 133 | } | |
| 134 | userCommonsRepository.save(userCommons); | |
| 135 | ||
| 136 | String body = mapper.writeValueAsString(userCommons); | |
| 137 |
1
1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
| 138 | } | |
| 139 | ||
| 140 | | |
| 141 | ||
| 142 | @Operation(summary = "Get all user commons for a specific commons") | |
| 143 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
| 144 | @GetMapping("/commons/all") | |
| 145 | public ResponseEntity<String> getUsersCommonsByCommonsId( | |
| 146 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 147 | Iterable<UserCommons> uc = userCommonsRepository.findByCommonsId(commonsId); | |
| 148 | | |
| 149 | | |
| 150 | String body = mapper.writeValueAsString(uc); | |
| 151 |
1
1. getUsersCommonsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED |
return ResponseEntity.ok().body(body); |
| 152 | } | |
| 153 | ||
| 154 | } | |
Mutations | ||
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 69 |
1.1 |
|
| 70 |
1.1 |
|
| 84 |
1.1 |
|
| 87 |
1.1 |
|
| 89 |
1.1 2.2 |
|
| 93 |
1.1 2.2 3.3 |
|
| 94 |
1.1 2.2 3.3 |
|
| 95 |
1.1 2.2 |
|
| 96 |
1.1 2.2 |
|
| 104 |
1.1 |
|
| 117 |
1.1 |
|
| 120 |
1.1 |
|
| 122 |
1.1 2.2 |
|
| 125 |
1.1 2.2 |
|
| 126 |
1.1 2.2 |
|
| 127 |
1.1 2.2 3.3 |
|
| 128 |
1.1 2.2 |
|
| 129 |
1.1 2.2 |
|
| 137 |
1.1 |
|
| 151 |
1.1 |