package at.ac.tuwien.inso.service;

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.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.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(Course course) {
        return courseRepository.save(course);
    }

}

@@ -29,16 +29,82 @@
 rvice;%0A%0A
+import at.ac.tuwien.inso.controller.lecturer.forms.AddCourseForm;%0A
 import a
@@ -260,16 +260,53 @@
 ubject;%0A
+import at.ac.tuwien.inso.entity.Tag;%0A
 import a
@@ -686,24 +686,60 @@
 .util.List;%0A
+import java.util.stream.Collectors;%0A
 import java.
@@ -1855,24 +1855,371 @@
 rse(
-Course course) %7B
+AddCourseForm form) %7B%0A        Course course = form.getCourse();%0A        List%3CTag%3E tags = form.getActiveAndInactiveTags().stream()%0A                .filter(tagBooleanEntry -%3E tagBooleanEntry.isActive())%0A                .map(tagBooleanEntry -%3E tagBooleanEntry.getTag())%0A                .collect(Collectors.toList());%0A        tags.forEach(tag -%3E course.addTags(tag));
 %0A   

Add save course to service