package at.ac.tuwien.inso.service;

import at.ac.tuwien.inso.controller.lecturer.forms.AddCourseForm;
import at.ac.tuwien.inso.entity.Course;
import at.ac.tuwien.inso.entity.Lecturer;
import at.ac.tuwien.inso.entity.Semester;
import at.ac.tuwien.inso.entity.Subject;
import at.ac.tuwien.inso.entity.Tag;
import at.ac.tuwien.inso.repository.CourseRepository;
import at.ac.tuwien.inso.repository.LecturerRepository;
import at.ac.tuwien.inso.repository.SubjectRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.validation.constraints.NotNull;

@Service
public class CourseService {

    @Autowired
    private SemesterService semesterService;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private SubjectRepository subjectRepository;


    @Transactional
    public List<Course> findCourseForCurrentSemesterWithName(@NotNull String name) {
        Semester semester = semesterService.getCurrentSemester();
        return courseRepository.findAllBySemesterAndSubjectNameLikeIgnoreCase(semester, "%" + name + "%");
    }

    @Transactional
    public List<Course> findCoursesForCurrentSemesterForLecturer(Lecturer lecturer) {
        Semester semester = semesterService.getCurrentSemester();
        Iterable<Subject> subjectsForLecturer = subjectRepository.findByLecturers_Id(lecturer.getId());
        List<Course> courses = new ArrayList<>();
        subjectsForLecturer.forEach(subject -> courses.addAll(courseRepository.findAllBySemesterAndSubject(semester, subject)));
        return courses;
    }

    @Transactional
    public Course saveCourse(AddCourseForm form) {
        Course course = form.getCourse();
        List<Tag> tags = form.getActiveAndInactiveTags().stream()
                .filter(tagBooleanEntry -> tagBooleanEntry.isActive())
                .map(tagBooleanEntry -> tagBooleanEntry.getTag())
                .collect(Collectors.toList());
        tags.forEach(tag -> course.addTags(tag));
        if (!(course.getStudentLimits() > 0)) {
            course.setStudentLimits(1);
        }
        return courseRepository.save(course);
    }

    @Transactional
    public Course findCourseWithId(Long id) {
        return courseRepository.findOne(id);
    }

}

@@ -215,24 +215,65 @@
 y.Semester;%0A
+import at.ac.tuwien.inso.entity.Student;%0A
 import at.ac
@@ -301,16 +301,16 @@
 ubject;%0A
-
 import a
@@ -440,32 +440,87 @@
 urerRepository;%0A
+import at.ac.tuwien.inso.repository.StudentRepository;%0A
 import at.ac.tuw
@@ -1130,16 +1130,149 @@
 tory;%0A%0A%0A
+    @Autowired%0A    private StudentRepository studentRepository;%0A%0A    @Autowired%0A    private UserAccountService userAccountService;%0A%0A%0A
     @Tra
@@ -2656,24 +2656,24 @@
 (Long id) %7B%0A
-
         retu
@@ -2712,10 +2712,524 @@
 %0A    %7D%0A%0A
+%0A    @Transactional%0A    public boolean registerStudentForCourse(Course course) %7B%0A        Student student = studentRepository.findOne(userAccountService.getCurrentLoggedInUser().getId());%0A        if (course.getStudentLimits() %3E= course.getStudents().size()) %7B%0A            return false;%0A        %7D else if (course.getStudents().contains(student)) %7B%0A            return true;%0A        %7D else %7B%0A            course.addStudents(student);%0A            courseRepository.save(course);%0A            return true;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add register student for course method to service