@Transactional
public Checkin createStudentCheckin(Long studentId) {
Instant now = Instant.now();
// Prevents accidental opening of multiple check-ins
if(checkinRepo.findByStudentIdAndCheckoutTimeIsNull(studentId).isPresent()){
System.out.println("Unclosed Checkin Found for Student with ID: " + studentId);
endStudentCheckin(studentId);
}
System.out.println("Creating Checkin for Student with ID: " + studentId);
Checkin newCheckin = new Checkin();
Student existingStudent = studentRepo.findById(studentId)
.orElseThrow(() -> new EntityNotFoundException("Student not found with id: " + studentId));
newCheckin.setStudent(existingStudent);
newCheckin.setStartTime(now);
checkinRepo.save(setCheckinTypeByDayHour(now, newCheckin));
existingStudent.getCheckin().add(newCheckin);
studentRepo.save(existingStudent);
return newCheckin;
}
@Transactional
public Checkin endStudentCheckin(Long studentId) {
Instant now = Instant.now();
System.out.println("Creating Checkout for Student with ID: " + studentId);
Student existingStudent = studentRepo.findById(studentId)
.orElseThrow(() -> new EntityNotFoundException(
"Student not found with id: " + studentId));
Checkin unclosedCheckin = checkinRepo.findByStudentIdAndCheckoutTimeIsNull(studentId)
.orElseThrow(() -> new EntityNotFoundException(
"Unclosed Check-in not found for student with id: " + studentId));
unclosedCheckin.setEndTime(now);
checkinRepo.save(unclosedCheckin);
BigInteger oneHourInSeconds = BigInteger.valueOf(3600);
//Sets max checkin time to 1 hour:
if(unclosedCheckin.getTimeInClassInSeconds().compareTo(oneHourInSeconds) > 0){
unclosedCheckin.setTimeInClassInSeconds(oneHourInSeconds);
}
existingStudent.getCheckin().add(unclosedCheckin);
System.out.println("Service: Saving finished checkin with ID: " + unclosedCheckin.getId());
return unclosedCheckin;
}
Extract createStudentCheckin and endStudentCheckin
This issue rescues code from unmergable PR 465
Add the following methods to automate Checkin timestamp and duration creation:
(Code for PR 465's CheckinService available here)
Notes: