Class StudentController

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

@RestController @RequestMapping("/api/v1/students") public class StudentController extends Object
REST controller for managing students and their course enrollments.
  • Constructor Details

    • StudentController

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

    • getStudents

      @GetMapping public List<Student> getStudents()
      Retrieves a list of all students.
      Returns:
      A list of Student objects.
    • getStudentById

      @GetMapping("/{id}") public org.springframework.http.ResponseEntity<Student> getStudentById(@PathVariable Long id)
      Retrieves a student by its ID.
      Parameters:
      id - The ID of the student to retrieve.
      Returns:
      A ResponseEntity containing the Student object if found, or a 404 Not Found status if not found.
    • addStudent

      @PostMapping public org.springframework.http.ResponseEntity<Student> addStudent(@RequestBody Student student)
      Creates a new student.
      Parameters:
      student - The Student object to create.
      Returns:
      A ResponseEntity containing the created Student object.
    • updateStudent

      @PutMapping("/{id}") public org.springframework.http.ResponseEntity<Student> updateStudent(@PathVariable Long id, @RequestBody Student updated)
      Updates an existing student.
      Parameters:
      id - The ID of the student to update.
      updated - The Student object containing the updated data.
      Returns:
      A ResponseEntity containing the updated Student object if found, or a 404 Not Found status if not found.
    • deleteStudent

      @DeleteMapping("/{id}") public org.springframework.http.ResponseEntity<Void> deleteStudent(@PathVariable Long id)
      Deletes a student by its ID.
      Parameters:
      id - The ID of the student to delete.
      Returns:
      A ResponseEntity with a 204 No Content status if the deletion was successful, or a 404 Not Found status if the student was not found.
    • enrollStudent

      @PostMapping("/{studentId}/enroll/{courseId}") public org.springframework.http.ResponseEntity<Student> enrollStudent(@PathVariable Long studentId, @PathVariable 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:
      A ResponseEntity containing the updated Student object if the enrollment was successful, or a 400 Bad Request status if the enrollment failed.
    • removeEnrollment

      @DeleteMapping("/{studentId}/unenroll/{courseId}") public org.springframework.http.ResponseEntity<Student> removeEnrollment(@PathVariable Long studentId, @PathVariable 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:
      A ResponseEntity containing the updated Student object if the unenrollment was successful, or a 400 Bad Request status if the unenrollment failed.