Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#1412 인기 학습로그 필터링 복구 재시도 #1414

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package wooteco.prolog.roadmap.application;

import java.util.List;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.roadmap.application.dto.EssayAnswerRequest;
import wooteco.prolog.roadmap.application.dto.EssayAnswerUpdateRequest;
import wooteco.prolog.roadmap.domain.EssayAnswer;
import wooteco.prolog.roadmap.domain.Quiz;
import wooteco.prolog.roadmap.domain.repository.EssayAnswerRepository;
import wooteco.prolog.roadmap.domain.repository.QuizRepository;

import javax.persistence.PersistenceContext;
import java.util.List;

@Transactional
@Service
Expand Down Expand Up @@ -46,11 +46,11 @@ public Long createEssayAnswer(EssayAnswerRequest essayAnswerRequest, Long member
}

@Transactional
public void updateEssayAnswer(Long answerId, String answer, Long memberId) {
public void updateEssayAnswer(Long answerId, EssayAnswerUpdateRequest request, Long memberId) {
EssayAnswer essayAnswer = getById(answerId);
Member member = memberService.findById(memberId);

essayAnswer.update(answer, member);
essayAnswer.update(request.getAnswer(), member);
essayAnswerRepository.save(essayAnswer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public ResponseEntity<EssayAnswerResponse> findById(@PathVariable Long essayAnsw
public ResponseEntity<Void> updateById(@PathVariable Long essayAnswerId,
@AuthMemberPrincipal LoginMember member,
@RequestBody EssayAnswerUpdateRequest request) {
essayAnswerService.updateEssayAnswer(essayAnswerId, request.getAnswer(), member.getId());
essayAnswerService.updateEssayAnswer(essayAnswerId, request, member.getId());
return ResponseEntity.ok().build();
}

@DeleteMapping("/essay-answers/{essayAnswerId}")
public ResponseEntity<Void> deleteEssayAnswerById(@PathVariable Long essayAnswerId,
@AuthMemberPrincipal LoginMember member) {
Expand All @@ -61,7 +61,7 @@ public ResponseEntity<Void> deleteEssayAnswerById(@PathVariable Long essayAnswer
}

@GetMapping("/quizzes/{quizId}")
public ResponseEntity<QuizResponse> findEssayAnswerById(@PathVariable Long quizId) {
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId) {
return ResponseEntity.ok(quizService.findById(quizId));
}

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.member.exception.MemberNotFoundException;
import wooteco.prolog.roadmap.application.dto.EssayAnswerRequest;
import wooteco.prolog.roadmap.application.dto.EssayAnswerUpdateRequest;
import wooteco.prolog.roadmap.domain.EssayAnswer;
import wooteco.prolog.roadmap.domain.Quiz;
import wooteco.prolog.roadmap.domain.repository.EssayAnswerRepository;
Expand All @@ -22,10 +23,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class EssayAnswerServiceTest {
Expand Down Expand Up @@ -77,7 +75,7 @@ void updateEssayAnswer() {
.thenReturn(new Member(null, null, null, 1L, null));

//when
essayAnswerService.updateEssayAnswer(1L, "answer", 1L);
essayAnswerService.updateEssayAnswer(1L, new EssayAnswerUpdateRequest("answer"), 1L);

//then
verify(essayAnswerRepository, times(1)).save(any());
Expand Down
Loading