Class StudentController
java.lang.Object
com.markvarga21.studentmanager.controller.StudentController
@RestController
@RequestMapping("/api/v1/students")
@CrossOrigin
public class StudentController
extends Object
A controller which is used to make- create-, read-,
update- and delete students.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<StudentDto>
createStudent
(@Valid StudentDto student, String username, String roles) Saves and validates a student in the database and then returns it.org.springframework.http.ResponseEntity<StudentDto>
Deletes a student and then retrieves it.org.springframework.data.domain.Page<StudentDto>
getAllStudents
(Integer page, Integer size) Retrieves all the students from the application.org.springframework.http.ResponseEntity<StudentDto>
getStudentById
(Long id) Retrieves the desired student using its ID, then returns it.org.springframework.http.ResponseEntity<StudentDto>
updateStudentById
(@Valid StudentDto student, Long studentId) Updates a student and then retrieves it.
-
Constructor Details
-
StudentController
public StudentController()
-
-
Method Details
-
getAllStudents
@GetMapping @PreAuthorize("hasRole(\'ROLE_ADMIN\')") public org.springframework.data.domain.Page<StudentDto> getAllStudents(@RequestParam(defaultValue="0") Integer page, @RequestParam(defaultValue="10") Integer size) Retrieves all the students from the application.- Parameters:
page
- The page number.size
- The number of elements in a single page.- Returns:
- All the students stored in a
Page
.
-
createStudent
@PostMapping @PreAuthorize("hasRole(\'ROLE_USER\')") public org.springframework.http.ResponseEntity<StudentDto> createStudent(@RequestBody @Valid @Valid StudentDto student, @RequestParam(required=false) String username, @RequestParam(required=false) String roles) Saves and validates a student in the database and then returns it.- Parameters:
student
- The student itself.username
- The username of the user who created the student.roles
- The roles of the user who created the student.- Returns:
- The saved
StudentDto
.
-
getStudentById
@GetMapping("/{id}") @PreAuthorize("hasRole(\'ROLE_USER\')") public org.springframework.http.ResponseEntity<StudentDto> getStudentById(@PathVariable Long id) Retrieves the desired student using its ID, then returns it.- Parameters:
id
- The ID of the student which we want to retrieve.- Returns:
- The wanted student if present.
-
updateStudentById
@PutMapping("/{studentId}") @PreAuthorize("hasRole(\'ROLE_USER\')") public org.springframework.http.ResponseEntity<StudentDto> updateStudentById(@RequestBody @Valid @Valid StudentDto student, @PathVariable("studentId") Long studentId) Updates a student and then retrieves it.- Parameters:
student
- The student itself.studentId
- The ID of the student which has to be updated.- Returns:
- The updated
StudentDto
.
-
deleteStudentById
@DeleteMapping("/{id}") @PreAuthorize("hasRole(\'ROLE_USER\')") public org.springframework.http.ResponseEntity<StudentDto> deleteStudentById(@PathVariable Long id) Deletes a student and then retrieves it.- Parameters:
id
- The ID of the student which we want to delete.- Returns:
- The recently deleted student DTO object.
-