2020-07-25 10:56:11 +02:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2020-08-08 14:14:42 +02:00
|
|
|
import java.util.List;
|
2020-09-21 17:45:52 +02:00
|
|
|
import java.util.Optional;
|
2020-08-08 14:14:42 +02:00
|
|
|
|
2020-08-05 21:29:48 +02:00
|
|
|
import com.plannaplan.entities.Course;
|
2020-07-25 10:56:11 +02:00
|
|
|
import com.plannaplan.repositories.CourseRepository;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2020-11-20 13:20:44 +01:00
|
|
|
/**
|
2021-01-15 15:54:17 +01:00
|
|
|
* Service of CourseService which can get(Course By Name, All Courses, Courses
|
|
|
|
* Ammount ), save, delete course.
|
2020-11-20 13:20:44 +01:00
|
|
|
*/
|
|
|
|
|
2020-07-25 10:56:11 +02:00
|
|
|
@Service
|
|
|
|
public class CourseService {
|
|
|
|
@Autowired
|
|
|
|
private CourseRepository repo;
|
2020-08-05 21:29:48 +02:00
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
2020-12-02 11:14:30 +01:00
|
|
|
* getCourseByName
|
2021-01-15 15:54:17 +01:00
|
|
|
*
|
2021-01-15 16:24:29 +01:00
|
|
|
* @param name name of course to be found
|
2021-01-15 15:54:17 +01:00
|
|
|
* @return Course By Name
|
2020-12-02 11:14:30 +01:00
|
|
|
*/
|
2020-09-21 17:45:52 +02:00
|
|
|
public Optional<Course> getCourseByName(String name) {
|
2020-08-05 21:29:48 +02:00
|
|
|
return this.repo.findByName(name);
|
|
|
|
}
|
2020-08-06 14:32:41 +02:00
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return all courses from db
|
2020-12-02 11:14:30 +01:00
|
|
|
*/
|
2020-08-08 14:14:42 +02:00
|
|
|
public List<Course> getAllCourses() {
|
|
|
|
return this.repo.findAll();
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* save to db
|
|
|
|
*
|
2020-12-02 11:14:30 +01:00
|
|
|
* @param course which course you would like to save
|
2021-01-15 15:54:17 +01:00
|
|
|
* @return Course instance with id from db
|
2020-12-02 11:14:30 +01:00
|
|
|
*/
|
2020-09-21 17:45:52 +02:00
|
|
|
public Course save(Course course) {
|
2020-08-06 14:32:41 +02:00
|
|
|
this.repo.save(course);
|
2020-09-21 17:45:52 +02:00
|
|
|
return course;
|
2020-08-06 14:32:41 +02:00
|
|
|
}
|
2020-09-04 16:24:00 +02:00
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* delete course from db
|
|
|
|
*
|
2020-12-02 11:14:30 +01:00
|
|
|
* @param course which course you would like to delete
|
|
|
|
*/
|
2020-09-21 17:45:52 +02:00
|
|
|
public void delete(Course course) {
|
2020-09-16 17:15:56 +02:00
|
|
|
this.repo.delete(course);
|
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* @return ammount of courses in db
|
2020-12-02 11:14:30 +01:00
|
|
|
*/
|
2020-09-21 17:45:52 +02:00
|
|
|
public int getCoursesAmmount() {
|
|
|
|
return (int) this.repo.count();
|
2020-09-04 16:24:00 +02:00
|
|
|
}
|
2020-12-02 11:14:30 +01:00
|
|
|
}
|