CourseService.java
/**
* TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
* Copyright (C) 2020 Eduardo Rodriguez Carro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.uoc.tfg.sel.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.uoc.tfg.sel.repository.CourseRepository;
import org.uoc.tfg.sel.repository.UserRepository;
import org.uoc.tfg.sel.repository.model.Course;
import org.uoc.tfg.sel.repository.model.User;
/**
* The Class CourseService.
* @author Eduardo Rodriguez Carro
*/
@Service
public class CourseService {
/** The course repository. */
@Autowired
private CourseRepository courseRepository;
/** The user repository. */
@Autowired
private UserRepository userRepository;
/**
* Gets the all.
*
* @return the all
*/
@Transactional(readOnly = true)
public List<Course> getAll(){
return ServiceUtils.iterableToList(courseRepository.findAll());
}
/**
* Gets the my courses.
*
* @param user the user
* @return the my courses
*/
@Transactional(readOnly = true)
public List<Course> getMyCourses(User user) {
List<Course> courses = courseRepository.getByTeacherTutorAndOpen(user,true);
return courses;
}
/**
* Gets the by id.
*
* @param id the id
* @return the by id
*/
@Transactional(readOnly = true)
public Course getById(Integer id) {
Optional<Course> item = courseRepository.findById(id);
if (item.isPresent()) {
return item.get();
}
return null;
}
/**
* Save.
*
* @param item the item
* @return the course
*/
@Transactional(readOnly = false)
public Course save(Course item) {
Integer userId = item.getTeacherTutor().getId();
Optional<User> userOptional = userRepository.findById(userId);
if(userOptional.isPresent()) {
item.setTeacherTutor(userOptional.get());
}
courseRepository.save(item);
return item;
}
/**
* Delete.
*
* @param id the id
*/
@Transactional(readOnly = false)
public void delete(Integer id) {
courseRepository.deleteById(id);
}
}