CourseOverTimeController.java

  1. package edu.ucsb.cs156.courses.controllers;

  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import edu.ucsb.cs156.courses.collections.ConvertedSectionCollection;
  5. import edu.ucsb.cs156.courses.documents.ConvertedSection;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.Parameter;
  8. import java.util.List;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.RestController;

  15. @RestController
  16. @RequestMapping("/api/public/courseovertime")
  17. public class CourseOverTimeController {

  18.   private ObjectMapper mapper = new ObjectMapper();

  19.   @Autowired ConvertedSectionCollection convertedSectionCollection;

  20.   @Operation(summary = "Get a list of courses over time")
  21.   @GetMapping(value = "/search", produces = "application/json")
  22.   public ResponseEntity<String> search(
  23.       @Parameter(
  24.               name = "startQtr",
  25.               description =
  26.                   "starting quarter in yyyyq format, e.g. 20231 for W23, 20232 for S23, etc. (1=Winter, 2=Spring, 3=Summer, 4=Fall)",
  27.               example = "20231",
  28.               required = true)
  29.           @RequestParam
  30.           String startQtr,
  31.       @Parameter(
  32.               name = "endQtr",
  33.               description =
  34.                   "ending quarter in yyyyq format, e.g. 20231 for W23, 20232 for S23, etc. (1=Winter, 2=Spring, 3=Summer, 4=Fall)",
  35.               example = "20231",
  36.               required = true)
  37.           @RequestParam
  38.           String endQtr,
  39.       @Parameter(
  40.               name = "subjectArea",
  41.               description = "simplified area name, e.g. CMPSC for computer science",
  42.               example = "CMPSC",
  43.               required = true)
  44.           @RequestParam
  45.           String subjectArea,
  46.       @Parameter(
  47.               name = "courseNumber",
  48.               description = "the specific course number, e.g. 130A for CS130A",
  49.               example = "130A",
  50.               required = true)
  51.           @RequestParam
  52.           String courseNumber)
  53.       throws JsonProcessingException {
  54.     List<ConvertedSection> courseResults =
  55.         convertedSectionCollection.findByQuarterRangeAndCourseId(
  56.             startQtr, endQtr, makeFormattedCourseId(subjectArea, courseNumber));
  57.     String body = mapper.writeValueAsString(courseResults);
  58.     return ResponseEntity.ok().body(body);
  59.   }

  60.   String makeFormattedCourseId(String subjectArea, String courseNumber) {
  61.     String[] nums = courseNumber.split("[a-zA-Z]+");
  62.     String[] suffs = courseNumber.split("[0-9]+");
  63.     if (suffs.length < 2) { // no suffix
  64.       return String.format("%-8s", subjectArea) // 'CMPSC   '
  65.           + String.format("%3s", nums[0]) // '  8'
  66.       ;
  67.     }
  68.     return String.format("%-8s", subjectArea) // 'CMPSC   '
  69.         + String.format("%3s", nums[0]) // '  8'
  70.         + String.format("%-2s", suffs[1]) // 'A '
  71.     ;
  72.   }
  73. }