| 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 | import java.util.ArrayList; | |
| 14 | import java.util.List; | |
| 15 | ||
| 16 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 17 | import edu.ucsb.cs156.happiercows.repositories.CowLotRepository; | |
| 18 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 19 | import edu.ucsb.cs156.happiercows.entities.CowLot; | |
| 20 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 21 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 22 | import edu.ucsb.cs156.happiercows.entities.CowLot; | |
| 23 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 24 | import edu.ucsb.cs156.happiercows.errors.NoCowsException; | |
| 25 | import edu.ucsb.cs156.happiercows.errors.NotEnoughMoneyException; | |
| 26 | ||
| 27 | import io.swagger.annotations.Api; | |
| 28 | import io.swagger.annotations.ApiOperation; | |
| 29 | import io.swagger.annotations.ApiParam; | |
| 30 | ||
| 31 | import org.springframework.http.ResponseEntity; | |
| 32 | import javax.validation.Valid; | |
| 33 | import org.springframework.web.bind.annotation.RequestBody; | |
| 34 | import org.springframework.web.bind.annotation.PutMapping; | |
| 35 | import org.springframework.web.bind.annotation.PostMapping; | |
| 36 | import org.springframework.web.bind.annotation.RequestBody; | |
| 37 | ||
| 38 | import java.util.Optional; | |
| 39 | ||
| 40 | @Api(description = "Cow Lots") | |
| 41 | @RequestMapping("/api/cowlots") | |
| 42 | @RestController | |
| 43 | public class CowLotController extends ApiController { | |
| 44 | ||
| 45 | @Autowired | |
| 46 | private UserCommonsRepository userCommonsRepository; | |
| 47 | ||
| 48 | @Autowired | |
| 49 | private CowLotRepository cowLotRepository; | |
| 50 | ||
| 51 | @Autowired | |
| 52 | ObjectMapper mapper; | |
| 53 | ||
| 54 | @ApiOperation(value = "Get all cow lots for current user:[[cowHealths...],[numCows...]]") | |
| 55 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 56 | @GetMapping("/forcurrentuser") | |
| 57 | public ResponseEntity<String> getCowLotsById( | |
| 58 | @ApiParam("commonsId") @RequestParam Long commonsId) throws JsonProcessingException { | |
| 59 | | |
| 60 | User u = getCurrentUser().getUser(); | |
| 61 | Long userId = u.getId(); | |
| 62 | UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 63 | .orElseThrow( | |
| 64 |
1
1. lambda$getCowLotsById$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CowLotController::lambda$getCowLotsById$0 → KILLED |
() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); |
| 65 | | |
| 66 | Long userCommonsId = userCommons.getId(); | |
| 67 | ||
| 68 | Iterable<CowLot> cl = cowLotRepository.findAllByUserCommonsId(userCommonsId); | |
| 69 | | |
| 70 | List<List<Object>> array = new ArrayList<>(); | |
| 71 | List<Object> healthList = new ArrayList<>(); | |
| 72 | List<Object> numCowsList = new ArrayList<>(); | |
| 73 | ||
| 74 | for (CowLot cowLot : cl) { | |
| 75 | healthList.add(cowLot.getHealth()); | |
| 76 | numCowsList.add(cowLot.getNumCows()); | |
| 77 | } | |
| 78 | ||
| 79 | array.add(healthList); | |
| 80 | array.add(numCowsList); | |
| 81 | ||
| 82 | String body = mapper.writeValueAsString(array); | |
| 83 |
1
1. getCowLotsById : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CowLotController::getCowLotsById → KILLED |
return ResponseEntity.ok().body(body); |
| 84 | } | |
| 85 | } | |
Mutations | ||
| 64 |
1.1 |
|
| 83 |
1.1 |