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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

import javax.validation.Valid;

import at.ac.tuwien.inso.controller.admin.forms.AddLecturersToSubjectForm;
import at.ac.tuwien.inso.entity.Lecturer;
import at.ac.tuwien.inso.entity.Subject;
import at.ac.tuwien.inso.service.SubjectService;

@Controller
@RequestMapping("/admin/subjects/{subjectId}")
public class AdminSubjectLecturersController {

    @Autowired
    private SubjectService subjectService;

    @ModelAttribute("subject")
    private Subject getSubject(@PathVariable Long subjectId) {
        return subjectService.findOne(subjectId);
    }

    @GetMapping(value = "/availableLecturers.json")
    @ResponseBody
    public List<Lecturer> getAvailableLecturers(@PathVariable Long subjectId) {
        return subjectService.getAvailableLecturersForSubject(subjectId);
    }

    @PostMapping("/lecturers")
    public String addLecturer(
            @PathVariable Long subjectId,
            @Valid AddLecturersToSubjectForm addLecturersToSubjectForm,
            RedirectAttributes redirectAttributes
    ) {

        Long lecturerUisUserId = addLecturersToSubjectForm.toLecturerId();
        subjectService.addLecturerToSubject(subjectId, lecturerUisUserId);

        return "redirect:/admin/subjects/" + subjectId;
    }

    @GetMapping("/lecturers/{lecturerId}/delete")
    public String removeLecturer(
            @PathVariable Long subjectId,
            @PathVariable Long lecturerId,
            RedirectAttributes redirectAttributes
    ) {
            subjectService.removeLecturerFromSubject(subjectId, lecturerId);
        return "redirect:/admin/subjects/" + subjectId;
    }
}

@@ -204,16 +204,78 @@
 Result;%0A
+import org.springframework.web.bind.annotation.DeleteMapping;%0A
 import o
@@ -317,32 +317,32 @@
 ion.GetMapping;%0A
-
 import org.sprin
@@ -919,16 +919,201 @@
 ubject;%0A
+import at.ac.tuwien.inso.exception.LecturerNotFoundException;%0Aimport at.ac.tuwien.inso.exception.RelationNotfoundException;%0Aimport at.ac.tuwien.inso.exception.SubjectNotFoundException;%0A
 import a
@@ -2382,145 +2382,961 @@
 ) %7B%0A
-            subjectService.removeLecturerFromSubject(subjectId, lecturerId);%0A        return %22redirect:/admin/subjects/%22 + subjectId;%0A    %7D
+%0A        try %7B%0A            Lecturer removedLecturer = subjectService.removeLecturerFromSubject(subjectId,%0A                    lecturerId);%0A%0A            String name = removedLecturer.getName();%0A%0A            redirectAttributes.addFlashAttribute(%22flashMessage%22, %22Lecturer %22 + name + %22 removed%22);%0A            return %22redirect:/admin/subjects/%22 + subjectId;%0A%0A        %7D catch (SubjectNotFoundException e) %7B%0A            redirectAttributes.addFlashAttribute(%22flashMessage%22, %22Subject not found%22);%0A            return %22redirect:/admin/subjects%22;%0A        %7D catch (LecturerNotFoundException e) %7B%0A            redirectAttributes.addFlashAttribute(%22flashMessage%22, %22Lecturer not found%22);%0A            return %22redirect:/admin/subjects/%22 + subjectId;%0A        %7D catch (RelationNotfoundException e) %7B%0A            redirectAttributes.addFlashAttribute(%22flashMessage%22, %22Person was not not a lecturer%22);%0A            return %22redirect:/admin/subjects/%22 + subjectId;%0A        %7D%0A    %7D%0A
 %0A%7D%0A

Implement "remove lecturer from subject" in controller