FarmerActivityController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import java.util.List;
4
import java.util.Optional;
5
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.security.access.prepost.PreAuthorize;
8
import org.springframework.web.bind.annotation.GetMapping;
9
import org.springframework.web.bind.annotation.PostMapping;
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.entities.Farmer;
15
import edu.ucsb.cs156.happiercows.entities.FarmerActivity;
16
import edu.ucsb.cs156.happiercows.entities.Game;
17
import edu.ucsb.cs156.happiercows.entities.User;
18
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
19
import edu.ucsb.cs156.happiercows.repositories.FarmerActivityRepository;
20
import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
21
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
22
import edu.ucsb.cs156.happiercows.services.FarmerActivityService;
23
24
import io.swagger.v3.oas.annotations.Operation;
25
import io.swagger.v3.oas.annotations.Parameter;
26
import io.swagger.v3.oas.annotations.tags.Tag;
27
28
@Tag(name = "Farmer Activity")
29
@RequestMapping("/api/farmeractivity")
30
@RestController
31
public class FarmerActivityController extends ApiController {
32
33
  @Autowired
34
  private FarmerActivityRepository farmerActivityRepository;
35
36
  @Autowired
37
  private FarmerRepository farmerRepository;
38
39
  @Autowired
40
  private GameRepository gameRepository;
41
42
  @Autowired
43
  private FarmerActivityService farmerActivityService;
44
45
  @Operation(
46
      summary =
47
          "Record a play-page view for the current user, if their email matches a student "
48
              + "on the roster of the course this game is linked to (a no-op otherwise)")
49
  @PreAuthorize("hasRole('ROLE_USER')")
50
  @PostMapping("/pageview")
51
  public void recordPageView(@Parameter(name = "gameId") @RequestParam Long gameId) {
52
53
    User user = getCurrentUser().getUser();
54
55
    Game game = gameRepository.findById(gameId)
56 1 1. lambda$recordPageView$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$recordPageView$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException("Game", gameId));
57
58
    Optional<Farmer> farmer = farmerRepository.findByGameIdAndUserId(gameId, user.getId());
59 2 1. lambda$recordPageView$1 : removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED
2. recordPageView : removed call to java/util/Optional::ifPresent → KILLED
    farmer.ifPresent(f -> farmerActivityService.recordActivityIfStudentMatch(
60
        user, game, f, FarmerActivity.ACTIVITY_TYPE_PLAY_PAGE_VIEW, f.getNumOfCows()));
61
  }
62
63
  @Operation(summary = "Get a farmer's activity history (admin only), reverse chronological")
64
  @PreAuthorize("hasRole('ROLE_ADMIN')")
65
  @GetMapping("/all")
66
  public List<FarmerActivity> getActivityForFarmer(
67
      @Parameter(name = "userId") @RequestParam Long userId,
68
      @Parameter(name = "gameId") @RequestParam Long gameId) {
69
70
    Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
71
        .orElseThrow(
72 1 1. lambda$getActivityForFarmer$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$getActivityForFarmer$0 → KILLED
            () -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
73
74 1 1. getActivityForFarmer : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::getActivityForFarmer → KILLED
    return farmerActivityRepository.findByFarmerOrderByTimestampDesc(farmer);
75
  }
76
77
  @Operation(
78
      summary =
79
          "Get all farmers' activity history for a game (admin only), reverse chronological")
80
  @PreAuthorize("hasRole('ROLE_ADMIN')")
81
  @GetMapping("/game")
82
  public List<FarmerActivity> getActivityForGame(
83
      @Parameter(name = "gameId") @RequestParam Long gameId) {
84
85
    Game game = gameRepository.findById(gameId)
86 1 1. lambda$getActivityForGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$getActivityForGame$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException("Game", gameId));
87
88 1 1. getActivityForGame : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::getActivityForGame → KILLED
    return farmerActivityRepository.findByFarmer_GameOrderByTimestampDesc(game);
89
  }
90
}

Mutations

56

1.1
Location : lambda$recordPageView$0
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:pageview_returns_404_when_game_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$recordPageView$0 → KILLED

59

1.1
Location : lambda$recordPageView$1
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:pageview_records_activity_when_farmer_exists()]
removed call to edu/ucsb/cs156/happiercows/services/FarmerActivityService::recordActivityIfStudentMatch → KILLED

2.2
Location : recordPageView
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:pageview_records_activity_when_farmer_exists()]
removed call to java/util/Optional::ifPresent → KILLED

72

1.1
Location : lambda$getActivityForFarmer$0
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:admin_get_activity_for_nonexistent_farmer_returns_404()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$getActivityForFarmer$0 → KILLED

74

1.1
Location : getActivityForFarmer
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:admin_can_get_activity_for_a_farmer()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::getActivityForFarmer → KILLED

86

1.1
Location : lambda$getActivityForGame$0
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:admin_get_activity_for_nonexistent_game_returns_404()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::lambda$getActivityForGame$0 → KILLED

88

1.1
Location : getActivityForGame
Killed by : edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.FarmerActivityControllerTests]/[method:admin_can_get_activity_for_a_game()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/FarmerActivityController::getActivityForGame → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1