Class StudentService

java.lang.Object
org.codewithmagret.rest.student.StudentService

@Service public class StudentService extends Object
Service class for handling business logic related to Student entities.
  • Constructor Details

    • StudentService

      public StudentService()
      Default constructor for StudentService.
  • Method Details

    • getStudents

      public List<Student> getStudents()
      Retrieves all students from the repository.
      Returns:
      a list of all students
    • getStudentById

      public Student getStudentById(Long id)
      Retrieves a student by its ID.
      Parameters:
      id - the ID of the student to retrieve
      Returns:
      the Student object if found, or null if not found
    • addStudent

      public Student addStudent(Student student)
      Adds a new student to the repository.
      Parameters:
      student - the Student object to add
      Returns:
      the saved Student object
    • updateStudent

      public Student updateStudent(Long id, Student studentToUpdate)
      Updates an existing student in the repository.
      Parameters:
      id - the ID of the student to update
      studentToUpdate - the Student object containing the updated data
      Returns:
      the updated Student object if found, or null if not found
    • deleteStudent

      public boolean deleteStudent(Long id)
      Deletes a student by its ID.
      Parameters:
      id - the ID of the student to delete
      Returns:
      true if the student was deleted, false if the student was not found
    • enrollStudentInCourse

      public Student enrollStudentInCourse(Long studentId, Long courseId)
      Enrolls a student in a course.
      Parameters:
      studentId - the ID of the student to enroll
      courseId - the ID of the course to enroll the student in
      Returns:
      the updated Student object if both the student and course were found and enrollment was successful, or null if either was not found
    • unenrollStudentFromCourse

      public Student unenrollStudentFromCourse(Long studentId, Long courseId)
      Unenrolls a student from a course.
      Parameters:
      studentId - the ID of the student to unenroll
      courseId - the ID of the course to unenroll the student from
      Returns:
      the updated Student object if both the student and course were found and unenrollment was successful, or null if either was not found