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.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
    CommonsRepository commonsRepository;
33
34
    @Autowired
35
    UserCommonsRepository userCommonsRepository;
36
37
    @Autowired
38
    ProfitRepository profitRepository;
39
40
    @Operation(summary = "Get all profits belonging to a user commons as a admin via CommonsID and UserId")
41
    @PreAuthorize("hasRole('ROLE_ADMIN')")
42
    @GetMapping("/all")
43
    public Iterable<Profit> allProfitsByCommonsId(
44
            @Parameter(name="userId") @RequestParam Long userId,
45
            @Parameter(name="commonsId") @RequestParam Long commonsId
46
47
    ) {
48
49
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
50 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));
51
52
        Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons);
53
54 1 1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED
        return profits;
55
    }
56
    @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID")
57
    @PreAuthorize("hasRole('ROLE_USER')")
58
    @GetMapping("/all/commonsid")
59
    public Iterable<Profit> allProfitsByCommonsId(
60
            @Parameter(name="commonsId") @RequestParam Long commonsId
61
    ) {
62
        Long userId = getCurrentUser().getUser().getId();
63
64
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
65 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));
66
67
        Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons);
68
69 1 1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED
        return profits;
70
    }
71
72
    @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID with pagination")
73
    @PreAuthorize("hasRole('ROLE_USER')")
74
    @GetMapping("/paged/commonsid")
75
    public Page<Profit> allProfitsByCommonsIdWithPagination(
76
            @Parameter(name = "commonsId") @RequestParam Long commonsId,
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
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
84 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));
85
86
        Iterable<Profit> iterableProfits = profitRepository.findAllByUserCommons(userCommons);
87
88
        List<Profit> allProfits = new ArrayList<>();
89 1 1. allProfitsByCommonsIdWithPagination : removed call to java/lang/Iterable::forEach → KILLED
        iterableProfits.forEach(allProfits::add);
90
91 1 1. allProfitsByCommonsIdWithPagination : Replaced integer multiplication with division → KILLED
        int start = pageNumber * pageSize;
92 1 1. allProfitsByCommonsIdWithPagination : Replaced integer addition with subtraction → KILLED
        int end = Math.min((start + pageSize), allProfits.size());
93
        List<Profit> paginatedProfits = allProfits.subList(start, end);
94
95
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
96
        Page<Profit> profitsPage = new PageImpl<>(paginatedProfits, pageable, allProfits.size());
97
98 1 1. allProfitsByCommonsIdWithPagination : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsIdWithPagination → KILLED
        return profitsPage;
99
    }
100
}

Mutations

50

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

54

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

65

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

69

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

84

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

89

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

91

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

92

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()]
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