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.UserCommons;
5
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
7
import edu.ucsb.cs156.happiercows.repositories.ProfitRepository;
8
import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
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.Sort;
15
import org.springframework.data.domain.PageImpl;
16
import org.springframework.data.domain.PageRequest;
17
import org.springframework.data.domain.Pageable;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.web.bind.annotation.GetMapping;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
22
import org.springframework.web.bind.annotation.RestController;
23
24
import java.util.ArrayList;
25
import java.util.List;
26
27
import java.util.Collections;
28
import java.util.Comparator;
29
30
@Tag(name = "Profits")
31
@RequestMapping("/api/profits")
32
@RestController
33
public class ProfitsController extends ApiController {
34
35
    @Autowired
36
    CommonsRepository commonsRepository;
37
38
    @Autowired
39
    UserCommonsRepository userCommonsRepository;
40
41
    @Autowired
42
    ProfitRepository profitRepository;
43
44
    @Operation(summary = "Get all profits belonging to a user commons as a admin via CommonsID and UserId")
45
    @PreAuthorize("hasRole('ROLE_ADMIN')")
46
    @GetMapping("/all")
47
    public Iterable<Profit> allProfitsByCommonsId(
48
            @Parameter(name="userId") @RequestParam Long userId,
49
            @Parameter(name="commonsId") @RequestParam Long commonsId
50
51
    ) {
52
53
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
54 1 1. lambda$allProfitsByCommonsId$0 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsId$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
55
56
        Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons);
57
58 1 1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED
        return profits;
59
    }
60
    @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID")
61
    @PreAuthorize("hasRole('ROLE_USER')")
62
    @GetMapping("/all/commonsid")
63
    public Iterable<Profit> allProfitsByCommonsId(
64
            @Parameter(name="commonsId") @RequestParam Long commonsId
65
    ) {
66
        Long userId = getCurrentUser().getUser().getId();
67
68
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
69 1 1. lambda$allProfitsByCommonsId$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsId$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
70
71
        Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons);
72
73 1 1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED
        return profits;
74
    }
75
76
    @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID with pagination")
77
    @PreAuthorize("hasRole('ROLE_USER')")
78
    @GetMapping("/paged/commonsid")
79
    public Page<Profit> allProfitsByCommonsIdWithPagination(
80
            @Parameter(name = "commonsId") @RequestParam Long commonsId,
81
            @Parameter(name = "pageNumber", description = "Page number, 0 indexed") @RequestParam(defaultValue = "0") int pageNumber,
82
            @Parameter(name = "pageSize", description = "Number of records per page") @RequestParam(defaultValue = "7") int pageSize
83
84
    ) {
85
        Long userId = getCurrentUser().getUser().getId();
86
87
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
88 1 1. lambda$allProfitsByCommonsIdWithPagination$2 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsIdWithPagination$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
89
90
        Iterable<Profit> iterableProfits = profitRepository.findAllByUserCommons(userCommons);
91
92
        List<Profit> allProfits = new ArrayList<>();
93 1 1. allProfitsByCommonsIdWithPagination : removed call to java/lang/Iterable::forEach → KILLED
        iterableProfits.forEach(allProfits::add);
94
95 1 1. allProfitsByCommonsIdWithPagination : Replaced integer multiplication with division → KILLED
        int start = pageNumber * pageSize;
96 1 1. allProfitsByCommonsIdWithPagination : Replaced integer addition with subtraction → KILLED
        int end = Math.min((start + pageSize), allProfits.size());
97
98 1 1. allProfitsByCommonsIdWithPagination : removed call to java/util/List::sort → KILLED
        allProfits.sort(Comparator.comparing(Profit::getTimestamp).reversed());
99
100
        List<Profit> paginatedProfits = allProfits.subList(start, end);
101
102
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
103
        Page<Profit> profitsPage = new PageImpl<>(paginatedProfits, pageable, allProfits.size());
104
105 1 1. allProfitsByCommonsIdWithPagination : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsIdWithPagination → KILLED
        return profitsPage;
106
    }
107
}

Mutations

54

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

58

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

69

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

73

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

88

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

93

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

95

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

96

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

98

1.1
Location : allProfitsByCommonsIdWithPagination
Killed by : edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.controllers.ProfitsControllerTests]/[method:get_profits_all_commons_using_commons_id_with_pagination_2()]
removed call to java/util/List::sort → KILLED

105

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

Active mutators

Tests examined


Report generated by PIT 1.7.3