SubjectService.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.SubjectsRepository;
import org.uoc.tfg.sel.repository.model.Subject;

/**
 * The Class SubjectService.
 * 
 * @author Eduardo Rodriguez Carro
 */
@Service
public class SubjectService {

	/** The subjects repository. */
	@Autowired
	private SubjectsRepository subjectsRepository;
	
	/**
	 * Gets the all.
	 *
	 * @return the all
	 */
	@Transactional(readOnly = true)
	public List<Subject> getAll(){
		 return ServiceUtils.iterableToList(subjectsRepository.findAll());
	}

	/**
	 * Gets the by id.
	 *
	 * @param id the id
	 * @return the by id
	 */
	@Transactional(readOnly = true)
	public Subject getById(Integer id) {
		Optional<Subject> item = subjectsRepository.findById(id);
		if (item.isPresent()) {
			return item.get();
		}
		return null;
	}
	
	/**
	 * Save.
	 *
	 * @param item the item
	 * @return the subject
	 */
	@Transactional(readOnly = false)
	public Subject save(Subject item) {
		subjectsRepository.save(item);
		return item;

	}
	
	/**
	 * Delete.
	 *
	 * @param id the id
	 */
	@Transactional(readOnly = false)
	public void delete(Integer id) {
		subjectsRepository.deleteById(id);
	}
}