Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import com.wasim.rest.api.entity.Student;
import com.wasim.rest.api.service.StudentService;
Expand All @@ -30,19 +23,24 @@ public ResponseEntity<Student> createStudent(@RequestBody Student student) {
return new ResponseEntity<Student>(studentService.createStudent(student), HttpStatus.CREATED);
}

@GetMapping("/getAllStudent")
@GetMapping("/getAllStudents")
public ResponseEntity<List<Student>> getAllStudent(){

return new ResponseEntity<List<Student>>(studentService.getAllStudent(), HttpStatus.OK);
}

@GetMapping("/getStudent/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable long id){
return new ResponseEntity<Student>(studentService.getStudentById(id), HttpStatus.OK);
}

@PutMapping("/editStudent/{id}")
public ResponseEntity<Student> updateStudent(@PathVariable("id") long id, @RequestBody Student student){
public ResponseEntity<Student> updateStudent(@PathVariable long id, @RequestBody Student student){
return new ResponseEntity<Student>(studentService.updateStudent(id, student), HttpStatus.OK);
}

@DeleteMapping("/deleteStudent/{id}")
public ResponseEntity<HttpStatus> deleteStudent(@PathVariable("id") long id){
public ResponseEntity<HttpStatus> deleteStudent(@PathVariable long id){
studentService.deleteStudent(id);
return new ResponseEntity<HttpStatus>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@ public class Student {
private String name;
private String email;
private String address;








}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.wasim.rest.api.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(StudentNotFoundException.class)
public ProblemDetail handleStudentNotFoundException(StudentNotFoundException e){
ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
problem.setTitle("Student not found.");
problem.setProperty("Student id: ", e.getStudentId());
return problem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wasim.rest.api.exception;

public class StudentNotFoundException extends RuntimeException{
private final Long studentId;
public StudentNotFoundException(Long studentId){
super("Student with ID " + studentId + " was not found.");
this.studentId = studentId;
}
public Long getStudentId() {
return studentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface StudentRepository extends JpaRepository<Student, Long>{

Student getStudentById(long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
public interface StudentService {

public Student createStudent(Student student);

public List<Student> getAllStudent();


public Student getStudentById(long id);

public Student updateStudent(long id, Student student);

public void deleteStudent(long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.wasim.rest.api.exception.StudentNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -25,6 +26,14 @@ public List<Student> getAllStudent() {
return studentRepository.findAll();
}

@Override
public Student getStudentById(long id){
if(!studentRepository.existsById(id)){
throw new StudentNotFoundException(id);
}
return studentRepository.getStudentById(id);
}

@Override
public Student updateStudent(long id, Student student) {
student.setId(id);
Expand Down