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 user via CommonsID")
41
    @PreAuthorize("hasRole('ROLE_USER')")
42
    @GetMapping("/all/commonsid")
43
    public Iterable<Profit> allProfitsByCommonsId(
44
            @Parameter(name="commonsId") @RequestParam Long commonsId
45
    ) {
46
        Long userId = getCurrentUser().getUser().getId();
47
48
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
49 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));
50
51
        Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons);
52
53 1 1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED
        return profits;
54
    }
55
56
    @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID with pagination")
57
    @PreAuthorize("hasRole('ROLE_USER')")
58
    @GetMapping("/paged/commonsid")
59
    public Page<Profit> allProfitsByCommonsIdWithPagination(
60
            @Parameter(name = "commonsId") @RequestParam Long commonsId,
61
            @Parameter(name = "pageNumber", description = "Page number, 0 indexed") @RequestParam(defaultValue = "0") int pageNumber,
62
            @Parameter(name = "pageSize", description = "Number of records per page") @RequestParam(defaultValue = "7") int pageSize
63
64
    ) {
65
        Long userId = getCurrentUser().getUser().getId();
66
67
        UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId)
68 1 1. lambda$allProfitsByCommonsIdWithPagination$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsIdWithPagination$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId));
69
70
        Iterable<Profit> iterableProfits = profitRepository.findAllByUserCommons(userCommons);
71
72
        List<Profit> allProfits = new ArrayList<>();
73 1 1. allProfitsByCommonsIdWithPagination : removed call to java/lang/Iterable::forEach → KILLED
        iterableProfits.forEach(allProfits::add);
74
75 1 1. allProfitsByCommonsIdWithPagination : Replaced integer multiplication with division → KILLED
        int start = pageNumber * pageSize;
76 1 1. allProfitsByCommonsIdWithPagination : Replaced integer addition with subtraction → KILLED
        int end = Math.min((start + pageSize), allProfits.size());
77
        List<Profit> paginatedProfits = allProfits.subList(start, end);
78
79
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
80
        Page<Profit> profitsPage = new PageImpl<>(paginatedProfits, pageable, allProfits.size());
81
82 1 1. allProfitsByCommonsIdWithPagination : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsIdWithPagination → KILLED
        return profitsPage;
83
    }
84
}

Mutations

49

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:get_profits_all_commons_nonexistent_using_commons_id()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsId$0 → KILLED

53

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

68

1.1
Location : lambda$allProfitsByCommonsIdWithPagination$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_with_pagination()]
replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$allProfitsByCommonsIdWithPagination$1 → KILLED

73

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/lang/Iterable::forEach → KILLED

75

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

76

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 addition with subtraction → KILLED

82

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 return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsIdWithPagination → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3