Skip to content

Commit

Permalink
refactor: 출석 정보 기능을 다른 서비스로 분리
Browse files Browse the repository at this point in the history
- AdminService로 부터 출석기록 정보가 사용되는 부분을 RecordService로 분리
  • Loading branch information
gikhoon committed Aug 1, 2024
1 parent d2a74ad commit b954494
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cotato.csquiz.api.admin.dto.MemberInfoResponse;
import org.cotato.csquiz.api.attendance.dto.AttendanceRecordResponse;
import org.cotato.csquiz.api.attendance.dto.UpdateAttendanceRequest;
import org.cotato.csquiz.api.session.dto.AddSessionRequest.AttendanceDeadLine;
import org.cotato.csquiz.common.error.ErrorCode;
import org.cotato.csquiz.common.error.exception.AppException;
import org.cotato.csquiz.domain.attendance.embedded.Location;
import org.cotato.csquiz.domain.attendance.entity.Attendance;
import org.cotato.csquiz.domain.attendance.entity.AttendanceRecord;
import org.cotato.csquiz.domain.attendance.repository.AttendanceRecordRepository;
import org.cotato.csquiz.domain.attendance.repository.AttendanceRepository;
import org.cotato.csquiz.domain.auth.service.MemberService;
import org.cotato.csquiz.domain.generation.entity.Session;
import org.cotato.csquiz.domain.generation.repository.SessionRepository;
import org.springframework.stereotype.Service;
Expand All @@ -34,9 +28,8 @@
public class AttendanceAdminService {

private final AttendanceRepository attendanceRepository;
private final AttendanceRecordRepository attendanceRecordRepository;
private final AttendanceRecordService attendanceRecordService;
private final SessionRepository sessionRepository;
private final MemberService memberService;

@Transactional
public void addAttendance(Session session, LocalDate localDate, Location location,
Expand Down Expand Up @@ -90,16 +83,7 @@ public List<AttendanceRecordResponse> findAttendanceRecords(Long generationId, I
List<Attendance> attendances = attendanceRepository.findAllBySessionIdsInQuery(sessionIds).stream()
.toList();

Map<Long, List<AttendanceRecord>> recordsByMemberId = attendanceRecordRepository.findAllByAttendanceIdsInQuery(
attendances).stream()
.collect(Collectors.groupingBy(AttendanceRecord::getMemberId));

return recordsByMemberId.entrySet().stream()
.map(entry -> {
MemberInfoResponse memberInfo = memberService.findMemberInfo(entry.getKey());
return AttendanceRecordResponse.of(memberInfo, entry.getValue(), attendances.size());
})
.toList();
return attendanceRecordService.generateAttendanceResponses(attendances);
}

private boolean checkAttendanceTimeValid(LocalTime startTime, LocalTime endTime) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.cotato.csquiz.domain.attendance.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cotato.csquiz.api.admin.dto.MemberInfoResponse;
import org.cotato.csquiz.api.attendance.dto.AttendanceRecordResponse;
import org.cotato.csquiz.domain.attendance.entity.Attendance;
import org.cotato.csquiz.domain.attendance.entity.AttendanceRecord;
import org.cotato.csquiz.domain.attendance.repository.AttendanceRecordRepository;
import org.cotato.csquiz.domain.auth.service.MemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class AttendanceRecordService {

private final AttendanceRecordRepository attendanceRecordRepository;
private final MemberService memberService;

public List<AttendanceRecordResponse> generateAttendanceResponses(List<Attendance> attendances){
Map<Long, List<AttendanceRecord>> recordsByMemberId = attendanceRecordRepository.findAllByAttendanceIdsInQuery(
attendances).stream()
.collect(Collectors.groupingBy(AttendanceRecord::getMemberId));

return recordsByMemberId.entrySet().stream()
.map(entry -> {
MemberInfoResponse memberInfo = memberService.findMemberInfo(entry.getKey());
return AttendanceRecordResponse.of(memberInfo, entry.getValue(), attendances.size());
})
.toList();
}
}

0 comments on commit b954494

Please sign in to comment.