| 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 lombok.extern.slf4j.Slf4j; | |
| 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 | ||
| 18 | import java.util.List; | |
| 19 | import java.util.ArrayList; | |
| 20 | ||
| 21 | import org.springframework.beans.factory.annotation.Autowired; | |
| 22 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 23 | import org.springframework.web.bind.annotation.GetMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 25 | import org.springframework.web.bind.annotation.RequestParam; | |
| 26 | import org.springframework.web.bind.annotation.RestController; | |
| 27 | ||
| 28 | ||
| 29 | ||
| 30 | @Tag(name = "Profits") | |
| 31 | @RequestMapping("/api/profits") | |
| 32 | @RestController | |
| 33 | @Slf4j | |
| 34 | ||
| 35 | public class ProfitsController extends ApiController { | |
| 36 | ||
| 37 |     @Autowired | |
| 38 |     CommonsRepository commonsRepository; | |
| 39 | ||
| 40 |     @Autowired | |
| 41 |     UserCommonsRepository userCommonsRepository; | |
| 42 | ||
| 43 |     @Autowired | |
| 44 |     ProfitRepository profitRepository; | |
| 45 | ||
| 46 |     @Operation(summary = "Get all profits belonging to a user commons as a user via CommonsID") | |
| 47 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 48 |     @GetMapping("/all/commonsid") | |
| 49 |     public Iterable<Profit> allProfitsByCommonsId( | |
| 50 |             @Parameter(name="commonsId") @RequestParam Long commonsId | |
| 51 |     ) { | |
| 52 |         Long userId = getCurrentUser().getUser().getId(); | |
| 53 | ||
| 54 |         UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 55 | 
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)); | 
| 56 | ||
| 57 |         Iterable<Profit> profits = profitRepository.findAllByUserCommons(userCommons); | |
| 58 | ||
| 59 | 
1
1. allProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::allProfitsByCommonsId → KILLED | 
        return profits; | 
| 60 |     } | |
| 61 | ||
| 62 |     @Operation(summary = "Get paged profits belonging to a user commons as a user via CommonsID") | |
| 63 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 64 |     @GetMapping("/paged/commonsid") | |
| 65 |     public Page<Profit> pagedProfitsByCommonsId( | |
| 66 |         @Parameter(name = "commonsId") @RequestParam Long commonsId, | |
| 67 |         @Parameter(name = "page") @RequestParam(defaultValue = "0") int page, | |
| 68 |         @Parameter(name = "size") @RequestParam(defaultValue = "5") int size | |
| 69 |     ){ | |
| 70 |         Long userId = getCurrentUser().getUser().getId(); | |
| 71 | ||
| 72 |         UserCommons userCommons = userCommonsRepository.findByCommonsIdAndUserId(commonsId, userId) | |
| 73 | 
1
1. lambda$pagedProfitsByCommonsId$1 : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::lambda$pagedProfitsByCommonsId$1 → KILLED | 
            .orElseThrow(() -> new EntityNotFoundException(UserCommons.class, "commonsId", commonsId, "userId", userId)); | 
| 74 | ||
| 75 |         Iterable<Profit> allProfitsIterable = profitRepository.findAllByUserCommons(userCommons); | |
| 76 | ||
| 77 |         List<Profit> profitsList = new ArrayList<>(); | |
| 78 | 
1
1. pagedProfitsByCommonsId : removed call to java/lang/Iterable::forEach → KILLED | 
        allProfitsIterable.forEach(profitsList::add); | 
| 79 | ||
| 80 | 
1
1. pagedProfitsByCommonsId : Replaced integer multiplication with division → KILLED | 
        int fromIndex = page * size; | 
| 81 | 
1
1. pagedProfitsByCommonsId : Replaced integer addition with subtraction → KILLED | 
        int toIndex = Math.min(fromIndex + size, profitsList.size()); | 
| 82 | ||
| 83 |         List<Profit> pagedProfits = profitsList.subList(fromIndex, toIndex); | |
| 84 | ||
| 85 | 
1
1. pagedProfitsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ProfitsController::pagedProfitsByCommonsId → KILLED | 
        return new PageImpl<>(pagedProfits, PageRequest.of(page, size), profitsList.size()); | 
| 86 |     } | |
| 87 | } | |
Mutations | ||
| 55 | 
 
 1.1  | 
|
| 59 | 
 
 1.1  | 
|
| 73 | 
 
 1.1  | 
|
| 78 | 
 
 1.1  | 
|
| 80 | 
 
 1.1  | 
|
| 81 | 
 
 1.1  | 
|
| 85 | 
 
 1.1  |