Skip to content

Commit

Permalink
Merge pull request #106 from DEPthes/feat/#73-exit-member
Browse files Browse the repository at this point in the history
[FEAT]: 회원 탈퇴 API 및 게시글 상세 조회 null 체크
  • Loading branch information
phonil authored Aug 18, 2024
2 parents 9d0192e + 01a2054 commit 3d2b7b1
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface AuthService {

SuccessResponse<Message> logout(UserDetailsImpl userDetails, LogoutReq logoutReq);

SuccessResponse<Message> exit(UserDetailsImpl userDetails);

SuccessResponse<ReissueRes> reissue(String refreshToken);

SuccessResponse<EmailDuplicateCheckRes> checkEmailDuplicate(String email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
import mvp.deplog.domain.auth.dto.response.LoginRes;
import mvp.deplog.domain.auth.dto.request.JoinReq;
import mvp.deplog.domain.auth.dto.response.ReissueRes;
import mvp.deplog.domain.comment.domain.Comment;
import mvp.deplog.domain.comment.domain.repository.CommentRepository;
import mvp.deplog.domain.likes.domain.Likes;
import mvp.deplog.domain.likes.domain.repository.LikesRepository;
import mvp.deplog.domain.post.domain.Post;
import mvp.deplog.domain.post.domain.repository.PostRepository;
import mvp.deplog.domain.scrap.domain.Scrap;
import mvp.deplog.domain.scrap.domain.repository.ScrapRepository;
import mvp.deplog.domain.tag.domain.Tag;
import mvp.deplog.domain.tag.domain.repository.TagRepository;
import mvp.deplog.domain.tagging.Tagging;
import mvp.deplog.domain.tagging.repository.TaggingRepository;
import mvp.deplog.global.common.Message;
import mvp.deplog.global.common.SuccessResponse;
import mvp.deplog.domain.member.domain.Member;
Expand All @@ -32,6 +44,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
Expand All @@ -50,6 +63,13 @@ public class AuthServiceImpl implements AuthService{
private final MemberAuthMapper memberAuthMapper;

private final MemberRepository memberRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final LikesRepository likesRepository;
private final ScrapRepository scrapRepository;
private final TaggingRepository taggingRepository;
private final TagRepository tagRepository;

private final PasswordEncoder passwordEncoder;
private final JwtTokenProvider jwtTokenProvider;

Expand Down Expand Up @@ -124,6 +144,76 @@ public SuccessResponse<Message> logout(UserDetailsImpl userDetails, LogoutReq lo
return SuccessResponse.of(message);
}

@Override
@Transactional
public SuccessResponse<Message> exit(UserDetailsImpl userDetails) {
Member member = memberRepository.findById(userDetails.getMember().getId())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 회원입니다."));

// method : handle likes - delete likes
List<Likes> likesList = likesRepository.findAllByMember(member);
for (Likes likes : likesList) {
Post post = likes.getPost();
if(post.getMember().equals(member))
continue;
post.decrementLikesCount();
}
likesRepository.deleteAll(likesList);

// method : handle scrap - delete scrap
List<Scrap> scrapList = scrapRepository.findAllByMember(member);
for (Scrap scrap : scrapList) {
Post post = scrap.getPost();
if (post.getMember().equals(member))
continue;
post.decrementScrapCount();
}
scrapRepository.deleteAll(scrapList);

// method : handle comment - delete comment
List<Comment> commentList = commentRepository.findByMember(member);
for (Comment comment : commentList) {
if (comment.getParentComment() == null) {
List<Comment> childCommentList = commentRepository.findByParentComment(comment);
commentRepository.deleteAll(childCommentList);
}
}
commentRepository.deleteAll(commentList);

// method : handle tag - control tag && delete post
List<Post> postList = postRepository.findByMember(member);
for (Post post : postList) {
List<Tagging> taggingList = taggingRepository.findByPost(post);
for (Tagging tagging : taggingList) {
Tag tag = tagging.getTag();
Long taggingCount = taggingRepository.countByTag(tag);
taggingRepository.delete(tagging);
if(taggingCount == 1)
tagRepository.delete(tag);
}

List<Comment> postCommentList = commentRepository.findByPost(post);
for (Comment comment : postCommentList) {
if (comment.getParentComment() == null) {
List<Comment> childCommentList = commentRepository.findByParentComment(comment);
commentRepository.deleteAll(childCommentList);
}
}
commentRepository.deleteAll(postCommentList);
}

postRepository.deleteAll(postList);

// delete member
memberRepository.delete(member);

Message message = Message.builder()
.message("회원 탈퇴가 완료되었습니다.")
.build();

return SuccessResponse.of(message);
}

@Override
public SuccessResponse<ReissueRes> reissue(String refreshToken) {
if (!jwtTokenProvider.isTokenValid(refreshToken))
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mvp/deplog/domain/auth/presentation/AuthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ ResponseEntity<SuccessResponse<Message>> logout(
@Parameter(description = "Schemas의 LogoutReq를 참고해주세요.", required = true) @RequestBody LogoutReq logoutReq
);

@Operation(summary = "회원 탈퇴 API", description = "회원 탈퇴를 진행합니다.")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200", description = "회원 탈퇴 성공",
content = {@Content(mediaType = "application/json", schema = @Schema(implementation = Message.class))}
),
@ApiResponse(
responseCode = "400", description = "회원 탈퇴 실패",
content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}
)
})
@DeleteMapping(value = "/exit")
ResponseEntity<SuccessResponse<Message>> exit(
@Parameter(description = "Access Token을 입력해주세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails
);

@Operation(summary = "토큰 재발급 API", description = "리프레시 토큰으로 액세스 토큰 재발급을 진행합니다.")
@ApiResponses(value = {
@ApiResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public ResponseEntity<SuccessResponse<Message>> logout(
return ResponseEntity.ok(authService.logout(userDetails, logoutReq));
}

@Override
@DeleteMapping(value = "/exit")
public ResponseEntity<SuccessResponse<Message>> exit(@AuthenticationPrincipal UserDetailsImpl userDetails) {
return ResponseEntity.ok(authService.exit(userDetails));
}

@Override
@GetMapping(value = "/reissue")
public ResponseEntity<SuccessResponse<ReissueRes>> reissue(@RequestParam(value = "refreshToken") String refreshToken) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package mvp.deplog.domain.comment.domain.repository;

import mvp.deplog.domain.comment.domain.Comment;
import mvp.deplog.domain.member.domain.Member;
import mvp.deplog.domain.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

// 댓글 목록 조히
// 댓글 목록 조회
List<Comment> findByPostAndParentCommentIsNull(Post post);

List<Comment> findByMember(Member member);

List<Comment> findByParentComment(Comment parentComment);
// 대댓글 목록 조회
List<Comment> findByPostAndParentCommentIsNotNull(Post post);

Expand All @@ -19,4 +23,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

// 댓글 삭제
void deleteByPostAndParentCommentIsNull(Post post);

List<Comment> findByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mvp.deplog.domain.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface LikesRepository extends JpaRepository<Likes, Long> {
Expand All @@ -13,5 +14,7 @@ public interface LikesRepository extends JpaRepository<Likes, Long> {

Optional<Likes> findByMemberAndPost(Member member, Post post);

List<Likes> findAllByMember(Member member);

void deleteByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MemberPostDetailServiceImpl implements PostDetailService<MemberPost

@Override
public boolean supports(UserDetailsImpl userDetails) {
return userDetails.getMember().getRole().equals(Role.MEMBER) || userDetails.getMember().getRole().equals(Role.ADMIN);
return userDetails != null && (userDetails.getMember().getRole().equals(Role.MEMBER) || userDetails.getMember().getRole().equals(Role.ADMIN));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ Page<Post> findByTitleContainingOrSearchContentContaining(@Param("titleSearchWor
Pageable pageable);

List<Post> findByMemberAndStageOrderByCreatedDateDesc(Member member, Stage stage);

List<Post> findByMember(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface ScrapRepository extends JpaRepository<Scrap, Long> {
Expand All @@ -17,5 +18,7 @@ public interface ScrapRepository extends JpaRepository<Scrap, Long> {

Page<Scrap> findByMember(Member member, Pageable pageable);

List<Scrap> findAllByMember(Member member);

void deleteByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public interface TaggingRepository extends JpaRepository<Tagging, Long> {
Page<Tagging> findByTag(Tag tag, Pageable pageable);

void deleteByPost(Post post);

List<Tagging> findAllByTag(Tag tag);

Long countByTag(Tag tag);
}

0 comments on commit 3d2b7b1

Please sign in to comment.