Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.46 KB | None | 0 0
  1. package ca.cmpt213.as5courseplanner.Controller;
  2.  
  3. import ca.cmpt213.as5courseplanner.Model.*;
  4. import ca.cmpt213.as5courseplanner.Utilities.CSVReader;
  5. import ca.cmpt213.as5courseplanner.Wrappers.*;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.web.bind.annotation.*;
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileWriter;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. /*ANY COMMANDS UNLESS OTHERWISE STATED RETURN 200
  17.   ANY COMMANDS THAT ACCEPT AN ID IN THE ENDPOINT RETURN 404 IF NOT ID DNE
  18.  
  19.   COMMANDS THAT EXPECT ARRAYS AS RETURN OBJECT MUST BE SORTED IN SOME FASHION
  20.  
  21.  */
  22.  
  23. @RestController
  24. public class StudentPlannerController {
  25.     CSVReader newReader = new CSVReader();
  26.     List<Department> allDepartments = newReader.getAllDepartments();
  27.  
  28.     @ResponseStatus(value = HttpStatus.OK) //200
  29.     @GetMapping("/api/about")
  30.     public ApiAboutWrapper getAbout() {
  31.         return new ApiAboutWrapper("Brandon's Awesome App", "Brandon Yip");
  32.     }
  33.  
  34.     @ResponseStatus(value = HttpStatus.OK) //200
  35.     @GetMapping("/api/dump-model")
  36.     public void printModel() {
  37.  
  38.         List<Course> allCourses;
  39.         List<CourseOffering> allCourseOfferings;
  40.         List<Instructor> allInstructors;
  41.         List<Section> allSections;
  42.         int instructorList_Length = 0;
  43.         int instructorList_Index = 0;
  44.  
  45.         for (Department currentDepartment : allDepartments) {
  46.             allCourses = currentDepartment.getAllCourses();
  47.  
  48.             for (Course currentCourse : allCourses) {
  49.                 System.out.println(currentDepartment.getDepartmentName() +
  50.                         " " + currentCourse.getCatalogueNumber());
  51.                 allCourseOfferings = currentCourse.getAllCourseOfferings();
  52.  
  53.                 for (CourseOffering currentCourseOffering : allCourseOfferings) {
  54.                     allInstructors = currentCourseOffering.getAllInstructors();
  55.                     instructorList_Length = allInstructors.size() - 1;
  56.                     System.out.print("\t" + currentCourseOffering.getSemesterCode() +
  57.                             " in " + currentCourseOffering.getLocation() + " by ");
  58.  
  59.                     for (Instructor currentInstructor : allInstructors) {
  60.                         System.out.print(currentInstructor.getInstructorName());
  61.                         if (instructorList_Index < instructorList_Length) {
  62.                             System.out.print(", ");
  63.                         }
  64.                         instructorList_Index++;
  65.                     }
  66.  
  67.                     System.out.println();
  68.                     instructorList_Index = 0;
  69.                     allSections = currentCourseOffering.getAllSections();
  70.  
  71.                     for (Section currentSection : allSections) {
  72.                         System.out.println("\t\tType=" + currentSection.getType() +
  73.                                 ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
  74.                                 "/" + currentSection.getEnrollmentCapacity());
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81. //    @ResponseStatus(value = HttpStatus.OK) //200
  82. //    @GetMapping("/api/dump-model")
  83. //    public void printModelWithTextWriter() {
  84. //        File textFile = new File("data/output_dump.txt");
  85. //        try {
  86. //            FileWriter textFileWriter = new FileWriter(textFile);
  87. //
  88. //            List<Course> allCourses;
  89. //            List<CourseOffering> allCourseOfferings;
  90. //            List<Instructor> allInstructors;
  91. //            List<Section> allSections;
  92. //            int instructorList_Length = 0;
  93. //            int instructorList_Index = 0;
  94. //
  95. //            for (Department currentDepartment : allDepartments) {
  96. //                allCourses = currentDepartment.getAllCourses();
  97. //
  98. //                for (Course currentCourse : allCourses) {
  99. //                    System.out.println(currentDepartment.getDepartmentName() +
  100. //                            " " + currentCourse.getCatalogueNumber());
  101. //                    textFileWriter.write(currentDepartment.getDepartmentName() +
  102. //                            " " + currentCourse.getCatalogueNumber() + "\n");
  103. //                    allCourseOfferings = currentCourse.getAllCourseOfferings();
  104. //
  105. //                    for (CourseOffering currentCourseOffering : allCourseOfferings) {
  106. //                        allInstructors = currentCourseOffering.getAllInstructors();
  107. //                        instructorList_Length = allInstructors.size() - 1;
  108. //                        System.out.print("\t" + currentCourseOffering.getSemesterCode() +
  109. //                                " in " + currentCourseOffering.getLocation() + " by ");
  110. //                        textFileWriter.write("\t" + currentCourseOffering.getSemesterCode() +
  111. //                                " in " + currentCourseOffering.getLocation() + " by ");
  112. //
  113. //                        for (Instructor currentInstructor : allInstructors) {
  114. //                            System.out.print(currentInstructor.getInstructorName());
  115. //                            textFileWriter.write(currentInstructor.getInstructorName());
  116. //                            if (instructorList_Index < instructorList_Length) {
  117. //                                System.out.print(", ");
  118. //                                textFileWriter.write(", ");
  119. //                            }
  120. //                            instructorList_Index++;
  121. //                        }
  122. //
  123. //                        System.out.println();
  124. //                        textFileWriter.write("\n");
  125. //                        instructorList_Index = 0;
  126. //                        allSections = currentCourseOffering.getAllSections();
  127. //
  128. //                        for (Section currentSection : allSections) {
  129. //                            System.out.println("\t\tType=" + currentSection.getComponentCode() +
  130. //                                    ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
  131. //                                    "/" + currentSection.getEnrollmentCapacity());
  132. //                            textFileWriter.write("\t\tType=" + currentSection.getComponentCode() +
  133. //                                    ", " + "Enrollment=" + currentSection.getEnrollmentTotal() +
  134. //                                    "/" + currentSection.getEnrollmentCapacity());
  135. //                            textFileWriter.write("\n");
  136. //                        }
  137. //                    }
  138. //                }
  139. //            }
  140. //        } catch (Exception e) {
  141. //            e.printStackTrace();
  142. //            throw new IndexOutOfBoundsException();
  143. //        }
  144. //    }
  145.  
  146.     @ResponseStatus(value = HttpStatus.OK) //200
  147.     @GetMapping("/api/departments")
  148.     public List<ApiDepartmentWrapper> getDepartments() {
  149.         List<ApiDepartmentWrapper> allDepartmentsWrapped = new ArrayList<>();
  150.         for (Department currentDepartment : allDepartments) {
  151.             ApiDepartmentWrapper newDepartmentWrapper =
  152.                     new ApiDepartmentWrapper(currentDepartment.getDepartmentID(),
  153.                                             currentDepartment.getDepartmentName());
  154.             allDepartmentsWrapped.add(newDepartmentWrapper);
  155.         }
  156.         return allDepartmentsWrapped;
  157.     }
  158.  
  159.     @ResponseStatus(value = HttpStatus.OK) //200
  160.     @GetMapping("/api/departments/{deptID}/courses")
  161.     public List<ApiCourseWrapper> getCourses(@PathVariable("deptID") int deptID) {
  162.         List<ApiCourseWrapper> allCoursesWrapped = new ArrayList<>();
  163.         for (Department currentDepartment : allDepartments) {
  164.             if (currentDepartment.getDepartmentID() == deptID) {
  165.  
  166.                 for (Course currentCourse : currentDepartment.getAllCourses()) {
  167.                     ApiCourseWrapper newCourseWrapper =
  168.                             new ApiCourseWrapper(currentCourse.getCourseID(),
  169.                                                 currentCourse.getCatalogueNumber());
  170.                     allCoursesWrapped.add(newCourseWrapper);
  171.                 }
  172.                 return allCoursesWrapped;
  173.             }
  174.         }
  175.         throw new IndexOutOfBoundsException(); //404
  176.     }
  177.  
  178.     @ResponseStatus(value = HttpStatus.OK) //200
  179.     @GetMapping("/api/departments/{deptID}/courses/{courseID}/offerings")
  180.     public List<ApiCourseOfferingWrapper> getCourseOfferings(@PathVariable("deptID") int deptID,
  181.                                                              @PathVariable("courseID") int courseID) {
  182.         List<ApiCourseOfferingWrapper> allCourseOfferingsWrapped = new ArrayList<>();
  183.  
  184.         for (Department currentDepartment : allDepartments) {
  185.             if (currentDepartment.getDepartmentID() == deptID) {
  186.  
  187.                 for (Course currentCourse : currentDepartment.getAllCourses()) {
  188.                     if (currentCourse.getCourseID() == courseID) {
  189.  
  190.                         for (CourseOffering currentCourseOffering : currentCourse.getAllCourseOfferings()) {
  191.                             ApiCourseOfferingWrapper newCourseOfferingWrapper =
  192.                                     new ApiCourseOfferingWrapper(currentCourseOffering.getCourseOfferingID(),
  193.                                                                 currentCourseOffering.getLocation(),
  194.                                                                 currentCourseOffering.getInstructors(),
  195.                                                                 currentCourseOffering.getTerm(),
  196.                                                                 currentCourseOffering.getSemesterCode(),
  197.                                                                 currentCourseOffering.getYear());
  198.                             allCourseOfferingsWrapped.add(newCourseOfferingWrapper);
  199.                         }
  200.                     }
  201.                     return allCourseOfferingsWrapped;
  202.                 }
  203.             }
  204.         }
  205.         throw new IndexOutOfBoundsException(); //404
  206.     }
  207.  
  208.     @ResponseStatus(value = HttpStatus.OK) //200
  209.     @GetMapping("/api/departments/{deptID}/courses/{courseID}/offerings/{courseOfferingID}")
  210.     public List<ApiOfferingSectionWrapper> getSections(@PathVariable("deptID") int deptID,
  211.                                                        @PathVariable("courseID") int courseID,
  212.                                                        @PathVariable("courseOfferingID") int courseOfferingID) {
  213.         List<ApiOfferingSectionWrapper> allSectionsWrapped = new ArrayList<>();
  214.         for (Department currentDepartment : allDepartments) {
  215.             if (currentDepartment.getDepartmentID() == deptID) {
  216.  
  217.                 for (Course currentCourse : currentDepartment.getAllCourses()) {
  218.                     if (currentCourse.getCourseID() == courseID) {
  219.  
  220.                         for (CourseOffering currentCourseOffering : currentCourse.getAllCourseOfferings()) {
  221.                             if (currentCourseOffering.getCourseOfferingID() == courseOfferingID) {
  222.  
  223.                                 for (Section currentSection : currentCourseOffering.getAllSections()) {
  224.                                     ApiOfferingSectionWrapper newSectionWrapper =
  225.                                             new ApiOfferingSectionWrapper(currentSection.getType(),
  226.                                                                         currentSection.getEnrollmentCapacity(),
  227.                                                                         currentSection.getEnrollmentTotal());
  228.                                     allSectionsWrapped.add(newSectionWrapper);
  229.                                 }
  230.                                 return allSectionsWrapped;
  231.                             }
  232.                         }
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.         throw new IndexOutOfBoundsException(); //404
  238.     }
  239.  
  240.     @ResponseStatus(value = HttpStatus.OK)
  241.     @GetMapping("api/stats/student-per-semester?deptID={courseID}")
  242.     public void getGraphData(@PathVariable("courseID") int courseID) {
  243.         //NOT VOID; do later
  244.         //return array sorted by semesterCode; chronological order
  245.  
  246.         throw new IndexOutOfBoundsException();
  247.     }
  248.  
  249.     @ResponseStatus(value = HttpStatus.CREATED) //201
  250.     @PostMapping("/api/addoffering")
  251.     public void addOffering() {
  252.  
  253.     }
  254.  
  255.     @ResponseStatus(value = HttpStatus.OK) //200
  256.     @GetMapping("/api/watchers")
  257.     public void getWatchers() {
  258.         //NOT VOID; do later; watcher class probably
  259.     }
  260.  
  261.     @ResponseStatus(value = HttpStatus.CREATED) //201
  262.     @PostMapping("/api/watchers")
  263.     public void addWatcher() {
  264.         //do later
  265.     }
  266.  
  267.     @ResponseStatus(value = HttpStatus.OK) //200
  268.     @GetMapping("/api/watchers/{watcherID}")
  269.     public void getWatcher(@PathVariable("watcherID") int watcherID) {
  270.         //do later
  271.  
  272.         throw new IndexOutOfBoundsException();
  273.     }
  274.  
  275.     @ResponseStatus(value = HttpStatus.NO_CONTENT) //204
  276.     @DeleteMapping("/api/watchers/{watcherID}")
  277.     public void deleteWatcher(@PathVariable("watcherID") int watcherID) {
  278.         //do later
  279.  
  280.         throw new IndexOutOfBoundsException(); //404
  281.     }
  282.  
  283.     @ResponseStatus(value = HttpStatus.NOT_FOUND) //404
  284.     @ExceptionHandler(IndexOutOfBoundsException.class)
  285.     public void IndexOutOfBoundsException() {
  286.         //nothing to do here
  287.     }
  288.  
  289.     @ResponseStatus(value = HttpStatus.BAD_REQUEST) //400
  290.     @ExceptionHandler(IllegalArgumentException.class)
  291.     public void IllegalArgumentException() {
  292.         //nothing to do here
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement