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

[BE] 같은 스터디 진행 이후 다른 멤버 스터디 기록이 조회되지 않는 오류 수정 #758

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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,6 +1,7 @@
package harustudy.backend.content.service;

import harustudy.backend.auth.dto.AuthMember;
import harustudy.backend.auth.exception.AuthorizationException;
import harustudy.backend.content.domain.Content;
import harustudy.backend.content.dto.ContentResponse;
import harustudy.backend.content.dto.ContentsResponse;
Expand All @@ -11,6 +12,7 @@
import harustudy.backend.member.domain.Member;
import harustudy.backend.member.repository.MemberRepository;
import harustudy.backend.participant.domain.Participant;
import harustudy.backend.participant.exception.ParticipantNotFoundException;
import harustudy.backend.participant.repository.ParticipantRepository;
import harustudy.backend.study.domain.Study;
import harustudy.backend.study.repository.StudyRepository;
Expand All @@ -33,16 +35,34 @@ public class ContentService {
@Transactional(readOnly = true)
public ContentsResponse findContentsWithFilter(AuthMember authMember, Long studyId,
Long participantId, Integer cycle) {
Participant participant = participantRepository.findByIdIfExists(participantId);
Study study = studyRepository.findByIdIfExists(studyId);
List<Participant> participants = participantRepository.findByStudy(study);
Member member = memberRepository.findByIdIfExists(authMember.id());

participant.validateIsCreatedByMember(member);
participant.validateIsBelongsTo(study);
validateMemberIncludedIn(participants, member);
Participant participant = findParticipantById(participants, participantId);

return getContentsResponseByCycleFilter(cycle, participant);
}

private void validateMemberIncludedIn(List<Participant> participants, Member member) {
if (isMemberIncludedInParticipants(member, participants)) {
throw new AuthorizationException();
}
}

private boolean isMemberIncludedInParticipants(Member member, List<Participant> participants) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

멤버가 참여자가 아니라면 true를 반환하는 메소드네요!

메소드명이 isMemberNotIncludedInParticipants여야 할 것 같은데, 아마 오타가 난 것 같습니다!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이래서 코드리뷰 하는거죠 ㅎㅎ 감사합니다 테오~ 😄

return participants.stream()
.noneMatch(participant -> participant.isCreatedBy(member));
}

private Participant findParticipantById(List<Participant> participants, Long participantId) {
return participants.stream()
.filter(participant -> participant.isSameId(participantId))
.findFirst()
.orElseThrow(ParticipantNotFoundException::new);
}

private ContentsResponse getContentsResponseByCycleFilter(Integer cycle, Participant participant) {
List<Content> contents = participant.getContents();
if (Objects.isNull(cycle)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ public void generateContents(int totalCycle) {
}
}

public boolean isSameId(Long id) {
return this.id.equals(id);
}

public boolean isParticipantOf(Study study) {
return this.study.getId().equals(study.getId());
}

public boolean isNotCreatedBy(Member member) {
return !this.member.getId().equals(member.getId());
public boolean isCreatedBy(Member member) {
return this.member.getId().equals(member.getId());
}

public boolean hasSameNicknameWith(Participant participant) {
return this.nickname.equals(participant.nickname);
}

public boolean isNotIncludedIn(Study other) {
return !study.getId().equals(other.getId());
}

public void validateIsHost() {
if (!isHost) {
throw new ParticipantIsNotHostException();
Expand All @@ -105,7 +105,7 @@ public void validateIsBelongsTo(Study study) {
}

public void validateIsCreatedByMember(Member member) {
if (isNotCreatedBy(member)) {
if (!isCreatedBy(member)) {
throw new AuthorizationException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import harustudy.backend.auth.dto.AuthMember;
import harustudy.backend.auth.exception.AuthorizationException;
import harustudy.backend.content.domain.Content;
import harustudy.backend.content.dto.ContentResponse;
import harustudy.backend.content.dto.ContentsResponse;
Expand Down Expand Up @@ -45,19 +46,21 @@ class ContentServiceTest {

private Study study;
private Member member;
private Member member2;
private Participant participant;
private Content content;

@BeforeEach
void setUp() {
study = new Study("studyName", 1, 20);
member = new Member("nickname", "email", "imageUrl", LoginType.GUEST);
member2 = new Member("nickname2", "email2", "imageUrl2", LoginType.GUEST);
participant = Participant.createParticipantOfStudy(study, member, "nickname");

content = new Content(participant, 1);

entityManager.persist(study);
entityManager.persist(member);
entityManager.persist(member2);
entityManager.persist(participant);
entityManager.persist(content);

Expand Down Expand Up @@ -224,6 +227,26 @@ void setUp() {
assertThat(content).isEqualTo(expectedContentResponses);
}

@Test
void 스터디원은_같은_스터디_내_다른_멤버의_콘텐츠를_조회할_수_있다() {
// given
AuthMember authMember = new AuthMember(member.getId());
Participant participant2 = Participant.createParticipantOfStudy(study, member2, "nickname2");
Content contentOfMember2 = new Content(participant2, 1);

entityManager.persist(participant2);
entityManager.persist(contentOfMember2);

EntityManagerUtil.flushAndClearContext(entityManager);

// when
ContentsResponse contentsWithFilter = contentService.findContentsWithFilter(authMember,
study.getId(), participant2.getId(), null);

// then
assertThat(contentsWithFilter.content().size()).isEqualTo(1);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트까지.. 💯

@Test
void 스터디에_참여한_특정_스터디원의_콘텐츠를_조회시_스터디가_없으면_예외를_던진다() {
// given
Expand All @@ -247,4 +270,14 @@ void setUp() {
study.getId(), 999L, null))
.isInstanceOf(ParticipantNotFoundException.class);
}

@Test
void 같은_스터디에_참여하지_않은_멤버가_다른_스터디_멤버의_콘텐츠를_조회하면_예외를_던진다() {
// given
AuthMember authMember = new AuthMember(member2.getId());

// when, then
assertThatThrownBy(() -> contentService.findContentsWithFilter(authMember,
study.getId(), participant.getId(), null)).isInstanceOf(AuthorizationException.class);
}
}
Loading