Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Controller;
- import Controller.wrappers.ApiAboutWrapper;
- import Controller.wrappers.ApiCourseOfferingWrapper;
- import Controller.wrappers.ApiCourseWrapper;
- import Controller.wrappers.ApiDepartmentWrapper;
- import Model.*;
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.*;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- @RestController
- public class CourseCatalogueController {
- private boolean isDataCollected = false;
- private ArrayList<Department> departments;
- private CSVReader reader = new CSVReader();
- private ApiAboutWrapper aboutWrapper;
- private ArrayList<ApiDepartmentWrapper> departmentWrappers = new ArrayList<>();
- /******************************* 1. General *******************************/
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/about")
- public ApiAboutWrapper getAbout() {
- if (!isDataCollected) {
- collectData();
- isDataCollected = true;
- }
- String appName = "CMPT 213 - Course Catalogue";
- String authorName = "Cole Stankov";
- ApiAboutWrapper aboutWrapper = new ApiAboutWrapper(appName, authorName);
- this.aboutWrapper = aboutWrapper;
- return aboutWrapper;
- }
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/dump-model")
- public void dumpModel() {
- if (!isDataCollected) {
- collectData();
- isDataCollected = true;
- }
- ArrayList<CourseCatalogue> courses;
- ArrayList<CourseOffering> courseOffering;
- ArrayList<Section> sections;
- ArrayList<String> componentCodes = new ArrayList<>();
- String instructors;
- for (Department dep : departments) {
- courses = dep.getCourseList();
- for (CourseCatalogue course : courses) {
- courseOffering = course.getCourseOfferings();
- System.out.print(dep.getDepartmentCode() + " " + course.getCatalogNumber() + "\n");
- for (CourseOffering offering : courseOffering) {
- sections = offering.getSupplementarySections();
- instructors = offering.getInstructorsStringafied();
- System.out.println(" " +
- offering.getSemesterCode() + " in " +
- offering.getLocation() + " by " +
- instructors);
- for (Section section : sections) {
- if (!componentCodes.contains(section.getComponentCode())) {
- System.out.println(" Type=" +
- section.getComponentCode() + ", Enrollment=" +
- section.getEnrollmentTotalByCode() + "/" +
- section.getEnrollmentCapacityByCode());
- componentCodes.add(section.getComponentCode());
- }
- }
- componentCodes.clear();
- }
- }
- }
- }
- /******** 2. Access Departments, Courses, Offerings, and Sections *********/
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/departments")
- public ArrayList<ApiDepartmentWrapper> getDepartments() {
- if (!isDataCollected) {
- collectData();
- isDataCollected = true;
- }
- for(Department currentDepartment: departments){
- ApiDepartmentWrapper department = new ApiDepartmentWrapper();
- department.deptId = currentDepartment.getDepartmentID();
- department.name = currentDepartment.getDepartmentCode();
- this.departmentWrappers.add(department);
- }
- return this.departmentWrappers;
- }
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/departments/{departmentID}/courses")
- public ArrayList<ApiCourseWrapper> getDepartmentByID(@PathVariable("departmentID") int departmentID) {
- if (!isDataCollected) {
- collectData();
- isDataCollected = true;
- }
- ArrayList<ApiCourseWrapper> courseWrappers = new ArrayList<>();
- ArrayList<CourseCatalogue> courseCatalogue = getCoursesByDepartID(departmentID);
- for(CourseCatalogue course: courseCatalogue){
- ApiCourseWrapper courseWrapper = new ApiCourseWrapper();
- courseWrapper.catalogNumber = course.getCatalogNumber();
- courseWrapper.courseId = course.getCourseID();
- courseWrappers.add(courseWrapper);
- }
- return courseWrappers;
- } // might have to change ID but it works
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/departments/{departmentID}/courses/{courseID}/offerings")
- public ArrayList<ApiCourseOfferingWrapper> getCourseAllOfferingsByCourseID(
- @PathVariable("departmentID") int departmentID,
- @PathVariable("courseID") int courseID) {
- if (!isDataCollected) {
- collectData();
- isDataCollected = true;
- }
- ArrayList<ApiCourseOfferingWrapper> courseOfferingWrappers = new ArrayList<>();
- ArrayList<CourseCatalogue> courseCatalogue = getCoursesByDepartID(departmentID);
- ArrayList<CourseOffering> courseOfferings = getCourseOfferingsByCourseID(courseCatalogue, courseID);
- for(CourseOffering currentCourseOffering: courseOfferings){
- ApiCourseOfferingWrapper courseOfferingWrapper = new ApiCourseOfferingWrapper();
- courseOfferingWrapper.courseOfferingId = currentCourseOffering.getCourseOfferingID();
- courseOfferingWrapper.location = currentCourseOffering.getLocation();
- courseOfferingWrapper.instructors = currentCourseOffering.getInstructorsStringafied();
- courseOfferingWrapper.semesterCode = currentCourseOffering.getSemesterCode();
- courseOfferingWrapper.term = currentCourseOffering.getSemester();
- courseOfferingWrapper.year = currentCourseOffering.getYear();
- courseOfferingWrappers.add(courseOfferingWrapper);
- }
- return courseOfferingWrappers;
- }
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/departments/{departmentID}/courses/{courseID}/offerings{offeringID}")
- public void getCourseOfferingByCourseID(@PathVariable("departmentID") int departmentID,
- @PathVariable("courseID") int courseID,
- @PathVariable("offeringID") int offeringID) {
- }
- /***************************** 3. Graph Data *******************************/
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/stats/students-per-semester?deptId=5")
- public void getDepartmentCourseStats() {
- }
- /********************** 4. Add New Offering / Section **********************/
- @ResponseStatus(value = HttpStatus.OK)
- @PostMapping("/api/addoffering")
- public void addOffering() {
- }
- /************************ 5. Course Change Watchers ***********************/
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/watchers")
- public void getChangeWatchers() {
- }
- @ResponseStatus(value = HttpStatus.OK)
- @PostMapping("/api/watchers")
- public void createWatcher() {
- }
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("/api/watchers/{watcherID}")
- public void getWatcherByID(@PathVariable("watcherID") int watcherID) {
- }
- @ResponseStatus(value = HttpStatus.OK)
- @DeleteMapping("/api/watchers/{watcherID}")
- public void deleteWatcherByID(@PathVariable("watcherID") int watcherID) {
- }
- /************************** Helper Functions *************************/
- private void collectData() {
- reader.readFile();
- departments = reader.getDepartmentList();
- }
- private ArrayList<CourseCatalogue> getCoursesByDepartID(int departmentID){
- boolean departmentFound = false;
- ArrayList<CourseCatalogue> courseCatalogue = new ArrayList<>();
- for(Department currentDepartment: departments){
- if(currentDepartment.getDepartmentID() == departmentID){
- courseCatalogue = currentDepartment.getCourseList();
- departmentFound = true;
- break;
- }
- }
- if(!departmentFound){
- throw new IndexOutOfBoundsException();
- }
- return courseCatalogue;
- }
- private ArrayList<CourseOffering> getCourseOfferingsByCourseID(ArrayList<CourseCatalogue> courseCatalogues, int courseID){
- boolean departmentFound = false;
- ArrayList<CourseOffering> courseOfferings = new ArrayList<>();
- for(CourseCatalogue currentCourse: courseCatalogues){
- if(currentCourse.getCourseID() == courseID){
- courseOfferings = currentCourse.getCourseOfferings();
- departmentFound = true;
- break;
- }
- }
- if(!departmentFound){
- throw new IndexOutOfBoundsException();
- }
- return courseOfferings;
- }
- /************************** Exception Handlers *************************/
- @ResponseStatus(value = HttpStatus.NOT_FOUND) //404
- @ExceptionHandler(IndexOutOfBoundsException.class)
- public void IndexOutOfBoundsException() {
- //nothing to do here
- }
- @ResponseStatus(value = HttpStatus.BAD_REQUEST) //400
- @ExceptionHandler(IllegalArgumentException.class)
- public void IllegalArgumentException() {
- //nothing to do here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment