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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.mvc.support.RedirectAttributes;

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

@Controller
@RequestMapping("/admin/subjects")
public class AdminSubjectsController {

    private static final Logger logger = LoggerFactory.getLogger(AdminSubjectsController.class);

    @Autowired
    private SubjectService subjectService;

    private static final Integer SUBJECTS_PER_PAGE = 10;

    @GetMapping
    private String listSubjects(Model model) {
        return listSubjectsForPage(0, model);
    }

    @GetMapping("/page/{pageNumber}")
    private String listSubjectsForPage(@PathVariable Integer pageNumber, Model model) {
        Page<Subject> subjects = subjectService.findAll(new PageRequest(pageNumber, SUBJECTS_PER_PAGE));
        model.addAttribute("subjects", subjects.getContent());
        model.addAttribute("page", subjects);
        return "admin/subjects";
    }

    @GetMapping("/{id}")
    private String getSubject(
            @PathVariable Long id,
            Model model,
            AddLecturersToSubjectForm addLecturersToSubjectForm,
            RedirectAttributes redirectAttributes
    ) {
        Subject subject = subjectService.findOne(id);
        model.addAttribute("subject", subject);

        if (subject == null) {
            logger.info("/admin/subjects: Subject with id " + id + " not found");

            String msgId = "admin.subjects.notFound";
            redirectAttributes.addFlashAttribute("flashMessage", msgId);

            return "redirect:/admin/subjects";
        }

        model.addAttribute("lecturers", subject.getLecturers());
        model.addAttribute("requiredSubjects", subject.getRequiredSubjects());
        return "admin/subject-details";
    }

    @GetMapping("/add")
    private String addSubject(Model model) {
        model.addAttribute("subject", new Subject());
        return "admin/subject-add";
    }

    @PostMapping("/add")
    private String addSubject(@ModelAttribute Subject subject) {
        subject = subjectService.create(subject);
        return "redirect:/admin/subjects/"+subject.getId();
    }
}

@@ -654,32 +654,94 @@
 equestMapping;%0D%0A
+import org.springframework.web.bind.annotation.RequestParam;%0D%0A
 import org.sprin
@@ -1330,16 +1330,108 @@
 ubjects(
+%0D%0A            @RequestParam(value = %22search%22, required = false) String search,%0D%0A            
 Model mo
@@ -1425,32 +1425,38 @@
      Model model
+%0D%0A    
 ) %7B%0D%0A        ret
@@ -1479,16 +1479,24 @@
 ForPage(
+search, 
 0, model
@@ -1586,16 +1586,108 @@
 ForPage(
+%0D%0A            @RequestParam(value = %22search%22, required = false) String search,%0D%0A            
 @PathVar
@@ -1711,16 +1711,29 @@
 eNumber,
+%0D%0A           
  Model m
@@ -1736,29 +1736,37 @@
 el model
+%0D%0A    
 ) %7B%0D%0A
+%0D%0A
         
 Page%3CSub
@@ -1761,104 +1761,223 @@
     
-Page%3CSubject%3E subjects = subjectService.findAll(new PageRequest(pageNumber, SUBJECTS_PER_PAGE));
+final String searchString = getSearchString(search);%0D%0A        PageRequest page = new PageRequest(pageNumber, SUBJECTS_PER_PAGE);%0D%0A%0D%0A        Page%3CSubject%3E subjects = subjectService.findBySearch(searchString, page);%0D%0A
 %0D%0A  
@@ -2081,24 +2081,26 @@
 subjects);%0D%0A
+%0D%0A
         retu
@@ -2122,32 +2122,290 @@
 cts%22;%0D%0A    %7D%0D%0A%0D%0A
+    @ModelAttribute(%22search%22)%0D%0A    private String getSearchString(@RequestParam(value = %22search%22, required = false) String search) %7B%0D%0A        if (search != null && !search.isEmpty()) %7B%0D%0A            return search;%0D%0A        %7D%0D%0A%0D%0A        return %22%22;%0D%0A    %7D%0D%0A%0D%0A%0D%0A
     @GetMapping(

Add search to SubjectsController