package com.student.details.controllers; import com.student.details.exceptions.ResourceNotFoundException; import com.student.details.models.Student; import com.student.details.services.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; @CrossOrigin(origins = "*") @RestController @RequestMapping("/api") public class DetailsController { @Autowired StudentService studentService; @GetMapping("/students") public List getAllStudents() { // System.out.println("This is the students index route."); return studentService.findAllStudents(); } @PostMapping("/students") public Student createStudent(@RequestBody Student student) { return studentService.addStudent(student); } @GetMapping("/students/{id}") public ResponseEntity getStudentById(@PathVariable(value = "id") Long studentId) throws ResourceNotFoundException { Student student = studentService.getStudentById(studentId) .orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId)); return ResponseEntity.ok().body(student); } @GetMapping("/students/email/{email:.+}") public ResponseEntity getStudentByEmail(@PathVariable(value = "email") String email) throws ResourceNotFoundException { Student student = studentService.getStudentByEmail(email) .orElseThrow(() -> new ResourceNotFoundException("Student with email" + email + "not found.")); return ResponseEntity.ok().body(student); } @PutMapping("/students/{id}") public Student updateStudent(@PathVariable(value = "id") Long studentId, @RequestBody Student student) throws ResourceNotFoundException { Student studentToUpdate = studentService.getStudentById(studentId) .orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId)); return studentService.updateStudent(studentId, student); } @PutMapping("/students/email/{email:.+}") public Student updateStudentByEmail(@PathVariable(value = "email") String email, @RequestBody Student student) throws ResourceNotFoundException { Student studentToUpdate = studentService.getStudentByEmail(email) .orElseThrow(() -> new ResourceNotFoundException("Student with email" + email + "not found.")); return studentService.updateStudent(studentToUpdate.getId(), student); } @DeleteMapping("/students/{id}") public Map deleteStudent(@PathVariable(value = "id") Long studentId) throws ResourceNotFoundException { Student student = studentService.getStudentById(studentId) .orElseThrow(() -> new ResourceNotFoundException("Student by Id Not Found" + studentId)); return studentService.deleteStudent(studentId); } @DeleteMapping("/students/email/{email:.+}") public Map deleteStudentByEmail(@PathVariable(value = "email") String email) throws ResourceNotFoundException { Student student = studentService.getStudentByEmail(email) .orElseThrow(() -> new ResourceNotFoundException("Student with email" + email + "not found.")); return studentService.deleteStudent(student.getId()); } }