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

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import at.ac.tuwien.inso.entity.Course;
import at.ac.tuwien.inso.entity.Student;
import at.ac.tuwien.inso.service.CourseService;
import at.ac.tuwien.inso.service.StudentService;
import at.ac.tuwien.inso.service.impl.Messages;

@Controller
@RequestMapping("/student")
public class StudentCourseRegistrationController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private Messages messages;

    @PostMapping("/register")
    public String registerStudent(@RequestParam Long courseId,
                                   RedirectAttributes redirectAttributes) {
        Course course = courseService.findOne(courseId);
        if (courseService.registerStudentForCourse(course)) {
            String successMsg = messages.msg("student.my.courses.register.success", course.getSubject().getName());
            redirectAttributes.addFlashAttribute("flashMessageNotLocalized", successMsg);
            return "redirect:/student/courses";
        } else {
            String failMsg = messages.msg("student.my.courses.register.fail", course.getSubject().getName());
            redirectAttributes.addFlashAttribute("flashMessageNotLocalized", failMsg);
            return "redirect:/student/courses";
        }
    }

    @PostMapping("/unregister")
    public String unregisterStudent(@RequestParam Long course,
                                    RedirectAttributes redirectAttributes,
                                    Principal principal) {
        Student student = studentService.findByUsername(principal.getName());
        Course courseToUnregister = courseService.unregisterStudentFromCourse(student, course);

        String successMsg = messages.msg("student.my.courses.unregister.success", courseToUnregister.getSubject().getName());
        redirectAttributes.addFlashAttribute("flashMessageNotLocalized", successMsg);
        return "redirect:/student/myCourses";
    }
}

@@ -980,32 +980,45 @@
 registerStudent(
+%0A            
 @RequestParam Lo
@@ -1030,39 +1030,16 @@
 urseId,%0A
-                       
         
@@ -1079,16 +1079,21 @@
 tributes
+%0A    
 ) %7B%0A    
@@ -1145,16 +1145,17 @@
 rseId);%0A
+%0A
         
@@ -1204,24 +1204,87 @@
 (course)) %7B%0A
+            String courseName = course.getSubject().getName();%0A
             
@@ -1361,39 +1361,20 @@
 , course
-.getSubject().get
 Name
-()
 );%0A     
@@ -1519,20 +1519,10 @@
    %7D
- else %7B%0A    
+%0A%0A
     
@@ -1619,28 +1619,24 @@
 getName());%0A
-    
         redi
@@ -1706,36 +1706,32 @@
 ilMsg);%0A        
-    
 return %22redirect
@@ -1750,26 +1750,16 @@
 urses%22;%0A
-        %7D%0A
     %7D%0A%0A 

Fix code style