| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | import edu.ucsb.cs156.courses.collections.UpdateCollection; | |
| 5 | import edu.ucsb.cs156.courses.documents.Update; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import java.util.Arrays; | |
| 10 | import java.util.List; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.data.domain.PageRequest; | |
| 14 | import org.springframework.data.domain.Sort.Direction; | |
| 15 | import org.springframework.web.bind.annotation.GetMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestParam; | |
| 18 | import org.springframework.web.bind.annotation.RestController; | |
| 19 | ||
| 20 | @Slf4j | |
| 21 | @Tag(name = "Updates", description = "API for course history updates") | |
| 22 | @RequestMapping("/api/updates") | |
| 23 | @RestController | |
| 24 | public class UpdateController extends ApiController { | |
| 25 |   @Autowired UpdateCollection updateCollection; | |
| 26 | ||
| 27 |   @Autowired ObjectMapper mapper; | |
| 28 | ||
| 29 |   @Operation(summary = "Get updates for a subject area and quarter (most recent first)") | |
| 30 |   @GetMapping(value = "", produces = "application/json") | |
| 31 |   public Iterable<Update> getUpdates( | |
| 32 |       @Parameter( | |
| 33 |               name = "subjectArea", | |
| 34 |               description = "Course subject area code (e.g. CMPSC) or ALL for all subject areas", | |
| 35 |               example = "ALL", | |
| 36 |               required = true) | |
| 37 |           @RequestParam | |
| 38 |           String subjectArea, | |
| 39 |       @Parameter( | |
| 40 |               name = "quarter", | |
| 41 |               description = "Quarter in yyyyq format (e.g. 20221) or ALL for all quarters", | |
| 42 |               example = "ALL", | |
| 43 |               required = true) | |
| 44 |           @RequestParam | |
| 45 |           String quarter, | |
| 46 |       @Parameter( | |
| 47 |               name = "page", | |
| 48 |               description = "what page of the data", | |
| 49 |               example = "0", | |
| 50 |               required = true) | |
| 51 |           @RequestParam | |
| 52 |           int page, | |
| 53 |       @Parameter( | |
| 54 |               name = "pageSize", | |
| 55 |               description = "size of each page", | |
| 56 |               example = "5", | |
| 57 |               required = true) | |
| 58 |           @RequestParam | |
| 59 |           int pageSize, | |
| 60 |       @Parameter( | |
| 61 |               name = "sortField", | |
| 62 |               description = "sort field", | |
| 63 |               example = "subjectArea", | |
| 64 |               required = true) | |
| 65 |           @RequestParam | |
| 66 |           String sortField, | |
| 67 |       @Parameter( | |
| 68 |               name = "sortDirection", | |
| 69 |               description = "sort direction", | |
| 70 |               example = "ASC", | |
| 71 |               required = true) | |
| 72 |           @RequestParam | |
| 73 |           String sortDirection) { | |
| 74 |     Iterable<Update> updates = null; | |
| 75 | ||
| 76 |     List<String> allowedSortFields = Arrays.asList("subjectArea", "quarter", "lastUpdate"); | |
| 77 | 
1
1. getUpdates : negated conditional → KILLED | 
    if (!allowedSortFields.contains(sortField)) { | 
| 78 |       throw new IllegalArgumentException( | |
| 79 |           String.format( | |
| 80 |               "%s is not a valid sort field.  Valid values are %s", sortField, allowedSortFields)); | |
| 81 |     } | |
| 82 |     List<String> allowedSortDirections = Arrays.asList("ASC", "DESC"); | |
| 83 | 
1
1. getUpdates : negated conditional → KILLED | 
    if (!allowedSortDirections.contains(sortDirection)) { | 
| 84 |       throw new IllegalArgumentException( | |
| 85 |           String.format( | |
| 86 |               "%s is not a valid sort direction.  Valid values are %s", | |
| 87 |               sortDirection, allowedSortDirections)); | |
| 88 |     } | |
| 89 | ||
| 90 |     Direction sortDirectionObject = Direction.ASC; | |
| 91 | 
1
1. getUpdates : negated conditional → KILLED | 
    if (sortDirection.equals("DESC")) { | 
| 92 |       sortDirectionObject = Direction.DESC; | |
| 93 |     } | |
| 94 | ||
| 95 |     PageRequest pageRequest = PageRequest.of(page, pageSize, sortDirectionObject, sortField); | |
| 96 | ||
| 97 | 
2
1. getUpdates : negated conditional → KILLED 2. getUpdates : negated conditional → KILLED  | 
    if (subjectArea.toUpperCase().equals("ALL") && quarter.toUpperCase().equals("ALL")) { | 
| 98 |       updates = updateCollection.findAll(pageRequest); | |
| 99 | 
1
1. getUpdates : negated conditional → KILLED | 
    } else if (subjectArea.toUpperCase().equals("ALL")) { | 
| 100 |       updates = updateCollection.findByQuarter(quarter, pageRequest); | |
| 101 | 
1
1. getUpdates : negated conditional → KILLED | 
    } else if (quarter.toUpperCase().equals("ALL")) { | 
| 102 |       updates = updateCollection.findBySubjectArea(subjectArea, pageRequest); | |
| 103 |     } else { | |
| 104 |       updates = updateCollection.findBySubjectAreaAndQuarter(subjectArea, quarter, pageRequest); | |
| 105 |     } | |
| 106 |     log.info("updates: {}", updates); | |
| 107 | 
1
1. getUpdates : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UpdateController::getUpdates → KILLED | 
    return updates; | 
| 108 |   } | |
| 109 | } | |
Mutations | ||
| 77 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 91 | 
 
 1.1  | 
|
| 97 | 
 
 1.1 2.2  | 
|
| 99 | 
 
 1.1  | 
|
| 101 | 
 
 1.1  | 
|
| 107 | 
 
 1.1  |