Class StudentController
java.lang.Object
org.codewithmagret.rest.student.StudentController
REST controller for managing students and their course enrollments.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<Student> addStudent(Student student) Creates a new student.org.springframework.http.ResponseEntity<Void> deleteStudent(Long id) Deletes a student by its ID.org.springframework.http.ResponseEntity<Student> enrollStudent(Long studentId, Long courseId) Enrolls a student in a course.org.springframework.http.ResponseEntity<Student> getStudentById(Long id) Retrieves a student by its ID.Retrieves a list of all students.org.springframework.http.ResponseEntity<Student> removeEnrollment(Long studentId, Long courseId) Unenrolls a student from a course.org.springframework.http.ResponseEntity<Student> updateStudent(Long id, Student updated) Updates an existing student.
-
Constructor Details
-
StudentController
public StudentController()Default constructor for StudentController.
-
-
Method Details
-
getStudents
-
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
-
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.
-