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; package com.StudentServices.MarksService.controller;
import com.StudentServices.MarksService.exception.ResourceNotFoundException;
import com.StudentServices.MarksService.model.StudentMarks; import com.StudentServices.MarksService.model.StudentMarks;
import com.StudentServices.MarksService.service.StudentService; import com.StudentServices.MarksService.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -31,18 +32,21 @@ public class StudentMarksController { ...@@ -31,18 +32,21 @@ public class StudentMarksController {
// (GET) find studentmark by id // (GET) find studentmark by id
@GetMapping("/studentMarks/{id}") @GetMapping("/studentMarks/{id}")
ResponseEntity<StudentMarks> findStudentMarksById(@PathVariable String id){ ResponseEntity<StudentMarks> findStudentMarksById(@PathVariable String id) throws ResourceNotFoundException {
StudentMarks sm = studentService.getStudentById(id);
if (sm == null){ StudentMarks sm = studentService.getStudentById(id).
return new ResponseEntity<>(HttpStatus.NOT_FOUND); orElseThrow( () -> new ResourceNotFoundException("Student Marks not found... " + id));
} return ResponseEntity.ok(sm);
else {
return ResponseEntity.ok(sm);
}
} }
// (PUT) update studentmark by id // (PUT) update studentmark by id
// (DELETE) delete 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 { ...@@ -27,11 +27,12 @@ public class StudentService {
return studentRepository.save(studentMarks); return studentRepository.save(studentMarks);
} }
public StudentMarks getStudentById(String ID) {
Optional<StudentMarks> sm = studentRepository.findById(ID); public Optional<StudentMarks> getStudentById(String ID) {
return sm.orElse(null); return studentRepository.findById(ID);
} }
public void updateStudent(StudentMarks studentMarks) { 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