Skip to content

Commit

Permalink
Feat: 유저 프로필 비공개 시 게시물도 비공개 상태로 하는 기능 구현 (#123)
Browse files Browse the repository at this point in the history
* #122 - feat: 멤버 프로필이 private일 때 게시글도 private으로 하는 기능 구현

게시글 조회시 멤버의 프로필 상태를 확인한다.
멤버 프로필 변경 api 구현

* #122 - test: 멤버 프로필 상태에 따라 게시글 조회 기능 테스트 작성

* #122 - refactor: 오타 수정

* #122 - feat: join을 명시적으로 하게 함
  • Loading branch information
GGHDMS authored Feb 14, 2024
1 parent 5eccf74 commit 64cd19c
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public ResponseEntity<ProfileResponse> uploadProfile(
) {
return ResponseEntity.ok(ProfileResponse.of(memberService.uploadProfile(multipartFile, details.getId())));
}

@PatchMapping("/profile-status")
public ResponseEntity<Void> changeProfileStatus(
@AuthenticationPrincipal AuthDetails details
) {
memberService.changeProfileStatus(details.getId());
return ResponseEntity.ok().build();
}

}
10 changes: 9 additions & 1 deletion src/main/java/cotato/bookitlist/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ public String updateProfileLink(String url) {
return profileLink;
}

public void validatePubicProfile(Long memberId) {
public void validatePublicProfile(Long memberId) {
if (profileStatus.equals(ProfileStatus.PRIVATE) && !id.equals(memberId)) {
throw new AccessDeniedException("권한이 존재하지 않는 멤버입니다.");
}
}

public void changeProfileStatus() {
if (profileStatus.equals(ProfileStatus.PRIVATE)) {
profileStatus = ProfileStatus.PUBLIC;
} else {
profileStatus = ProfileStatus.PRIVATE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MemberDto getMemberInfo(Long memberId, Long loginMemberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new EntityNotFoundException("멤버를 찾을 수 없습니다."));

member.validatePubicProfile(loginMemberId);
member.validatePublicProfile(loginMemberId);

return MemberDto.from(member, loginMemberId);
}
Expand All @@ -37,4 +37,9 @@ public String uploadProfile(MultipartFile profile, Long memberId) {

return member.updateProfileLink(url);
}

public void changeProfileStatus(Long memberId) {
Member member = memberRepository.getReferenceById(memberId);
member.changeProfileStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

public interface PostRepository extends PostRepositoryCustom, JpaRepository<Post, Long> {

@Query("select p from Post p where p.status = 'PUBLIC'")
@Query("select p from Post p where p.status = 'PUBLIC' and p.member.profileStatus = 'PUBLIC'")
Page<Post> findPublicPostAll(Pageable pageable);

@Query("select count(p) from Post p where p.status = 'PUBLIC' and p.book.isbn13 = :isbn13")
@Query("select count(p) from Post p where p.status = 'PUBLIC' and p.book.isbn13 = :isbn13 and p.member.profileStatus = 'PUBLIC'")
int countPublicPostByBook_Isbn13(String isbn13);

Optional<Post> findByIdAndMemberId(Long postId, Long memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import cotato.bookitlist.member.domain.ProfileStatus;
import cotato.bookitlist.post.domain.PostStatus;
import cotato.bookitlist.post.dto.PostDetailDto;
import cotato.bookitlist.post.dto.PostDto;
Expand All @@ -18,6 +19,7 @@
import java.util.List;
import java.util.Optional;

import static cotato.bookitlist.member.domain.QMember.member;
import static cotato.bookitlist.post.domain.entity.QPost.post;
import static cotato.bookitlist.post.domain.entity.QPostLike.postLike;

Expand Down Expand Up @@ -48,7 +50,8 @@ public Page<PostDto> findPublicPostWithLikedByIsbn13(String isbn13, Long memberI
)
)
.from(post)
.where(post.book.isbn13.eq(isbn13), post.status.eq(PostStatus.PUBLIC))
.join(post.member, member)
.where(post.book.isbn13.eq(isbn13), post.status.eq(PostStatus.PUBLIC), post.member.profileStatus.eq(ProfileStatus.PUBLIC))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
Expand Down Expand Up @@ -86,7 +89,8 @@ public Optional<PostDetailDto> findPublicPostDetailByPostId(Long postId, Long me
)
)
.from(post)
.where(post.id.eq(postId), post.status.eq(PostStatus.PUBLIC))
.join(post.member, member)
.where(post.id.eq(postId), post.status.eq(PostStatus.PUBLIC), post.member.profileStatus.eq(ProfileStatus.PUBLIC))
.fetchOne());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -87,4 +88,17 @@ void givenPrivateMemberIdWithLogin_whenGettingMember_thenReturnErrorResponse() t
;
}

@Test
@WithCustomMockUser
@DisplayName("profileStatus를 변경하는 요청을 한다.")
void givenLoginMember_whenChangingProfileStatus_thenChangeProfileStatus() throws Exception{
//given

//when & then
mockMvc.perform(patch("/members/profile-status")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cotato.bookitlist.member.service;

import cotato.bookitlist.member.domain.Member;
import cotato.bookitlist.member.domain.ProfileStatus;
import cotato.bookitlist.member.repository.MemberRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static cotato.bookitlist.fixture.MemberFixture.createMember;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

@DisplayName("멤버 서비스 테스트")
@ExtendWith(MockitoExtension.class)
class MemberServiceTest {


@InjectMocks
MemberService sut;
@Mock
MemberRepository memberRepository;

@Test
@DisplayName("멤버 id를 이용해 멤버의 ProfileStatus를 변경한다.")
void givenMemberId_whenChangingProfileStatus_thenChangeProfileStatus() throws Exception{
//given
Long memberId = 1L;
Member member = createMember(memberId);
given(memberRepository.getReferenceById(memberId)).willReturn(member);

//when
sut.changeProfileStatus(memberId);

//then
then(memberRepository).should().getReferenceById(memberId);
assertThat(member.getProfileStatus()).isEqualTo(ProfileStatus.PRIVATE);
}


}
4 changes: 3 additions & 1 deletion src/test/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ values (1, 1, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false),
'first<============================>second<============================>third<============================>fourth',
'PUBLIC', 'TEMPLATE', 0, 0, false),
(1, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false),
(1, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false);
(1, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false),
(4, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false),
(4, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false);

INSERT INTO review (member_id, book_id, content, status, like_count, view_count, deleted)
VALUES (1, 1, 'reviewContent', 'PUBLIC', 0, 0, false),
Expand Down

0 comments on commit 64cd19c

Please sign in to comment.