| 1 | package mk.ukim.finki.wp.db.controller;
|
|---|
| 2 |
|
|---|
| 3 | import lombok.RequiredArgsConstructor;
|
|---|
| 4 | import mk.ukim.finki.wp.db.dto.ModuleWithLessonDto;
|
|---|
| 5 | import mk.ukim.finki.wp.db.entity.Course;
|
|---|
| 6 | import mk.ukim.finki.wp.db.entity.ModuleEntity;
|
|---|
| 7 | import mk.ukim.finki.wp.db.service.CategoryService;
|
|---|
| 8 | import mk.ukim.finki.wp.db.service.CourseService;
|
|---|
| 9 | import mk.ukim.finki.wp.db.service.LessonService;
|
|---|
| 10 | import mk.ukim.finki.wp.db.service.ModuleService;
|
|---|
| 11 | import org.springframework.security.core.Authentication;
|
|---|
| 12 | import org.springframework.stereotype.Controller;
|
|---|
| 13 | import org.springframework.ui.Model;
|
|---|
| 14 | import org.springframework.web.bind.annotation.GetMapping;
|
|---|
| 15 | import org.springframework.web.bind.annotation.PathVariable;
|
|---|
| 16 | import org.springframework.web.bind.annotation.PostMapping;
|
|---|
| 17 | import org.springframework.web.bind.annotation.RequestParam;
|
|---|
| 18 |
|
|---|
| 19 | import java.math.BigDecimal;
|
|---|
| 20 | import java.util.ArrayList;
|
|---|
| 21 | import java.util.List;
|
|---|
| 22 |
|
|---|
| 23 | @Controller
|
|---|
| 24 | @RequiredArgsConstructor
|
|---|
| 25 | public class CourseController {
|
|---|
| 26 |
|
|---|
| 27 | private final CourseService courseService;
|
|---|
| 28 | private final CategoryService categoryService;
|
|---|
| 29 | private final ModuleService moduleService;
|
|---|
| 30 | private final LessonService lessonService;
|
|---|
| 31 |
|
|---|
| 32 | @GetMapping("/course")
|
|---|
| 33 | public String getCoursePage(Model model, Authentication authentication) {
|
|---|
| 34 | Object principal = authentication.getPrincipal();
|
|---|
| 35 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 36 | model.addAttribute("availableCourses", courseService.findAllWithUserNotEnrolled(email));
|
|---|
| 37 | model.addAttribute("unavailableCourses", courseService.findAllWithUserEnrolled(email));
|
|---|
| 38 | return "course/course";
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @GetMapping("/instructor/course")
|
|---|
| 42 | public String getInstructorCoursePage(Model model, Authentication authentication) {
|
|---|
| 43 | Object principal = authentication.getPrincipal();
|
|---|
| 44 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 45 | model.addAttribute("courses", courseService.findAllByInstructor(email));
|
|---|
| 46 | return "course/course_instructor";
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @GetMapping("/add/course")
|
|---|
| 50 | public String getAddCoursePage(Model model) {
|
|---|
| 51 | model.addAttribute("categories", categoryService.findAll());
|
|---|
| 52 | return "course/course_form";
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | @PostMapping("/add/course")
|
|---|
| 56 | public String addCourse(@RequestParam String name,
|
|---|
| 57 | @RequestParam BigDecimal price,
|
|---|
| 58 | @RequestParam Integer categoryId,
|
|---|
| 59 | Authentication authentication) {
|
|---|
| 60 | Object principal = authentication.getPrincipal();
|
|---|
| 61 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 62 | courseService.addCourse(name, price, categoryId, email);
|
|---|
| 63 | return "redirect:/instructor/course";
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @GetMapping("/edit/course/{id}")
|
|---|
| 67 | public String getEditCoursePage(@PathVariable Integer id, Model model) {
|
|---|
| 68 | model.addAttribute("categories", categoryService.findAll());
|
|---|
| 69 | model.addAttribute("course", courseService.findById(id));
|
|---|
| 70 | return "course/course_form";
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @PostMapping("/edit/course/{id}")
|
|---|
| 74 | public String editCourse(@PathVariable Integer id,
|
|---|
| 75 | @RequestParam String name,
|
|---|
| 76 | @RequestParam BigDecimal price,
|
|---|
| 77 | @RequestParam Integer categoryId,
|
|---|
| 78 | Authentication authentication) {
|
|---|
| 79 | courseService.editCourse(id, name, price, categoryId);
|
|---|
| 80 | return "redirect:/instructor/course";
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | @PostMapping("/delete/course/{id}")
|
|---|
| 84 | public String deleteCourse(@PathVariable Integer id) {
|
|---|
| 85 | courseService.deleteCourse(id);
|
|---|
| 86 | return "redirect:/instructor/course";
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @PostMapping("/change-status/course/{id}")
|
|---|
| 90 | public String changeCourseStatus(@PathVariable Integer id) {
|
|---|
| 91 | courseService.changeCourseStatus(id);
|
|---|
| 92 | return "redirect:/instructor/course";
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @GetMapping("/course/enroll/{id}")
|
|---|
| 96 | public String enrollUserToCourse(@PathVariable Integer id, Authentication authentication) {
|
|---|
| 97 | Object principal = authentication.getPrincipal();
|
|---|
| 98 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 99 | courseService.enrollUserToCourse(id, email);
|
|---|
| 100 | return "redirect:/course";
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | @GetMapping("/course/{id}")
|
|---|
| 104 | public String goToFullCoursePage(@PathVariable Integer id, Model model, Authentication authentication) {
|
|---|
| 105 | Course course = courseService.findById(id);
|
|---|
| 106 | List<ModuleEntity> modules = moduleService.findAllByCourse(course);
|
|---|
| 107 | List<ModuleWithLessonDto> moduleWithLessonDtos = new ArrayList<>();
|
|---|
| 108 |
|
|---|
| 109 | Object principal = authentication.getPrincipal();
|
|---|
| 110 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 111 |
|
|---|
| 112 | for(ModuleEntity module : modules) {
|
|---|
| 113 | ModuleWithLessonDto moduleWithLessonDto = new ModuleWithLessonDto();
|
|---|
| 114 | moduleWithLessonDto.setModuleId(module.getModuleId());
|
|---|
| 115 | moduleWithLessonDto.setTitle(module.getTitle());
|
|---|
| 116 | moduleWithLessonDto.setDescription(module.getDescription());
|
|---|
| 117 | moduleWithLessonDto.setLessons(lessonService.findAllByModule(module));
|
|---|
| 118 | moduleWithLessonDtos.add(moduleWithLessonDto);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | model.addAttribute("modules", moduleWithLessonDtos);
|
|---|
| 122 | model.addAttribute("course", course);
|
|---|
| 123 | model.addAttribute("certificate", courseService.getCertificate(id, email));
|
|---|
| 124 |
|
|---|
| 125 | return "course/course_view";
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | @PostMapping("/course/complete/{id}")
|
|---|
| 129 | public String completeCourse(@PathVariable Integer id, Authentication authentication) {
|
|---|
| 130 | Object principal = authentication.getPrincipal();
|
|---|
| 131 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 132 | courseService.completeCourse(id, email);
|
|---|
| 133 | return "redirect:/course/" + id;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|