ProfitsController.java

1
package edu.ucsb.cs156.happiercows.controllers;
2
3
import edu.ucsb.cs156.happiercows.entities.Profit;
4
import edu.ucsb.cs156.happiercows.entities.Farmer;
5
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.happiercows.repositories.GameRepository;
7
import edu.ucsb.cs156.happiercows.repositories.ProfitRepository;
8
import edu.ucsb.cs156.happiercows.repositories.FarmerRepository;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import io.swagger.v3.oas.annotations.Operation;
11
import io.swagger.v3.oas.annotations.Parameter;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.data.domain.Page;
14
import org.springframework.data.domain.PageImpl;
15
import org.springframework.data.domain.PageRequest;
16
import org.springframework.data.domain.Pageable;
17
import org.springframework.security.access.prepost.PreAuthorize;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestParam;
21
import org.springframework.web.bind.annotation.RestController;
22
23
import java.util.ArrayList;
24
import java.util.List;
25
26
@Tag(name = "Profits")
27
@RequestMapping("/api/profits")
28
@RestController
29
public class ProfitsController extends ApiController {
30
31
    @Autowired
32
    GameRepository gameRepository;
33
34
    @Autowired
35
    FarmerRepository farmerRepository;
36
37
    @Autowired
38
    ProfitRepository profitRepository;
39
40
    @Operation(summary = "Get all profits belonging to a user game as a admin via GameID and UserId")
41
    @PreAuthorize("hasRole('ROLE_ADMIN')")
42
    @GetMapping("/all")
43
    public Iterable<Profit> allProfitsByGameId(
44
            @Parameter(name="userId") @RequestParam Long userId,
45
            @Parameter(name="gameId") @RequestParam Long gameId
46
47
    ) {
48
49
        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
50 1 1. lambda$allProfitsByGameId$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByGameId$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
51
52
        Iterable<Profit> profits = profitRepository.findAllByFarmer(farmer);
53
54 1 1. allProfitsByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameId → KILLED
        return profits;
55
    }
56
    @Operation(summary = "Get all profits belonging to a user game as a user via GameID")
57
    @PreAuthorize("hasRole('ROLE_USER')")
58
    @GetMapping("/all/gameid")
59
    public Iterable<Profit> allProfitsByGameId(
60
            @Parameter(name="gameId") @RequestParam Long gameId
61
    ) {
62
        Long userId = getCurrentUser().getUser().getId();
63
64
        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
65 1 1. lambda$allProfitsByGameId$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByGameId$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
66
67
        Iterable<Profit> profits = profitRepository.findAllByFarmer(farmer);
68
69 1 1. allProfitsByGameId : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameId → KILLED
        return profits;
70
    }
71
72
    @Operation(summary = "Get all profits belonging to a user game as a user via GameID with pagination")
73
    @PreAuthorize("hasRole('ROLE_USER')")
74
    @GetMapping("/paged/gameid")
75
    public Page<Profit> allProfitsByGameIdWithPagination(
76
            @Parameter(name = "gameId") @RequestParam Long gameId,
77
            @Parameter(name = "pageNumber", description = "Page number, 0 indexed") @RequestParam(defaultValue = "0") int pageNumber,
78
            @Parameter(name = "pageSize", description = "Number of records per page") @RequestParam(defaultValue = "7") int pageSize
79
80
    ) {
81
        Long userId = getCurrentUser().getUser().getId();
82
83
        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
84 1 1. lambda$allProfitsByGameIdWithPagination$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByGameIdWithPagination$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
85
86 1 1. allProfitsByGameIdWithPagination : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameIdWithPagination → KILLED
        return pagedProfitsForFarmer(farmer, pageNumber, pageSize);
87
    }
88
89
    @Operation(summary = "Get all profits belonging to a user game as an admin via GameID and UserId, with pagination")
90
    @PreAuthorize("hasRole('ROLE_ADMIN')")
91
    @GetMapping("/paged")
92
    public Page<Profit> allProfitsByGameIdAndUserIdWithPagination(
93
            @Parameter(name = "userId") @RequestParam Long userId,
94
            @Parameter(name = "gameId") @RequestParam Long gameId,
95
            @Parameter(name = "pageNumber", description = "Page number, 0 indexed") @RequestParam(defaultValue = "0") int pageNumber,
96
            @Parameter(name = "pageSize", description = "Number of records per page") @RequestParam(defaultValue = "7") int pageSize
97
98
    ) {
99
        Farmer farmer = farmerRepository.findByGameIdAndUserId(gameId, userId)
100 1 1. lambda$allProfitsByGameIdAndUserIdWithPagination$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByGameIdAndUserIdWithPagination$0 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Farmer.class, "gameId", gameId, "userId", userId));
101
102 1 1. allProfitsByGameIdAndUserIdWithPagination : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameIdAndUserIdWithPagination → KILLED
        return pagedProfitsForFarmer(farmer, pageNumber, pageSize);
103
    }
104
105
    private Page<Profit> pagedProfitsForFarmer(Farmer farmer, int pageNumber, int pageSize) {
106
        Iterable<Profit> iterableProfits = profitRepository.findAllByFarmer(farmer);
107
108
        List<Profit> allProfits = new ArrayList<>();
109 1 1. pagedProfitsForFarmer : removed call to java/lang/Iterable::forEach → KILLED
        iterableProfits.forEach(allProfits::add);
110
111 1 1. pagedProfitsForFarmer : Replaced integer multiplication with division → KILLED
        int start = pageNumber * pageSize;
112 1 1. pagedProfitsForFarmer : Replaced integer addition with subtraction → KILLED
        int end = Math.min((start + pageSize), allProfits.size());
113
        List<Profit> paginatedProfits = allProfits.subList(start, end);
114
115
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
116 1 1. pagedProfitsForFarmer : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::pagedProfitsForFarmer → KILLED
        return new PageImpl<>(paginatedProfits, pageable, allProfits.size());
117
    }
118
}

Mutations

50

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

54

1.1
Location : allProfitsByGameId
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameId → KILLED

65

1.1
Location : lambda$allProfitsByGameId$1
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:get_profits_all_game_nonexistent_using_game_id()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByGameId$1 → KILLED

69

1.1
Location : allProfitsByGameId
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:get_profits_all_game_using_game_id()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameId → KILLED

84

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

86

1.1
Location : allProfitsByGameIdWithPagination
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:get_profits_all_game_using_game_id_with_pagination_2()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameIdWithPagination → KILLED

100

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

102

1.1
Location : allProfitsByGameIdAndUserIdWithPagination
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id_with_pagination_2()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByGameIdAndUserIdWithPagination → KILLED

109

1.1
Location : pagedProfitsForFarmer
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id_with_pagination_2()]
removed call to java/lang/Iterable::forEach → KILLED

111

1.1
Location : pagedProfitsForFarmer
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id_with_pagination_2()]
Replaced integer multiplication with division → KILLED

112

1.1
Location : pagedProfitsForFarmer
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id_with_pagination_2()]
Replaced integer addition with subtraction → KILLED

116

1.1
Location : pagedProfitsForFarmer
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:admin_get_profits_all_game_using_game_id_with_pagination_2()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::pagedProfitsForFarmer → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1