| 1 | package mk.ukim.finki.wp.db.controller;
|
|---|
| 2 |
|
|---|
| 3 | import lombok.RequiredArgsConstructor;
|
|---|
| 4 | import mk.ukim.finki.wp.db.entity.Course;
|
|---|
| 5 | import mk.ukim.finki.wp.db.entity.ModuleEntity;
|
|---|
| 6 | import mk.ukim.finki.wp.db.service.CourseService;
|
|---|
| 7 | import mk.ukim.finki.wp.db.service.ModuleService;
|
|---|
| 8 | import org.springframework.stereotype.Controller;
|
|---|
| 9 | import org.springframework.ui.Model;
|
|---|
| 10 | import org.springframework.web.bind.annotation.GetMapping;
|
|---|
| 11 | import org.springframework.web.bind.annotation.PathVariable;
|
|---|
| 12 | import org.springframework.web.bind.annotation.PostMapping;
|
|---|
| 13 | import org.springframework.web.bind.annotation.RequestParam;
|
|---|
| 14 |
|
|---|
| 15 | @Controller
|
|---|
| 16 | @RequiredArgsConstructor
|
|---|
| 17 | public class ModuleController {
|
|---|
| 18 |
|
|---|
| 19 | private final CourseService courseService;
|
|---|
| 20 | private final ModuleService moduleService;
|
|---|
| 21 |
|
|---|
| 22 | @GetMapping("/module/{courseId}")
|
|---|
| 23 | public String getModulesPage(@PathVariable Integer courseId, Model model) {
|
|---|
| 24 | Course course = courseService.findById(courseId);
|
|---|
| 25 |
|
|---|
| 26 | model.addAttribute("course", course);
|
|---|
| 27 | model.addAttribute("modules", moduleService.findAllByCourse(course));
|
|---|
| 28 |
|
|---|
| 29 | return "module/module";
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | @GetMapping("/module/{courseId}/add")
|
|---|
| 33 | public String getAddModulePage(@PathVariable Integer courseId, Model model) {
|
|---|
| 34 | Course course = courseService.findById(courseId);
|
|---|
| 35 |
|
|---|
| 36 | model.addAttribute("course", course);
|
|---|
| 37 |
|
|---|
| 38 | return "module/module_form";
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @PostMapping("/module/{courseId}/add")
|
|---|
| 42 | public String addModule(@PathVariable Integer courseId,
|
|---|
| 43 | @RequestParam String title,
|
|---|
| 44 | @RequestParam String description) {
|
|---|
| 45 |
|
|---|
| 46 | Course course = courseService.findById(courseId);
|
|---|
| 47 | moduleService.addModule(course, title, description);
|
|---|
| 48 |
|
|---|
| 49 | return "redirect:/module/" + courseId;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @GetMapping("/module/{courseId}/edit/{moduleId}")
|
|---|
| 53 | public String getEditModulePage(@PathVariable Integer courseId,
|
|---|
| 54 | @PathVariable Integer moduleId,
|
|---|
| 55 | Model model) {
|
|---|
| 56 |
|
|---|
| 57 | Course course = courseService.findById(courseId);
|
|---|
| 58 | ModuleEntity module = moduleService.findById(moduleId);
|
|---|
| 59 |
|
|---|
| 60 | model.addAttribute("course", course);
|
|---|
| 61 | model.addAttribute("module", module);
|
|---|
| 62 |
|
|---|
| 63 | return "module/module_form";
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @PostMapping("/module/{courseId}/edit/{moduleId}")
|
|---|
| 67 | public String editModule(@PathVariable Integer courseId,
|
|---|
| 68 | @PathVariable Integer moduleId,
|
|---|
| 69 | @RequestParam String title,
|
|---|
| 70 | @RequestParam String description) {
|
|---|
| 71 |
|
|---|
| 72 | moduleService.editModule(moduleId, title, description);
|
|---|
| 73 |
|
|---|
| 74 | return "redirect:/module/" + courseId;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @PostMapping("/module/{courseId}/delete/{moduleId}")
|
|---|
| 78 | public String deleteModule(@PathVariable Integer courseId,
|
|---|
| 79 | @PathVariable Integer moduleId) {
|
|---|
| 80 |
|
|---|
| 81 | moduleService.deleteModule(moduleId);
|
|---|
| 82 |
|
|---|
| 83 | return "redirect:/module/" + courseId;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|