Commit f4bbdcc8 authored by Kevin Kaminski's avatar Kevin Kaminski

Add ResourceNotFoundError to updateAttendance

parent fe731b82
......@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
@CrossOrigin(origins = "*")
@RestController
......@@ -41,8 +42,17 @@ public class AttendanceController {
@PutMapping("/attendance/{id}")
public ResponseEntity<Attendance> updateAttendance(@PathVariable(value = "id") Long attendanceId,
@Valid @RequestBody Attendance attendanceDetails) {
Attendance updatedAttendance = attendanceService.updateAttendance(attendanceId, attendanceDetails);
@Valid @RequestBody Attendance attendanceDetails)
throws ResourceNotFoundException {
Optional<Attendance> attendanceOptional = attendanceService.getAttendanceById(attendanceId);
if (attendanceOptional.isPresent()) {
Attendance attendance = attendanceOptional.get();
} else {
throw new ResourceNotFoundException("Attendance record not found." + attendanceId);
}
Attendance updatedAttendance = attendanceService.updateAttendance(attendanceOptional.get(), attendanceDetails);
return ResponseEntity.ok(updatedAttendance);
}
}
......@@ -32,14 +32,9 @@ public class AttendanceService {
return attendanceRepository.save(attendance);
}
public Attendance updateAttendance(Long attendanceId, Attendance attendanceDetails) {
Optional<Attendance> attendanceOptional = getAttendanceById(attendanceId);
if (attendanceOptional.isPresent()) {
Attendance attendance = attendanceOptional.get();
public Attendance updateAttendance(Attendance attendance, Attendance attendanceDetails) {
attendance.setAttendanceDate(attendanceDetails.getAttendanceDate());
attendance.setDidAttend(attendanceDetails.getDidAttend());
return attendanceRepository.save(attendance);
}
return null;
}
}
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