| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.courses.entities.RateLimitedIP; | |
| 4 | import edu.ucsb.cs156.courses.repositories.RateLimitedIPRepository; | |
| 5 | import io.swagger.v3.oas.annotations.Operation; | |
| 6 | import io.swagger.v3.oas.annotations.Parameter; | |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | import java.util.Arrays; | |
| 9 | import java.util.List; | |
| 10 | import lombok.extern.slf4j.Slf4j; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.data.domain.Page; | |
| 13 | import org.springframework.data.domain.PageRequest; | |
| 14 | import org.springframework.data.domain.Sort.Direction; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.web.bind.annotation.GetMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 18 | import org.springframework.web.bind.annotation.RequestParam; | |
| 19 | import org.springframework.web.bind.annotation.RestController; | |
| 20 | ||
| 21 | @Tag(name = "Rate Limited IPs") | |
| 22 | @RequestMapping("/api/admin/rate-limited-ips") | |
| 23 | @RestController | |
| 24 | @Slf4j | |
| 25 | public class RateLimitedIPsController extends ApiController { | |
| 26 | ||
| 27 | @Autowired private RateLimitedIPRepository rateLimitedIPRepository; | |
| 28 | ||
| 29 | @Operation(summary = "Get a paginated list of rate-limited IP addresses") | |
| 30 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 31 | @GetMapping(value = "", produces = "application/json") | |
| 32 | public Page<RateLimitedIP> getRateLimitedIPs( | |
| 33 | @Parameter( | |
| 34 | name = "page", | |
| 35 | description = "what page of the data", | |
| 36 | example = "0", | |
| 37 | required = true) | |
| 38 | @RequestParam | |
| 39 | int page, | |
| 40 | @Parameter( | |
| 41 | name = "pageSize", | |
| 42 | description = "size of each page", | |
| 43 | example = "10", | |
| 44 | required = true) | |
| 45 | @RequestParam | |
| 46 | int pageSize, | |
| 47 | @Parameter( | |
| 48 | name = "sortField", | |
| 49 | description = "sort field (requestCount or lastRequestAt)", | |
| 50 | example = "requestCount", | |
| 51 | required = false) | |
| 52 | @RequestParam(defaultValue = "requestCount") | |
| 53 | String sortField, | |
| 54 | @Parameter( | |
| 55 | name = "sortDirection", | |
| 56 | description = "sort direction (ASC or DESC)", | |
| 57 | example = "DESC", | |
| 58 | required = false) | |
| 59 | @RequestParam(defaultValue = "DESC") | |
| 60 | String sortDirection) { | |
| 61 | ||
| 62 | List<String> allowedSortFields = Arrays.asList("requestCount", "lastRequestAt"); | |
| 63 |
1
1. getRateLimitedIPs : negated conditional → KILLED |
if (!allowedSortFields.contains(sortField)) { |
| 64 | throw new IllegalArgumentException( | |
| 65 | String.format( | |
| 66 | "%s is not a valid sort field. Valid values are %s", sortField, allowedSortFields)); | |
| 67 | } | |
| 68 | ||
| 69 | List<String> allowedSortDirections = Arrays.asList("ASC", "DESC"); | |
| 70 |
1
1. getRateLimitedIPs : negated conditional → KILLED |
if (!allowedSortDirections.contains(sortDirection)) { |
| 71 | throw new IllegalArgumentException( | |
| 72 | String.format( | |
| 73 | "%s is not a valid sort direction. Valid values are %s", | |
| 74 | sortDirection, allowedSortDirections)); | |
| 75 | } | |
| 76 | ||
| 77 |
1
1. getRateLimitedIPs : negated conditional → KILLED |
Direction sortDirectionObject = sortDirection.equals("ASC") ? Direction.ASC : Direction.DESC; |
| 78 | ||
| 79 | PageRequest pageRequest = PageRequest.of(page, pageSize, sortDirectionObject, sortField); | |
| 80 |
1
1. getRateLimitedIPs : replaced return value with null for edu/ucsb/cs156/courses/controllers/RateLimitedIPsController::getRateLimitedIPs → KILLED |
return rateLimitedIPRepository.findAll(pageRequest); |
| 81 | } | |
| 82 | } | |
Mutations | ||
| 63 |
1.1 |
|
| 70 |
1.1 |
|
| 77 |
1.1 |
|
| 80 |
1.1 |