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.BuyPositiveException; | |
23 | ||
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, BuyPositiveException, JsonProcessingException{ | |
79 | ||
80 |
2
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : negated conditional → KILLED |
if(numCows <= 0){ |
81 | throw new BuyPositiveException("The number of cows you buy must be positive"); | |
82 | } | |
83 | ||
84 | User u = getCurrentUser().getUser(); | |
85 | Long userId = u.getId(); | |
86 | ||
87 | Commons commons = commonsRepository.findById(commonsId).orElseThrow( | |
88 |
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)); |
89 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
90 | .orElseThrow( | |
91 |
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)); |
92 | ||
93 |
3
1. putUserCommonsByIdBuy : changed conditional boundary → KILLED 2. putUserCommonsByIdBuy : Replaced double multiplication with division → KILLED 3. putUserCommonsByIdBuy : negated conditional → KILLED |
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 { | |
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 |
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() >= numCows) && (numCows >= 0)){ |
123 |
2
1. putUserCommonsByIdSell : Replaced double multiplication with division → KILLED 2. putUserCommonsByIdSell : Replaced double division with multiplication → KILLED |
double cowValue = commons.getCowPrice() * userCommons.getCowHealth() / 100; |
124 |
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)); |
125 |
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); |
126 |
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); |
127 | } | |
128 | else{ | |
129 | throw new NoCowsException("You do not have enough cows to sell!"); | |
130 | } | |
131 | userCommonsRepository.save(userCommons); | |
132 | ||
133 | String body = mapper.writeValueAsString(userCommons); | |
134 |
1
1. putUserCommonsByIdSell : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::putUserCommonsByIdSell → KILLED |
return ResponseEntity.ok().body(body); |
135 | } | |
136 | ||
137 | | |
138 | ||
139 | @Operation(summary = "Get all user commons for a specific commons") | |
140 | @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')") | |
141 | @GetMapping("/commons/all") | |
142 | public ResponseEntity<String> getUsersCommonsByCommonsId( | |
143 | @Parameter(name="commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
144 | Iterable<UserCommons> uc = userCommonsRepository.findByCommonsId(commonsId); | |
145 | | |
146 | | |
147 | String body = mapper.writeValueAsString(uc); | |
148 |
1
1. getUsersCommonsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/UserCommonsController::getUsersCommonsByCommonsId → KILLED |
return ResponseEntity.ok().body(body); |
149 | } | |
150 | ||
151 | } | |
Mutations | ||
55 |
1.1 |
|
56 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
80 |
1.1 2.2 |
|
88 |
1.1 |
|
91 |
1.1 |
|
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 3.3 4.4 |
|
123 |
1.1 2.2 |
|
124 |
1.1 2.2 3.3 |
|
125 |
1.1 2.2 |
|
126 |
1.1 2.2 |
|
134 |
1.1 |
|
148 |
1.1 |