@@ -32,8 +32,7 @@ public class AttendanceService {
3232 private final StudyMemberRepository studyMemberRepository ;
3333
3434 public AttendanceInfoResponse getAttendances (Long studyId , Long userId ) {
35- Study study = studyRepository .findByIdAndActivatedTrue (studyId )
36- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
35+ Study study = getStudyById (studyId );
3736
3837 List <Attendance > attendances = attendanceRepository .findByStudyIdAndUserId (studyId , userId );
3938 List <AttendanceResponse > responses = attendances .stream ()
@@ -47,23 +46,26 @@ public AttendanceInfoResponse getAttendances(Long studyId, Long userId) {
4746
4847 @ Transactional
4948 public Long createAttendance (Long studyId , Long userId ) {
50- Study study = studyRepository .findByIdAndActivatedTrue (studyId )
51- .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
52-
49+ getStudyById (studyId );
5350 validateNotAlreadyChecked (studyId , userId );
5451
5552 Attendance attendance = Attendance .create (studyId , userId );
5653 attendanceRepository .save (attendance );
5754 return attendance .getId ();
5855 }
5956
57+ private Study getStudyById (Long studyId ) {
58+ return studyRepository .findById (studyId )
59+ .orElseThrow (() -> new NotFoundException (STUDY_NOT_FOUND ));
60+ }
61+
6062 private int getAttendanceRate (Long studyId , Long userId , Study study , List <AttendanceResponse > responses ) {
61- LocalDate start = study .getStartDate ().toLocalDate ();
62- LocalDate participated = studyMemberRepository .findCreatedDateByStudyIdAndUserId (studyId , userId ).toLocalDate ();
63- LocalDate attendanceStart = start .isAfter (participated ) ? start : participated ;
64- LocalDate end = study .getEndDate ().toLocalDate ();
63+ LocalDate startDay = study .getStartDate ().toLocalDate ();
64+ LocalDate participatedDay = studyMemberRepository .findCreatedDateByStudyIdAndUserId (studyId , userId ).toLocalDate ();
65+ LocalDate attendanceStartDay = startDay .isAfter (participatedDay ) ? startDay : participatedDay ;
66+ LocalDate endDay = study .getEndDate ().toLocalDate ();
6567
66- int totalDays = (int ) ChronoUnit .DAYS .between (attendanceStart , end ) + 1 ;
68+ int totalDays = (int ) ChronoUnit .DAYS .between (attendanceStartDay , endDay ) + 1 ;
6769 int attendDays = responses .size ();
6870 int attendanceRate = totalDays == 0 ? 0 : Math .round (((float ) attendDays / totalDays ) * 100 );
6971
0 commit comments