package at.ac.tuwien.inso.controller.lecturer;

import at.ac.tuwien.inso.controller.lecturer.forms.*;
import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.service.*;
import at.ac.tuwien.inso.service.impl.Messages;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.*;

@Controller
@RequestMapping("/lecturer/editCourse")
public class LecturerEditCourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private TagService tagService;

    @Autowired
    private Messages messages;

    @ModelAttribute("subject")
    private Subject getSubject(@RequestParam("courseId") Long courseId) {
        return courseService.findOne(courseId).getSubject();
    }

    @ModelAttribute("addCourseForm")
    private AddCourseForm getAddCourseForm(@RequestParam("courseId") Long courseId) {
        Course course = courseService.findOne(courseId);
        AddCourseForm form = new AddCourseForm(course);
        form.setInitialTags(tagService.findAll());
        form.setInitialActiveTags(course.getTags());
        return form;
    }


    @GetMapping
    private String getAddCoursesPage(@RequestParam("courseId") Long courseId) {
        return "lecturer/editCourse";
    }

    @PostMapping
    private String createCourse(@ModelAttribute AddCourseForm form,
                                RedirectAttributes redirectAttributes) {
        Course course = courseService.saveCourse(form);

        redirectAttributes.addFlashAttribute("flashMessageNotLocalized", messages.msg("lecturer.course.edit.success", course.getSubject().getName()));
        return "redirect:/lecturer/courses";
    }

}

@@ -306,24 +306,61 @@
 ereotype.*;%0A
+import org.springframework.ui.Model;%0A
 import org.s
@@ -1287,18 +1287,18 @@
  get
-Add
+Edit
 Course
-s
 Page
@@ -1329,35 +1329,151 @@
 %22) Long courseId
-) %7B
+, Model model) %7B%0A        Course course = courseService.findOne(courseId);%0A        model.addAttribute(%22course%22, course);
 %0A        return 
@@ -1542,11 +1542,11 @@
 ing 
-cre
+upd
 ateC

add model attribute for the course