Skip to content

Commit

Permalink
Merge pull request #1415 from woowacourse/fix/1412-filter-popular-stu…
Browse files Browse the repository at this point in the history
…dylogs

Fix/#1412 인기 학습로그 필터링 기능 복구
  • Loading branch information
BGuga authored Jul 16, 2023
2 parents 0488681 + aac1bc4 commit a3ac396
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package wooteco.prolog.studylog.application;

import static java.util.stream.Collectors.toList;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -20,25 +28,13 @@
import wooteco.prolog.studylog.domain.repository.PopularStudylogRepository;
import wooteco.prolog.studylog.domain.repository.StudylogRepository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class PopularStudylogService {

private static final int A_WEEK = 7;
private static final int THREE_WEEK = 21;
private static final String BACKEND = "백엔드";
private static final String FRONTEND = "프론트엔드";
private static final String ANDROID = "안드로이드";

private final StudylogService studylogService;
private final StudylogRepository studylogRepository;
Expand All @@ -51,24 +47,23 @@ public void updatePopularStudylogs(Pageable pageable) {
deleteAllLegacyPopularStudylogs();

List<GroupMember> groupMembers = groupMemberRepository.findAll();
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll().stream()
.collect(Collectors.groupingBy(MemberGroup::getGroupType));
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll()
.stream().collect(Collectors.groupingBy(MemberGroup::getGroupType));

final List<Studylog> recentStudylogs = findRecentStudylogs(LocalDateTime.now(), pageable.getPageSize());
final List<Studylog> recentStudylogs = findRecentStudylogs(LocalDateTime.now(),
pageable.getPageSize());

List<Studylog> popularStudylogs = new ArrayList<>();

for (MemberGroupType groupType : MemberGroupType.values()) {
popularStudylogs.addAll(filterStudylogsByMemberGroups(recentStudylogs, new MemberGroups(memberGroupsBygroupType.get(groupType)), groupMembers)
.stream()
popularStudylogs.addAll(filterStudylogsByMemberGroups(recentStudylogs,
new MemberGroups(memberGroupsBygroupType.get(groupType)), groupMembers).stream()
.sorted(Comparator.comparing(Studylog::getPopularScore).reversed())
.limit(pageable.getPageSize())
.collect(toList()));
.limit(pageable.getPageSize()).collect(toList()));
}

popularStudylogRepository.saveAll(popularStudylogs.stream()
.map(it -> new PopularStudylog(it.getId()))
.collect(toList()));
popularStudylogRepository.saveAll(
popularStudylogs.stream().map(it -> new PopularStudylog(it.getId())).collect(toList()));
}

private void deleteAllLegacyPopularStudylogs() {
Expand All @@ -79,7 +74,8 @@ private void deleteAllLegacyPopularStudylogs() {
}
}

private List<Studylog> findRecentStudylogs(final LocalDateTime dateTime, final int minStudylogsSize) {
private List<Studylog> findRecentStudylogs(final LocalDateTime dateTime,
final int minStudylogsSize) {
int decreaseDays = A_WEEK;
List<Studylog> recentStudylogs = new ArrayList<>();
while (decreaseDays <= THREE_WEEK) {
Expand All @@ -92,61 +88,56 @@ private List<Studylog> findRecentStudylogs(final LocalDateTime dateTime, final i
return recentStudylogs;
}

private List<Studylog> filterStudylogsByMemberGroups(final List<Studylog> studylogs, final MemberGroups memberGroups, final List<GroupMember> groupMembers) {
return studylogs.stream()
.filter(studylog -> checkMemberAssignedInMemberGroups(memberGroups, studylog.getMember(),
groupMembers))
.collect(toList());
private List<Studylog> filterStudylogsByMemberGroups(final List<Studylog> studylogs,
final MemberGroups memberGroups,
final List<GroupMember> groupMembers) {
return studylogs.stream().filter(
studylog -> checkMemberAssignedInMemberGroups(memberGroups, studylog.getMember(),
groupMembers)).collect(toList());
}

private boolean checkMemberAssignedInMemberGroups(MemberGroups memberGroups,
Member member,
private boolean checkMemberAssignedInMemberGroups(MemberGroups memberGroups, Member member,
List<GroupMember> groupMembers) {
if (canJudgeMemberGroup(groupMembers, member)) {
return groupMembers.stream()
.anyMatch(memberGroups::isContainsMemberGroups);
}

return false;
}

private boolean canJudgeMemberGroup(final List<GroupMember> groupMembers, final Member member) {
return groupMembers.stream()
.anyMatch(it -> it.getMember().equals(member));
return groupMembers.stream().anyMatch(
it -> it.getMember().equals(member) && memberGroups.isContainsMemberGroups(it));
}

public PopularStudylogsResponse findPopularStudylogs(Pageable pageable,
Long memberId,
public PopularStudylogsResponse findPopularStudylogs(Pageable pageable, Long memberId,
boolean isAnonymousMember) {

List<Studylog> allPopularStudylogs = getSortedPopularStudyLogs(pageable);
List<GroupMember> groupedMembers = groupMemberRepository.findAll();
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll().stream()
.collect(Collectors.groupingBy(MemberGroup::getGroupType));
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll()
.stream().collect(Collectors.groupingBy(MemberGroup::getGroupType));

return PopularStudylogsResponse.of(
studylogsResponse(allPopularStudylogs, pageable, memberId),
studylogsResponse(filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.FRONTEND)), groupedMembers), pageable, memberId),
studylogsResponse(filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.BACKEND)), groupedMembers), pageable, memberId),
studylogsResponse(filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.ANDROID)), groupedMembers), pageable, memberId)
);
studylogsResponse(
filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.FRONTEND)), groupedMembers),
pageable,
memberId),
studylogsResponse(
filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.BACKEND)), groupedMembers),
pageable,
memberId),
studylogsResponse(
filterStudylogsByMemberGroups(allPopularStudylogs, new MemberGroups(memberGroupsBygroupType.get(MemberGroupType.ANDROID)), groupedMembers),
pageable,
memberId));
}

private List<Studylog> getSortedPopularStudyLogs(Pageable pageable) {
return studylogRepository.findAllByIdIn(getPopularStudylogIds(), pageable)
.stream()
.sorted(Comparator.comparing(Studylog::getPopularScore).reversed())
.collect(toList());
return studylogRepository.findAllByIdIn(getPopularStudylogIds(), pageable).stream()
.sorted(Comparator.comparing(Studylog::getPopularScore).reversed()).collect(toList());
}

private List<Long> getPopularStudylogIds() {
return popularStudylogRepository.findAllByDeletedFalse()
.stream()
.map(PopularStudylog::getStudylogId)
.collect(toList());
return popularStudylogRepository.findAllByDeletedFalse().stream()
.map(PopularStudylog::getStudylogId).collect(toList());
}

private StudylogsResponse studylogsResponse(final List<Studylog> studylogs, final Pageable pageable, final Long memberId) {
private StudylogsResponse studylogsResponse(final List<Studylog> studylogs,
final Pageable pageable, final Long memberId) {
final PageImpl<Studylog> page = new PageImpl<>(studylogs, pageable, studylogs.size());
if (memberId == null) {
return StudylogsResponse.of(page);
Expand Down
Loading

0 comments on commit a3ac396

Please sign in to comment.