@RequiredArgsConstructor
@Service
public class RecommentService {
private final RecommentRepository recommentRepository;
private final TokenProvider tokenProvider;
private final PostService postService;
private final CommentService commentService;
// 대댓글 생성
@Transactional
public ResponseDto<?> createrecomment(RecommentRequestDto requestDto,
HttpServletRequest request) {
if (null == request.getHeader("Refresh-Token")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
if (null == request.getHeader("Authorization")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
Member member = validateMember(request);
if (null == member) {
return ResponseDto.fail("INVALID_TOKEN", "Token이 유효하지 않습니다.");
}
Comment comment = commentService.isPresentComment(requestDto.getCommentId());
if (null == comment) {
return ResponseDto.fail("NOT_FOUND", "존재하지 않는 게시글 id 입니다.");
}
// 대댓글이지만 게시판 번호 불러오기.
Post post = comment.getPost();
Recomment recomment = Recomment.builder()
.member(member)
.comment(comment)
.post(post)
.content(requestDto.getContent())
.build();
recommentRepository.save(recomment);
// List<recomment> recommentList = new ArrayList<>();
// recommentList.add(recomment);
//
// Comment comment1 = new Comment(recommentList);
return ResponseDto.success(
RecommentResponseDto.builder()
.id(recomment.getId())
.commentId(comment.getId())
.author(recomment.getMember().getNickname())
.content(recomment.getContent())
.likes(0)
.createdAt(recomment.getCreatedAt())
.modifiedAt(recomment.getModifiedAt())
.build()
);
}
// 대댓글 수정
@Transactional
public ResponseDto<?> updaterecomment(Long id,
RecommentRequestDto requestDto,
HttpServletRequest request) {
if (null == request.getHeader("Refresh-Token")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
if (null == request.getHeader("Authorization")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
Member member = validateMember(request);
if (null == member) {
return ResponseDto.fail("INVALID_TOKEN", "Token이 유효하지 않습니다.");
}
Recomment recomment = isPresentrecomment(id);
if (null == recomment) {
return ResponseDto.fail("NOT_FOUND", "존재하지 않는 대댓글 id 입니다.");
}
if (recomment.validateMember(member)) {
return ResponseDto.fail("BAD_REQUEST", "작성자만 수정할 수 있습니다.");
}
recomment.update(requestDto);
recommentRepository.save(recomment);
return ResponseDto.success(
RecommentResponseDto.builder()
.id(recomment.getId())
.commentId(recomment.getComment().getId())
.author(recomment.getMember().getNickname())
.content(recomment.getContent())
.likes(recomment.getLikes_count())
.createdAt(recomment.getCreatedAt())
.modifiedAt(recomment.getModifiedAt())
.build()
);
}
@Transactional
public ResponseDto<?> deleterecomment(Long id, HttpServletRequest request) {
if (null == request.getHeader("Refresh-Token")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
if (null == request.getHeader("Authorization")) {
return ResponseDto.fail("MEMBER_NOT_FOUND",
"로그인이 필요합니다.");
}
Member member = validateMember(request);
if (null == member) {
return ResponseDto.fail("INVALID_TOKEN", "Token이 유효하지 않습니다.");
}
Recomment recomment = isPresentrecomment(id);
if (null == recomment) {
return ResponseDto.fail("NOT_FOUND", "존재하지 않는 댓글 id 입니다.");
}
if (recomment.validateMember(member)) {
return ResponseDto.fail("BAD_REQUEST", "작성자만 수정할 수 있습니다.");
}
recommentRepository.delete(recomment);
return ResponseDto.success("success");
}
//===============아래 부터는 메서드=================
@Transactional(readOnly = true)
public Recomment isPresentrecomment(Long id) {
Optional<Recomment> optionalrecomment = recommentRepository.findById(id);
return optionalrecomment.orElse(null);
}
@Transactional
public Member validateMember(HttpServletRequest request) {
if (!tokenProvider.validateToken(request.getHeader("Refresh-Token"))) {
return null;
}
return tokenProvider.getMemberFromAuthentication();
}
}