| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | |
| 4 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 5 | import org.springframework.web.bind.annotation.GetMapping; | |
| 6 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 7 | import org.springframework.web.bind.annotation.RequestParam; | |
| 8 | import org.springframework.web.bind.annotation.RestController; | |
| 9 | ||
| 10 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 11 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 12 | import edu.ucsb.cs156.happiercows.entities.Herd; | |
| 13 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 14 | import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException; | |
| 15 | import edu.ucsb.cs156.happiercows.errors.NotEnrolledInCourseAssociatedWithGameException; | |
| 16 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 17 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 18 | import edu.ucsb.cs156.happiercows.repositories.HerdRepository; | |
| 19 | import edu.ucsb.cs156.happiercows.services.CourseAccessService; | |
| 20 | ||
| 21 | import io.swagger.v3.oas.annotations.Operation; | |
| 22 | import io.swagger.v3.oas.annotations.Parameter; | |
| 23 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 24 | ||
| 25 | @Tag(name = "Herds") | |
| 26 | @RequestMapping("/api/herds") | |
| 27 | @RestController | |
| 28 | public class HerdController extends ApiController { | |
| 29 | ||
| 30 | @Autowired | |
| 31 | private HerdRepository herdRepository; | |
| 32 | ||
| 33 | @Autowired | |
| 34 | private FarmerRepository farmerRepository; | |
| 35 | ||
| 36 | @Autowired | |
| 37 | private GameRepository gameRepository; | |
| 38 | ||
| 39 | @Autowired | |
| 40 | private CourseAccessService courseAccessService; | |
| 41 | ||
| 42 | @Operation(summary = "Get the herds of a specific farmer in a game (admin only)") | |
| 43 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 44 | @GetMapping("") | |
| 45 | public Iterable<Herd> getHerdsByGameIdAndUserId( | |
| 46 | @Parameter(name = "gameId") @RequestParam Long gameId, | |
| 47 | @Parameter(name = "userId") @RequestParam Long userId) { | |
| 48 | ||
| 49 | farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 50 | .orElseThrow( | |
| 51 |
1
1. lambda$getHerdsByGameIdAndUserId$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/HerdController::lambda$getHerdsByGameIdAndUserId$0 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 52 |
1
1. getHerdsByGameIdAndUserId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/HerdController::getHerdsByGameIdAndUserId → KILLED |
return herdRepository.findByGameIdAndUserId(gameId, userId); |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * The userId is always taken from the logged-in user, never from a request | |
| 57 | * parameter, so a farmer can only ever read their own herds. | |
| 58 | */ | |
| 59 | @Operation(summary = "Get the current user's herds in a game") | |
| 60 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 61 | @GetMapping("/forcurrentuser") | |
| 62 | public Iterable<Herd> getHerdsForCurrentUser( | |
| 63 | @Parameter(name = "gameId") @RequestParam Long gameId) { | |
| 64 | ||
| 65 | User u = getCurrentUser().getUser(); | |
| 66 | Long userId = u.getId(); | |
| 67 | Game game = gameRepository.findById(gameId) | |
| 68 |
1
1. lambda$getHerdsForCurrentUser$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/HerdController::lambda$getHerdsForCurrentUser$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException("Game", gameId)); |
| 69 |
2
1. getHerdsForCurrentUser : negated conditional → KILLED 2. getHerdsForCurrentUser : negated conditional → KILLED |
if (game.getCourseId() != null && !courseAccessService.isEligibleForGame(u, game)) { |
| 70 | throw new NotEnrolledInCourseAssociatedWithGameException(); | |
| 71 | } | |
| 72 | farmerRepository.findByGameIdAndUserId(gameId, userId) | |
| 73 | .orElseThrow( | |
| 74 |
1
1. lambda$getHerdsForCurrentUser$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/HerdController::lambda$getHerdsForCurrentUser$1 → KILLED |
() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId)); |
| 75 |
1
1. getHerdsForCurrentUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/HerdController::getHerdsForCurrentUser → KILLED |
return herdRepository.findByGameIdAndUserId(gameId, userId); |
| 76 | } | |
| 77 | } | |
Mutations | ||
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 68 |
1.1 |
|
| 69 |
1.1 2.2 |
|
| 74 |
1.1 |
|
| 75 |
1.1 |