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.repositories.CowLotRepository; | |
17 | import edu.ucsb.cs156.happiercows.entities.User; | |
18 | import edu.ucsb.cs156.happiercows.entities.CowLot; | |
19 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
20 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
21 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
22 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
23 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
24 | ||
25 | import io.swagger.annotations.Api; | |
26 | import io.swagger.annotations.ApiOperation; | |
27 | import io.swagger.annotations.ApiParam; | |
28 | ||
29 | import org.springframework.http.ResponseEntity; | |
30 | import javax.validation.Valid; | |
31 | import org.springframework.web.bind.annotation.RequestBody; | |
32 | import org.springframework.web.bind.annotation.PutMapping; | |
33 | import org.springframework.web.bind.annotation.PostMapping; | |
34 | import org.springframework.web.bind.annotation.RequestBody; | |
35 | ||
36 | import java.util.Optional; | |
37 | ||
38 | @Api(description = "User Commons") | |
39 | @RequestMapping("/api/usercommons") | |
40 | @RestController | |
41 | public class UserCommonsController extends ApiController { | |
42 | ||
43 | @Autowired | |
44 | private UserCommonsRepository userCommonsRepository; | |
45 | ||
46 | @Autowired | |
47 | private CommonsRepository commonsRepository; | |
48 | ||
49 | @Autowired | |
50 | private CowLotRepository cowLotRepository; | |
51 | ||
52 | @Autowired | |
53 | ObjectMapper mapper; | |
54 | ||
55 | @ApiOperation(value = "Get a specific user commons (admin only)") | |
56 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
57 | @GetMapping("") | |
58 | public UserCommons getUserCommonsById( | |
59 | @ApiParam("userId") @RequestParam Long userId, | |
60 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
61 | ||
62 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
63 | .orElseThrow( | |
64 |
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)); |
65 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
66 | } | |
67 | ||
68 | @ApiOperation(value = "Get a user commons for current user") | |
69 | @PreAuthorize("hasRole('ROLE_USER')") | |
70 | @GetMapping("/forcurrentuser") | |
71 | public UserCommons getUserCommonsById( | |
72 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
73 | ||
74 | User u = getCurrentUser().getUser(); | |
75 | Long userId = u.getId(); | |
76 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
77 | .orElseThrow( | |
78 |
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)); |
79 |
1
1. getUserCommonsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUserCommonsById → KILLED |
return userCommons; |
80 | } | |
81 | ||
82 | @ApiOperation(value = "Buy a cow, totalWealth updated") | |
83 | @PreAuthorize("hasRole('ROLE_USER')") | |
84 | @PutMapping("/buy") | |
85 | public ResponseEntity<String> putUserCommonsByIdBuy( | |
86 | @ApiParam("commonsId") @RequestParam Long commonsId) throws NotEnoughMoneyException, JsonProcessingException{ | |
87 | ||
88 | User u = getCurrentUser().getUser(); | |
89 | Long userId = u.getId(); | |
90 | ||
91 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
92 |
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)); |
93 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
94 | .orElseThrow( | |
95 |
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)); |
96 | ||
97 |
2
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : negated conditional → KILLED |
if(userCommons.getTotalWealth() >= commons.getCowPrice() ){ |
98 |
2
1. putUserCommonsByIdBuy : Replaced double subtraction with addition → KILLED 2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(userCommons.getTotalWealth() - commons.getCowPrice()); |
99 |
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() + 1); |
100 | Optional<CowLot> existingCowLotOptional = cowLotRepository.findByUserCommonsIdAndHealth( | |
101 | userCommons.getId(), | |
102 | 100d); | |
103 |
1
1. putUserCommonsByIdBuy : negated conditional → KILLED |
if(existingCowLotOptional.isPresent()){ |
104 | CowLot existingCowLot = existingCowLotOptional.get(); | |
105 |
2
1. putUserCommonsByIdBuy : Replaced integer addition with subtraction → KILLED 2. putUserCommonsByIdBuy : removed call to edu/ucsb/cs156/happiercows/entities/CowLot::setNumCows → KILLED |
existingCowLot.setNumCows(existingCowLot.getNumCows()+1); |
106 | cowLotRepository.save(existingCowLot); | |
107 | } else { | |
108 | CowLot lot = CowLot.builder() | |
109 | .userCommonsId(userCommons.getId()) | |
110 | .numCows(1) | |
111 | .health(100d) | |
112 | .build(); | |
113 | cowLotRepository.save(lot); | |
114 | } | |
115 | } | |
116 | else{ | |
117 | throw new NotEnoughMoneyException("You need more money!"); | |
118 | } | |
119 | userCommonsRepository.save(userCommons); | |
120 | ||
121 | String body = mapper.writeValueAsString(userCommons); | |
122 |
1
1. putUserCommonsByIdBuy : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdBuy → KILLED |
return ResponseEntity.ok().body(body); |
123 | } | |
124 | ||
125 | @ApiOperation(value = "Sell a cow, totalWealth updated") | |
126 | @PreAuthorize("hasRole('ROLE_USER')") | |
127 | @PutMapping("/sell") | |
128 | public ResponseEntity<String> putUserCommonsByIdSell( | |
129 | @ApiParam("commonsId") @RequestParam Long commonsId) throws NoCowsException, JsonProcessingException { | |
130 | User u = getCurrentUser().getUser(); | |
131 | Long userId = u.getId(); | |
132 | ||
133 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
134 |
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)); |
135 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
136 | .orElseThrow( | |
137 |
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)); |
138 | ||
139 | ||
140 |
2
1. putUserCommonsByIdSell : changed conditional boundary → KILLED 2. putUserCommonsByIdSell : negated conditional → KILLED |
if(userCommons.getNumOfCows() >= 1 ){ |
141 | CowLot lot = cowLotRepository.findTopByUserCommonsIdOrderByHealthDesc(userCommons.getId()); | |
142 |
2
1. putUserCommonsByIdSell : Replaced integer subtraction with addition → KILLED 2. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/CowLot::setNumCows → KILLED |
lot.setNumCows(lot.getNumCows() - 1); |
143 |
1
1. putUserCommonsByIdSell : negated conditional → KILLED |
if(lot.getNumCows() == 0){ |
144 |
1
1. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/repositories/CowLotRepository::delete → KILLED |
cowLotRepository.delete(lot); |
145 | } else { | |
146 | cowLotRepository.save(lot); | |
147 | } | |
148 |
4
1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdSell : Replaced double division with multiplication → KILLED 3. putUserCommonsByIdSell : Replaced double addition with subtraction → KILLED 4. putUserCommonsByIdSell : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setTotalWealth → KILLED |
userCommons.setTotalWealth(userCommons.getTotalWealth() + commons.getCowPrice()*lot.getHealth()/100); |
149 |
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() - 1); |
150 | } | |
151 | else{ | |
152 | throw new NoCowsException("You have no cows to sell!"); | |
153 | } | |
154 | userCommonsRepository.save(userCommons); | |
155 | ||
156 | String body = mapper.writeValueAsString(userCommons); | |
157 |
1
1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
158 | } | |
159 | ||
160 | @ApiOperation(value = "Get all user commons for a specific commons") | |
161 | @GetMapping("/commons/all") | |
162 | public ResponseEntity<String> getUsersCommonsByCommonsId( | |
163 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
164 | Iterable<UserCommons> uc = userCommonsRepository.findByCommonsId(commonsId); | |
165 | | |
166 | | |
167 | String body = mapper.writeValueAsString(uc); | |
168 |
1
1. getUsersCommonsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED |
return ResponseEntity.ok().body(body); |
169 | } | |
170 | ||
171 | } | |
Mutations | ||
64 |
1.1 |
|
65 |
1.1 |
|
78 |
1.1 |
|
79 |
1.1 |
|
92 |
1.1 |
|
95 |
1.1 |
|
97 |
1.1 2.2 |
|
98 |
1.1 2.2 |
|
99 |
1.1 2.2 |
|
103 |
1.1 |
|
105 |
1.1 2.2 |
|
122 |
1.1 |
|
134 |
1.1 |
|
137 |
1.1 |
|
140 |
1.1 2.2 |
|
142 |
1.1 2.2 |
|
143 |
1.1 |
|
144 |
1.1 |
|
148 |
1.1 2.2 3.3 4.4 |
|
149 |
1.1 2.2 |
|
157 |
1.1 |
|
168 |
1.1 |