Skip to content

Commit

Permalink
feat: 출석 목록에 해당하는 멤버 정보 받는 방식 변경
Browse files Browse the repository at this point in the history
- 기존 멤버 하나씩 정보를 만드는 방식에서 활동 중인 회원 목록을 가져와 Map으로 만들게 변경

feat: 출석 목록에 해당하는 멤버 정보 받는 방식 변경

- 기존 멤버 하나씩 정보를 만드는 방식에서 활동 중인 회원 목록을 가져와 Map으로 만들게 변경
  • Loading branch information
gikhoon committed Aug 1, 2024
1 parent b954494 commit e2d12d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,43 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.cotato.csquiz.api.admin.dto.MemberInfoResponse;
import org.cotato.csquiz.domain.attendance.entity.AttendanceRecord;
import org.cotato.csquiz.domain.attendance.enums.AttendanceStatus;
import org.cotato.csquiz.domain.attendance.enums.AttendanceType;
import org.cotato.csquiz.domain.auth.entity.Member;
import org.cotato.csquiz.domain.auth.enums.MemberRole;


public record AttendanceRecordResponse(
MemberInfoResponse memberInfo,
AttendanceMemberInfo memberInfo,
List<AttendanceInfoResponse> attendanceInfos,
AttendanceStatistic statistic
) {
public static AttendanceRecordResponse of(MemberInfoResponse memberInfo, List<AttendanceRecord> attendanceRecords,
public static AttendanceRecordResponse of(Member member, List<AttendanceRecord> attendanceRecords,
Integer totalAttendance) {
return new AttendanceRecordResponse(
memberInfo,
AttendanceMemberInfo.from(member),
attendanceRecords.stream()
.map(AttendanceInfoResponse::from)
.toList(),
AttendanceStatistic.from(attendanceRecords, totalAttendance)
);
}

public record AttendanceMemberInfo(
Long memberId,
String memberName,
MemberRole role
){
static AttendanceMemberInfo from(Member member) {
return new AttendanceMemberInfo(
member.getId(),
member.getName(),
member.getRole()
);
}
}

public record AttendanceStatistic(
Integer onLine,
Integer offLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
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.entity.Member;
import org.cotato.csquiz.domain.auth.service.MemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,16 +24,25 @@ public class AttendanceRecordService {
private final AttendanceRecordRepository attendanceRecordRepository;
private final MemberService memberService;

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

Map<Long, Member> memberMap = memberService.findActiveMember().stream()
.collect(Collectors.toMap(Member::getId, member -> member));

return recordsByMemberId.entrySet().stream()
.map(entry -> {
MemberInfoResponse memberInfo = memberService.findMemberInfo(entry.getKey());
return AttendanceRecordResponse.of(memberInfo, entry.getValue(), attendances.size());
Member member = memberMap.get(entry.getKey());
if (member == null) {
return null;
}
return AttendanceRecordResponse.of(member, entry.getValue(),
attendances.size());
})
.filter(Objects::nonNull)
.toList();

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cotato.csquiz.domain.auth.enums;

import java.util.ArrayList;
import java.util.List;
import org.cotato.csquiz.common.error.exception.AppException;
import org.cotato.csquiz.common.error.ErrorCode;
import java.util.Arrays;
Expand Down Expand Up @@ -27,4 +29,8 @@ public static MemberRole fromKey(final String key) {
.findFirst()
.orElseThrow(() -> new AppException(ErrorCode.ENUM_NOT_RESOLVED));
}

public static List<MemberRole> activeMemberRoles() {
return List.of(ADMIN, EDUCATION, MEMBER);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cotato.csquiz.domain.auth.service;

import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cotato.csquiz.api.admin.dto.MemberInfoResponse;
Expand All @@ -13,6 +15,7 @@
import org.cotato.csquiz.common.error.exception.AppException;
import org.cotato.csquiz.common.error.exception.ImageException;
import org.cotato.csquiz.domain.auth.entity.Member;
import org.cotato.csquiz.domain.auth.enums.MemberRole;
import org.cotato.csquiz.domain.auth.repository.MemberRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -125,4 +128,8 @@ public MemberInfo getMemberInfo(Long memberId) {
.orElseThrow(() -> new EntityNotFoundException("해당 멤버를 찾을 수 없습니다."));
return MemberInfo.of(findMember, findBackFourNumber(findMember));
}

public List<Member> findActiveMember() {
return memberRepository.findAllByRoleInQuery(MemberRole.activeMemberRoles());
}
}

0 comments on commit e2d12d2

Please sign in to comment.