Commit 43d67ae2 authored by Kyle Muldoon's avatar Kyle Muldoon

Reworked getStudentById to return an optional. Controller now returns HTTP exceptions when needed

parent ee30cc91
package com.StudentServices.MarksService.controller;
import com.StudentServices.MarksService.exception.ResourceNotFoundException;
import com.StudentServices.MarksService.model.StudentMarks;
import com.StudentServices.MarksService.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -31,18 +32,21 @@ public class StudentMarksController {
// (GET) find studentmark by id
@GetMapping("/studentMarks/{id}")
ResponseEntity<StudentMarks> findStudentMarksById(@PathVariable String id){
StudentMarks sm = studentService.getStudentById(id);
if (sm == null){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
else {
return ResponseEntity.ok(sm);
}
ResponseEntity<StudentMarks> findStudentMarksById(@PathVariable String id) throws ResourceNotFoundException {
StudentMarks sm = studentService.getStudentById(id).
orElseThrow( () -> new ResourceNotFoundException("Student Marks not found... " + id));
return ResponseEntity.ok(sm);
}
// (PUT) update studentmark by id
// (DELETE) delete studentmark by id
// @DeleteMapping("remove/{id}")
// ResponseEntity<StudentMarks> deleteStudentMarkById(@PathVariable String id) {
// StudentMarks sm = studentService.getStudentById(id)
// }
//////////////////////////////////
......
......@@ -27,11 +27,12 @@ public class StudentService {
return studentRepository.save(studentMarks);
}
public StudentMarks getStudentById(String ID) {
Optional<StudentMarks> sm = studentRepository.findById(ID);
return sm.orElse(null);
public Optional<StudentMarks> getStudentById(String ID) {
return studentRepository.findById(ID);
}
public void updateStudent(StudentMarks studentMarks) {
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment