Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.cmpt213.as5courseplanner.Controller;
- import ca.cmpt213.as5courseplanner.Model.*;
- import ca.cmpt213.as5courseplanner.Utilities.CSVReader;
- import ca.cmpt213.as5courseplanner.Wrappers.*;
- import org.springframework.http.HttpStatus;
- import org.springframework.web.bind.annotation.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.ArrayList;
- import java.util.List;
- /*ANY COMMANDS UNLESS OTHERWISE STATED RETURN 200
- ANY COMMANDS THAT ACCEPT AN ID IN THE ENDPOINT RETURN 404 IF NOT ID DNE
- COMMANDS THAT EXPECT ARRAYS AS RETURN OBJECT MUST BE SORTED IN SOME FASHION
- */
- @RestController
- public class StudentPlannerController {
- CSVReader newReader = new CSVReader();
- List<Department> allDepartments = newReader.getAllDepartments();
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/about")
- public ApiAboutWrapper getAbout() {
- return new ApiAboutWrapper("Brandon's Awesome App", "Brandon Yip");
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/dump-model")
- public void printModel() {
- List<Course> allCourses;
- List<CourseOffering> allCourseOfferings;
- List<Instructor> allInstructors;
- List<Section> allSections;
- int instructorList_Length = 0;
- int instructorList_Index = 0;
- for (Department currentDepartment : allDepartments) {
- allCourses = currentDepartment.getAllCourses();
- for (Course currentCourse : allCourses) {
- System.out.println(currentDepartment.getDepartmentName() +
- " " + currentCourse.getCatalogueNumber());
- allCourseOfferings = currentCourse.getAllCourseOfferings();
- for (CourseOffering currentCourseOffering : allCourseOfferings) {
- allInstructors = currentCourseOffering.getAllInstructors();
- instructorList_Length = allInstructors.size() - 1;
- System.out.print("\t" + currentCourseOffering.getSemesterCode() +
- " in " + currentCourseOffering.getLocation() + " by ");
- for (Instructor currentInstructor : allInstructors) {
- System.out.print(currentInstructor.getInstructorName());
- if (instructorList_Index < instructorList_Length) {
- System.out.print(", ");
- }
- instructorList_Index++;
- }
- System.out.println();
- instructorList_Index = 0;
- allSections = currentCourseOffering.getAllSections();
- for (Section currentSection : allSections) {
- System.out.println("\t\tType=" + currentSection.getType() +
- ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
- "/" + currentSection.getEnrollmentCapacity());
- }
- }
- }
- }
- }
- // @ResponseStatus(value = HttpStatus.OK) //200
- // @GetMapping("/api/dump-model")
- // public void printModelWithTextWriter() {
- // File textFile = new File("data/output_dump.txt");
- // try {
- // FileWriter textFileWriter = new FileWriter(textFile);
- //
- // List<Course> allCourses;
- // List<CourseOffering> allCourseOfferings;
- // List<Instructor> allInstructors;
- // List<Section> allSections;
- // int instructorList_Length = 0;
- // int instructorList_Index = 0;
- //
- // for (Department currentDepartment : allDepartments) {
- // allCourses = currentDepartment.getAllCourses();
- //
- // for (Course currentCourse : allCourses) {
- // System.out.println(currentDepartment.getDepartmentName() +
- // " " + currentCourse.getCatalogueNumber());
- // textFileWriter.write(currentDepartment.getDepartmentName() +
- // " " + currentCourse.getCatalogueNumber() + "\n");
- // allCourseOfferings = currentCourse.getAllCourseOfferings();
- //
- // for (CourseOffering currentCourseOffering : allCourseOfferings) {
- // allInstructors = currentCourseOffering.getAllInstructors();
- // instructorList_Length = allInstructors.size() - 1;
- // System.out.print("\t" + currentCourseOffering.getSemesterCode() +
- // " in " + currentCourseOffering.getLocation() + " by ");
- // textFileWriter.write("\t" + currentCourseOffering.getSemesterCode() +
- // " in " + currentCourseOffering.getLocation() + " by ");
- //
- // for (Instructor currentInstructor : allInstructors) {
- // System.out.print(currentInstructor.getInstructorName());
- // textFileWriter.write(currentInstructor.getInstructorName());
- // if (instructorList_Index < instructorList_Length) {
- // System.out.print(", ");
- // textFileWriter.write(", ");
- // }
- // instructorList_Index++;
- // }
- //
- // System.out.println();
- // textFileWriter.write("\n");
- // instructorList_Index = 0;
- // allSections = currentCourseOffering.getAllSections();
- //
- // for (Section currentSection : allSections) {
- // System.out.println("\t\tType=" + currentSection.getComponentCode() +
- // ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
- // "/" + currentSection.getEnrollmentCapacity());
- // textFileWriter.write("\t\tType=" + currentSection.getComponentCode() +
- // ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
- // "/" + currentSection.getEnrollmentCapacity());
- // textFileWriter.write("\n");
- // }
- // }
- // }
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // throw new IndexOutOfBoundsException();
- // }
- // }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/departments")
- public List<ApiDepartmentWrapper> getDepartments() {
- List<ApiDepartmentWrapper> allDepartmentsWrapped = new ArrayList<>();
- for (Department currentDepartment : allDepartments) {
- ApiDepartmentWrapper newDepartmentWrapper =
- new ApiDepartmentWrapper(currentDepartment.getDepartmentID(),
- currentDepartment.getDepartmentName());
- allDepartmentsWrapped.add(newDepartmentWrapper);
- }
- return allDepartmentsWrapped;
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/departments/{deptID}/courses")
- public List<ApiCourseWrapper> getCourses(@PathVariable("deptID") int deptID) {
- List<ApiCourseWrapper> allCoursesWrapped = new ArrayList<>();
- for (Department currentDepartment : allDepartments) {
- if (currentDepartment.getDepartmentID() == deptID) {
- for (Course currentCourse : currentDepartment.getAllCourses()) {
- ApiCourseWrapper newCourseWrapper =
- new ApiCourseWrapper(currentCourse.getCourseID(),
- currentCourse.getCatalogueNumber());
- allCoursesWrapped.add(newCourseWrapper);
- }
- return allCoursesWrapped;
- }
- }
- throw new IndexOutOfBoundsException(); //404
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/departments/{deptID}/courses/{courseID}/offerings")
- public List<ApiCourseOfferingWrapper> getCourseOfferings(@PathVariable("deptID") int deptID,
- @PathVariable("courseID") int courseID) {
- List<ApiCourseOfferingWrapper> allCourseOfferingsWrapped = new ArrayList<>();
- for (Department currentDepartment : allDepartments) {
- if (currentDepartment.getDepartmentID() == deptID) {
- for (Course currentCourse : currentDepartment.getAllCourses()) {
- if (currentCourse.getCourseID() == courseID) {
- for (CourseOffering currentCourseOffering : currentCourse.getAllCourseOfferings()) {
- ApiCourseOfferingWrapper newCourseOfferingWrapper =
- new ApiCourseOfferingWrapper(currentCourseOffering.getCourseOfferingID(),
- currentCourseOffering.getLocation(),
- currentCourseOffering.getInstructors(),
- currentCourseOffering.getTerm(),
- currentCourseOffering.getSemesterCode(),
- currentCourseOffering.getYear());
- allCourseOfferingsWrapped.add(newCourseOfferingWrapper);
- }
- }
- return allCourseOfferingsWrapped;
- }
- }
- }
- throw new IndexOutOfBoundsException(); //404
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/departments/{deptID}/courses/{courseID}/offerings/{courseOfferingID}")
- public List<ApiOfferingSectionWrapper> getSections(@PathVariable("deptID") int deptID,
- @PathVariable("courseID") int courseID,
- @PathVariable("courseOfferingID") int courseOfferingID) {
- List<ApiOfferingSectionWrapper> allSectionsWrapped = new ArrayList<>();
- for (Department currentDepartment : allDepartments) {
- if (currentDepartment.getDepartmentID() == deptID) {
- for (Course currentCourse : currentDepartment.getAllCourses()) {
- if (currentCourse.getCourseID() == courseID) {
- for (CourseOffering currentCourseOffering : currentCourse.getAllCourseOfferings()) {
- if (currentCourseOffering.getCourseOfferingID() == courseOfferingID) {
- for (Section currentSection : currentCourseOffering.getAllSections()) {
- ApiOfferingSectionWrapper newSectionWrapper =
- new ApiOfferingSectionWrapper(currentSection.getType(),
- currentSection.getEnrollmentCapacity(),
- currentSection.getEnrollmentTotal());
- allSectionsWrapped.add(newSectionWrapper);
- }
- return allSectionsWrapped;
- }
- }
- }
- }
- }
- }
- throw new IndexOutOfBoundsException(); //404
- }
- @ResponseStatus(value = HttpStatus.OK)
- @GetMapping("api/stats/student-per-semester?deptID={courseID}")
- public void getGraphData(@PathVariable("courseID") int courseID) {
- //NOT VOID; do later
- //return array sorted by semesterCode; chronological order
- throw new IndexOutOfBoundsException();
- }
- @ResponseStatus(value = HttpStatus.CREATED) //201
- @PostMapping("/api/addoffering")
- public void addOffering() {
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/watchers")
- public void getWatchers() {
- //NOT VOID; do later; watcher class probably
- }
- @ResponseStatus(value = HttpStatus.CREATED) //201
- @PostMapping("/api/watchers")
- public void addWatcher() {
- //do later
- }
- @ResponseStatus(value = HttpStatus.OK) //200
- @GetMapping("/api/watchers/{watcherID}")
- public void getWatcher(@PathVariable("watcherID") int watcherID) {
- //do later
- throw new IndexOutOfBoundsException();
- }
- @ResponseStatus(value = HttpStatus.NO_CONTENT) //204
- @DeleteMapping("/api/watchers/{watcherID}")
- public void deleteWatcher(@PathVariable("watcherID") int watcherID) {
- //do later
- throw new IndexOutOfBoundsException(); //404
- }
- @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
Advertisement