Skip to content

Commit

Permalink
Merge pull request #220 from OutDecision/issue/168
Browse files Browse the repository at this point in the history
♻️ [REFACTOR]: 사용자인증토큰 메소드 사용
  • Loading branch information
sye1101 authored May 26, 2024
2 parents ee07b4b + 0572457 commit 6d7b6e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Member> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +54,7 @@ public boolean uploadPost(UploadPostDTO request, List<String> 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())
Expand Down Expand Up @@ -106,17 +107,14 @@ public boolean uploadPost(UploadPostDTO request, List<String> optionNames, List<
return true;
}




/* 조회 */
@Override
public PostDTO viewPost(Long postId) {
Optional<Member> memberOptional = findMemberService.findLoginMember();
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));
Expand Down Expand Up @@ -169,7 +167,12 @@ public PostDTO viewPost(Long postId) {
@Override
public boolean updatePost(Long postId, UploadPostDTO request, List<String> optionNames,
List<MultipartFile> optionImages, List<String> originImages) {
Long memberId = 2024L;
Optional<Member> 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;
Expand Down Expand Up @@ -230,9 +233,15 @@ public boolean updatePost(Long postId, UploadPostDTO request, List<String> optio
/* 삭제 */
@Override
public boolean deletePost(Long postId) {
Long memberId = 2024L;
Optional<Member> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6d7b6e5

Please sign in to comment.