Advertisement
Alekss33

Untitled

Aug 12th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. package com.example.teacherapp.controllers;
  2.  
  3. import com.example.teacherapp.models.*;
  4. import com.example.teacherapp.services.*;
  5. import com.example.teacherapp.services.functional.ProgressTracker;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.*;
  10. import org.springframework.web.multipart.MultipartFile;
  11.  
  12. import javax.validation.Valid;
  13. import java.io.IOException;
  14. import java.nio.file.Files;
  15. import java.nio.file.Path;
  16. import java.nio.file.Paths;
  17. import java.text.DecimalFormat;
  18. import java.util.List;
  19.  
  20. @Controller
  21. public class LectureController {
  22. private LectureService lectureService;
  23. private CourseService courseService;
  24. private StudentCourseService studentCourseService;
  25. private ProgressTracker progressTracker;
  26. private RatingService ratingService;
  27. private UserService userService;
  28. private CommentService commentService;
  29.  
  30. public static String uploadDirectory = System.getProperty("user.dir")+"/uploads";
  31.  
  32. @Autowired
  33. public LectureController(LectureService lectureService, CourseService courseService, StudentCourseService studentCourseService, ProgressTracker progressTracker, RatingService ratingService, UserService userService, CommentService commentService) {
  34. this.lectureService = lectureService;
  35. this.courseService = courseService;
  36. this.studentCourseService = studentCourseService;
  37. this.progressTracker = progressTracker;
  38. this.ratingService = ratingService;
  39. this.userService = userService;
  40. this.commentService = commentService;
  41. }
  42.  
  43. @GetMapping("/lectures/{courseId}")
  44. public String showCourseInfo(@PathVariable int courseId, Model model) {
  45.  
  46.  
  47. try {
  48.  
  49. StudentCourse checkStudentCourse = studentCourseService.getById(userService.getSessionUser().getUsername(), courseId);
  50. List<Lecture> courseLectures = lectureService.getAllForCourse(courseId);
  51. Course course = courseService.getById(courseId);
  52. int progress = progressTracker.totalProgress(userService.getSessionUser().getUsername(), courseId);
  53. List<Rating> checkIfRatingExist = ratingService.getByCourseAndStudent(userService.getSessionUser().getUsername(), courseId);
  54. RatingDTO ratingDto = new RatingDTO();
  55. Double courseRating = ratingService.getAverageCourseRating(courseId);
  56. DecimalFormat df = new DecimalFormat("0.##");
  57. courseRating = Double.valueOf(df.format(courseRating));
  58.  
  59. List<Comment> allComments = commentService.getAllComments();
  60. model.addAttribute("allComments",allComments);
  61.  
  62. model.addAttribute("studentCourse", checkStudentCourse);
  63. model.addAttribute("lectures", courseLectures);
  64. model.addAttribute("course", course);
  65. model.addAttribute("progress", progress);
  66. model.addAttribute("checkRating", checkIfRatingExist);
  67. model.addAttribute("ratingDTO", ratingDto);
  68. model.addAttribute("courseRating", courseRating);
  69.  
  70.  
  71. return "courseInfo";
  72. } catch (Exception ex) {
  73. List<Lecture> courseLectures = lectureService.getAllForCourse(courseId);
  74. Course course = courseService.getById(courseId);
  75. model.addAttribute("lectures", courseLectures);
  76. model.addAttribute("course", course);
  77. return "courseInfoII";
  78. }
  79.  
  80. }
  81.  
  82. @GetMapping("/lectures/enroll/{courseId}")
  83. public String enrollInCourse(@PathVariable int courseId) {
  84.  
  85. studentCourseService.create(userService.getSessionUser().getUsername(), courseId);
  86.  
  87. return "redirect:/lectures/" + courseId;
  88. }
  89.  
  90.  
  91. @GetMapping("/lecture/new")
  92. public String createLectureView(Model model) {
  93.  
  94. List<Course> teachersCourses = courseService.getAllActiveByTeacher(userService.getSessionUser().getUsername());
  95. model.addAttribute("lecture", new Lecture());
  96. model.addAttribute("courses", teachersCourses);
  97. return "createLecture";
  98. }
  99.  
  100. //TODO
  101. @PostMapping("/lecture/new")
  102. public String createNewLecture(@Valid @ModelAttribute Lecture lecture, @RequestParam("files") MultipartFile file) throws IOException {
  103.  
  104. Path fileNameAndPath = Paths.get(uploadDirectory, file.getOriginalFilename());
  105. String fileName =file.getOriginalFilename();
  106.  
  107. Files.write(fileNameAndPath, file.getBytes());
  108.  
  109. lectureService.create(lecture.getTitle(), lecture.getDescription(), lecture.getVideoName(), fileName, lecture.getCourseId());
  110. return "redirect:/lecture/new";
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement