diff --git a/src/main/java/KGUcapstone/OutDecision/domain/comments/service/CommentsService.java b/src/main/java/KGUcapstone/OutDecision/domain/comments/service/CommentsService.java index e05074e8..66701242 100644 --- a/src/main/java/KGUcapstone/OutDecision/domain/comments/service/CommentsService.java +++ b/src/main/java/KGUcapstone/OutDecision/domain/comments/service/CommentsService.java @@ -10,6 +10,8 @@ import KGUcapstone.OutDecision.domain.user.domain.Member; import KGUcapstone.OutDecision.domain.user.repository.MemberRepository; import KGUcapstone.OutDecision.domain.user.service.FindMemberService; +import KGUcapstone.OutDecision.global.error.exception.handler.MemberHandler; +import KGUcapstone.OutDecision.global.error.status.ErrorStatus; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -41,16 +43,24 @@ public Comments save(Long id, CommentsRequestDto dto) { return comment; } else { - throw new RuntimeException("User not found"); + throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); } } @Transactional public void delete(Long postsId, Long commentsId) { + Optional memberOptional = findMemberService.findLoginMember(); + Long memberId; + // 로그인 체크 + if(memberOptional.isPresent()) memberId = memberOptional.get().getId(); + else throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); + Comments comments = commentsRepository.findByPostIdAndId(postsId, commentsId).orElseThrow(() -> new IllegalArgumentException("해당 댓글이 존재하지 않습니다. commentsId=" + commentsId)); + if (!memberId.equals(comments.getMember().getId())) throw new IllegalArgumentException("Bad Request"); + commentsRepository.delete(comments); } diff --git a/src/main/java/KGUcapstone/OutDecision/domain/post/service/PostServiceImpl.java b/src/main/java/KGUcapstone/OutDecision/domain/post/service/PostServiceImpl.java index 94e7f8a2..e846f48a 100644 --- a/src/main/java/KGUcapstone/OutDecision/domain/post/service/PostServiceImpl.java +++ b/src/main/java/KGUcapstone/OutDecision/domain/post/service/PostServiceImpl.java @@ -20,6 +20,7 @@ import KGUcapstone.OutDecision.domain.user.service.S3Service; import KGUcapstone.OutDecision.domain.vote.domain.Vote; import KGUcapstone.OutDecision.domain.vote.repository.VoteRepository; +import KGUcapstone.OutDecision.global.error.exception.handler.MemberHandler; import KGUcapstone.OutDecision.global.error.exception.handler.PostHandler; import KGUcapstone.OutDecision.global.error.status.ErrorStatus; import lombok.RequiredArgsConstructor; @@ -53,7 +54,7 @@ public boolean uploadPost(UploadPostDTO request, List optionNames, List< Member member; // 로그인 체크 if(memberOptional.isPresent()) member = memberOptional.get(); - else return false; + else throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); Post post = Post.builder() .title(request.getTitle()) @@ -106,9 +107,6 @@ public boolean uploadPost(UploadPostDTO request, List optionNames, List< return true; } - - - /* 조회 */ @Override public PostDTO viewPost(Long postId) { @@ -116,7 +114,7 @@ public PostDTO viewPost(Long postId) { Long memberId; // 로그인 체크 if(memberOptional.isPresent()) memberId = memberOptional.get().getId(); - else memberId = 0L; + else throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); Post post = postRepository.findById(postId) .orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); @@ -169,7 +167,12 @@ public PostDTO viewPost(Long postId) { @Override public boolean updatePost(Long postId, UploadPostDTO request, List optionNames, List optionImages, List originImages) { - Long memberId = 2024L; + Optional memberOptional = findMemberService.findLoginMember(); + Long memberId; + // 로그인 체크 + if(memberOptional.isPresent()) memberId = memberOptional.get().getId(); + else throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); + Post post = postRepository.findById(postId) .orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); if (!memberId.equals(post.getMember().getId())) return false; @@ -230,9 +233,15 @@ public boolean updatePost(Long postId, UploadPostDTO request, List optio /* 삭제 */ @Override public boolean deletePost(Long postId) { - Long memberId = 2024L; + Optional memberOptional = findMemberService.findLoginMember(); + Long memberId; + // 로그인 체크 + if(memberOptional.isPresent()) memberId = memberOptional.get().getId(); + else throw new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND); + Post post = postRepository.findById(postId) .orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + if (!memberId.equals(post.getMember().getId())) { return false; } diff --git a/src/main/java/KGUcapstone/OutDecision/global/common/util/JwtUtil.java b/src/main/java/KGUcapstone/OutDecision/global/common/util/JwtUtil.java index 175a2aef..6476026f 100644 --- a/src/main/java/KGUcapstone/OutDecision/global/common/util/JwtUtil.java +++ b/src/main/java/KGUcapstone/OutDecision/global/common/util/JwtUtil.java @@ -66,8 +66,8 @@ public String generateRefreshToken(String email, String role) { public String generateAccessToken(String email, String role) { - long tokenPeriod = 1000L * 60L * 30L; // 30분 -// long tokenPeriod = 1000L * 10L; +// long tokenPeriod = 1000L * 60L * 30L; // 30분 + long tokenPeriod = 1000L * 10L; Claims claims = Jwts.claims().setSubject(email); claims.put("role", role);