1 | package edu.ucsb.cs156.happiercows.controllers; | |
2 | ||
3 | import java.util.Optional; | |
4 | import java.util.ArrayList; | |
5 | import java.util.List; | |
6 | import java.util.Iterator; | |
7 | import java.util.*; | |
8 | import java.util.stream.*; | |
9 | ||
10 | import com.fasterxml.jackson.core.JsonProcessingException; | |
11 | import com.fasterxml.jackson.databind.ObjectMapper; | |
12 | ||
13 | import org.springframework.beans.factory.annotation.Autowired; | |
14 | import org.springframework.format.annotation.DateTimeFormat; | |
15 | import org.springframework.http.HttpStatus; | |
16 | import org.springframework.http.ResponseEntity; | |
17 | import org.springframework.security.access.prepost.PreAuthorize; | |
18 | import org.springframework.web.bind.annotation.DeleteMapping; | |
19 | import org.springframework.web.bind.annotation.GetMapping; | |
20 | import org.springframework.web.bind.annotation.PathVariable; | |
21 | import org.springframework.web.bind.annotation.PostMapping; | |
22 | import org.springframework.web.bind.annotation.PutMapping; | |
23 | import org.springframework.web.bind.annotation.RequestBody; | |
24 | import org.springframework.web.bind.annotation.RequestMapping; | |
25 | import org.springframework.web.bind.annotation.RequestParam; | |
26 | import org.springframework.web.bind.annotation.RestController; | |
27 | ||
28 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
29 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
30 | import edu.ucsb.cs156.happiercows.entities.User; | |
31 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
32 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
33 | import edu.ucsb.cs156.happiercows.models.CreateUserCommonsParams; | |
34 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
35 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
36 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
37 | ||
38 | import io.swagger.annotations.Api; | |
39 | import io.swagger.annotations.ApiOperation; | |
40 | import io.swagger.annotations.ApiParam; | |
41 | ||
42 | import org.springframework.http.ResponseEntity; | |
43 | import javax.validation.Valid; | |
44 | import org.springframework.web.bind.annotation.RequestBody; | |
45 | import org.springframework.web.bind.annotation.PutMapping; | |
46 | import org.springframework.web.bind.annotation.PostMapping; | |
47 | import org.springframework.web.bind.annotation.RequestBody; | |
48 | ||
49 | @Api(description = "User Commons") | |
50 | @RequestMapping("/api/usercommons") | |
51 | @RestController | |
52 | public class UserCommonsController extends ApiController { | |
53 | ||
54 | @Autowired | |
55 | private UserCommonsRepository userCommonsRepository; | |
56 | ||
57 | @Autowired | |
58 | private CommonsRepository commonsRepository; | |
59 | ||
60 | @Autowired | |
61 | ObjectMapper mapper; | |
62 | ||
63 | @ApiOperation(value = "Get a specific user commons (admin only)") | |
64 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
65 | @GetMapping("") | |
66 | public UserCommons getUserCommonsById( | |
67 | @ApiParam("userId") @RequestParam Long userId, | |
68 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
69 | ||
70 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
71 | .orElseThrow( | |
72 |
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)); |
73 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
74 | } | |
75 | ||
76 | @ApiOperation(value = "Get a user commons for current user") | |
77 | @PreAuthorize("hasRole('ROLE_USER')") | |
78 | @GetMapping("/forcurrentuser") | |
79 | public UserCommons getUserCommonsById( | |
80 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
81 | ||
82 | User u = getCurrentUser().getUser(); | |
83 | Long userId = u.getId(); | |
84 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
85 | .orElseThrow( | |
86 |
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)); |
87 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
88 | } | |
89 | ||
90 | @ApiOperation(value = "Update a user commons: cow health, number of cows, total wealth") | |
91 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
92 | @PutMapping("/update") | |
93 | public ResponseEntity<String> updateUserCommons( | |
94 | @ApiParam("userId") @RequestParam Long userId, | |
95 | @ApiParam("commonsId") @RequestParam Long commonsId, | |
96 | @ApiParam("request body") @RequestBody CreateUserCommonsParams params) throws IllegalArgumentException, JsonProcessingException { | |
97 | | |
98 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
99 | .orElseThrow( | |
100 |
1
1. lambda$updateUserCommons$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$updateUserCommons$2 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
101 | ||
102 |
1
1. updateUserCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(params.getCowHealth()); |
103 |
1
1. updateUserCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(params.getNumOfCows()); |
104 |
1
1. updateUserCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(params.getTotalWealth()); |
105 | ||
106 | userCommonsRepository.save(userCommons); | |
107 | ||
108 | String body = mapper.writeValueAsString(userCommons); | |
109 | | |
110 |
1
1. updateUserCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::updateUserCommons → KILLED |
return ResponseEntity.ok().body(body); |
111 | } | |
112 | ||
113 | @ApiOperation(value = "Buy a cow, totalWealth updated") | |
114 | @PreAuthorize("hasRole('ROLE_USER')") | |
115 | @PutMapping("/buy") | |
116 | public ResponseEntity<String> putUserCommonsByIdBuy( | |
117 | @ApiParam("commonsId") @RequestParam Long commonsId, | |
118 | @ApiParam("numCows") @RequestParam int numCows) | |
119 | throws NotEnoughMoneyException, JsonProcessingException{ | |
120 | ||
121 | User u = getCurrentUser().getUser(); | |
122 | Long userId = u.getId(); | |
123 | ||
124 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
125 |
1
1. lambda$putUserCommonsByIdBuy$3 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$3 → KILLED |
()->new EntityNotFoundException(Commons.class, commonsId)); |
126 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
127 | .orElseThrow( | |
128 |
1
1. lambda$putUserCommonsByIdBuy$4 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdBuy$4 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
129 | ||
130 |
5
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : changed conditional boundary → KILLED 3. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED 4. putUserCommonsByIdBuy : negated conditional → KILLED 5. putUserCommonsByIdBuy : negated conditional → KILLED |
if((numCows > 0) && (userCommons.getTotalWealth() >= (numCows * commons.getCowPrice()))){ |
131 |
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() - (numCows * commons.getCowPrice())); |
132 |
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); |
133 |
2
1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalCowsBought → KILLED |
userCommons.setTotalCowsBought(userCommons.getTotalCowsBought() + numCows); |
134 | } | |
135 | else{ | |
136 | throw new NotEnoughMoneyException("You need more money!"); | |
137 | } | |
138 | userCommonsRepository.save(userCommons); | |
139 | ||
140 | String body = mapper.writeValueAsString(userCommons); | |
141 |
1
1. putUserCommonsByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
142 | } | |
143 | ||
144 | @ApiOperation(value = "Sell a cow, totalWealth updated") | |
145 | @PreAuthorize("hasRole('ROLE_USER')") | |
146 | @PutMapping("/sell") | |
147 | public ResponseEntity<String> putUserCommonsByIdSell( | |
148 | @ApiParam("commonsId") @RequestParam Long commonsId, | |
149 | @ApiParam("numCows") @RequestParam int numCows) | |
150 | throws NoCowsException, JsonProcessingException { | |
151 | ||
152 | User u = getCurrentUser().getUser(); | |
153 | Long userId = u.getId(); | |
154 | ||
155 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
156 |
1
1. lambda$putUserCommonsByIdSell$5 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$5 → KILLED |
()->new EntityNotFoundException(Commons.class, commonsId)); |
157 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
158 | .orElseThrow( | |
159 |
1
1. lambda$putUserCommonsByIdSell$6 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::lambda$putUserCommonsByIdSell$6 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
160 | ||
161 |
4
1. putUserCommonsByIdSell : changed conditional boundary → KILLED 2. putUserCommonsByIdSell : changed conditional boundary → KILLED 3. putUserCommonsByIdSell : negated conditional → KILLED 4. putUserCommonsByIdSell : negated conditional → KILLED |
if((userCommons.getNumOfCows() >= 1) && (numCows >= 1)){ |
162 | numCows = Math.min(numCows, userCommons.getNumOfCows()); | |
163 |
4
1. putUserCommonsByIdSell : Replaced double division with multiplication → KILLED 2. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 3. putUserCommonsByIdSell : negated conditional → KILLED 4. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(userCommons.getTotalWealth() + (numCows * (commons.isScaleCowSalePrice() ? (commons.getCowPrice() * (userCommons.getCowHealth()/100)) |
164 |
2
1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdSell : Replaced double addition with subtraction → KILLED |
: commons.getCowPrice()))); |
165 |
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); |
166 |
2
1. putUserCommonsByIdSell : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalCowsSold → KILLED |
userCommons.setTotalCowsSold(userCommons.getTotalCowsSold() + numCows); |
167 | } | |
168 | else{ | |
169 | throw new NoCowsException("You have no cows to sell!"); | |
170 | } | |
171 | userCommonsRepository.save(userCommons); | |
172 | ||
173 | String body = mapper.writeValueAsString(userCommons); | |
174 |
1
1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
175 | } | |
176 | ||
177 | @ApiOperation(value = "Get all user commons for a specific commons") | |
178 | @GetMapping("/commons/all") | |
179 | public ResponseEntity<String> getUsersCommonsByCommonsId( | |
180 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
181 | Iterable<UserCommons> uc = userCommonsRepository.findByCommonsId(commonsId); | |
182 | | |
183 | | |
184 | String body = mapper.writeValueAsString(uc); | |
185 |
1
1. getUsersCommonsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED |
return ResponseEntity.ok().body(body); |
186 | } | |
187 | ||
188 | } | |
Mutations | ||
72 |
1.1 |
|
73 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
100 |
1.1 |
|
102 |
1.1 |
|
103 |
1.1 |
|
104 |
1.1 |
|
110 |
1.1 |
|
125 |
1.1 |
|
128 |
1.1 |
|
130 |
1.1 2.2 3.3 4.4 5.5 |
|
131 |
1.1 2.2 3.3 |
|
132 |
1.1 2.2 |
|
133 |
1.1 2.2 |
|
141 |
1.1 |
|
156 |
1.1 |
|
159 |
1.1 |
|
161 |
1.1 2.2 3.3 4.4 |
|
163 |
1.1 2.2 3.3 4.4 |
|
164 |
1.1 2.2 |
|
165 |
1.1 2.2 |
|
166 |
1.1 2.2 |
|
174 |
1.1 |
|
185 |
1.1 |