Skip to content

Commit 995442f

Browse files
authored
Merge pull request #282 from prgrms-web-devcourse-final-project/refactor/EA3-205-attendance
[EA3-205] refactor: 출석 기능 리팩터링
2 parents e1254ac + 835f3c1 commit 995442f

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/main/java/grep/neogulcoder/domain/attendance/controller/AttendanceController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import grep.neogulcoder.global.auth.Principal;
66
import grep.neogulcoder.global.response.ApiResponse;
77
import lombok.RequiredArgsConstructor;
8+
import org.springframework.http.ResponseEntity;
89
import org.springframework.security.core.annotation.AuthenticationPrincipal;
910
import org.springframework.web.bind.annotation.*;
1011

@@ -16,17 +17,17 @@ public class AttendanceController implements AttendanceSpecification {
1617
private final AttendanceService attendanceService;
1718

1819
@GetMapping
19-
public ApiResponse<AttendanceInfoResponse> getAttendances(@PathVariable("studyId") Long studyId,
20-
@AuthenticationPrincipal Principal userDetails) {
20+
public ResponseEntity<ApiResponse<AttendanceInfoResponse>> getAttendances(@PathVariable("studyId") Long studyId,
21+
@AuthenticationPrincipal Principal userDetails) {
2122
AttendanceInfoResponse attendances = attendanceService.getAttendances(studyId, userDetails.getUserId());
22-
return ApiResponse.success(attendances);
23+
return ResponseEntity.ok(ApiResponse.success(attendances));
2324
}
2425

2526
@PostMapping
26-
public ApiResponse<Long> createAttendance(@PathVariable("studyId") Long studyId,
27+
public ResponseEntity<ApiResponse<Long>> createAttendance(@PathVariable("studyId") Long studyId,
2728
@AuthenticationPrincipal Principal userDetails) {
2829
Long userId = userDetails.getUserId();
2930
Long id = attendanceService.createAttendance(studyId, userId);
30-
return ApiResponse.success(id);
31+
return ResponseEntity.ok(ApiResponse.success(id));
3132
}
3233
}

src/main/java/grep/neogulcoder/domain/attendance/controller/AttendanceSpecification.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import grep.neogulcoder.global.response.ApiResponse;
66
import io.swagger.v3.oas.annotations.Operation;
77
import io.swagger.v3.oas.annotations.tags.Tag;
8+
import org.springframework.http.ResponseEntity;
89

910
@Tag(name = "Attendance", description = "출석 API")
1011
public interface AttendanceSpecification {
1112

1213
@Operation(summary = "출석 조회", description = "일주일 단위로 출석을 조회합니다.")
13-
ApiResponse<AttendanceInfoResponse> getAttendances(Long studyId, Principal userDetails);
14+
ResponseEntity<ApiResponse<AttendanceInfoResponse>> getAttendances(Long studyId, Principal userDetails);
1415

1516
@Operation(summary = "출석 체크", description = "스터디에 출석을 합니다.")
16-
ApiResponse<Long> createAttendance(Long studyId, Principal userDetails);
17+
ResponseEntity<ApiResponse<Long>> createAttendance(Long studyId, Principal userDetails);
1718
}

src/main/java/grep/neogulcoder/domain/attendance/controller/dto/response/AttendanceInfoResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
public class AttendanceInfoResponse {
1111

1212
@Schema(description = "출석일 목록")
13-
private List<AttendanceResponse> attendances;
13+
private final List<AttendanceResponse> attendances;
1414

1515
@Schema(description = "출석률", example = "50")
16-
private int attendanceRate;
16+
private final int attendanceRate;
1717

1818
@Builder
1919
private AttendanceInfoResponse(List<AttendanceResponse> attendances, int attendanceRate) {

src/main/java/grep/neogulcoder/domain/attendance/controller/dto/response/AttendanceResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
public class AttendanceResponse {
1212

1313
@Schema(description = "스터디 Id", example = "1")
14-
private Long studyId;
14+
private final Long studyId;
1515

1616
@Schema(description = "유저 Id", example = "2")
17-
private Long userId;
17+
private final Long userId;
1818

1919
@Schema(description = "출석일", example = "2025-07-10")
20-
private LocalDate attendanceDate;
20+
private final LocalDate attendanceDate;
2121

2222
@Builder
2323
private AttendanceResponse(Long studyId, Long userId, LocalDate attendanceDate) {

src/main/java/grep/neogulcoder/domain/attendance/service/AttendanceService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)