Skip to content

Commit

Permalink
Merge pull request #82 from ttoklip/Feat/15-question-comment-like
Browse files Browse the repository at this point in the history
pr Feat/15 question comment like, likedByCurrentUser value
  • Loading branch information
why-only-english authored Feb 17, 2024
2 parents 8d32661 + f644b8b commit 290bb6b
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,27 @@ public class QuestionResponseConstant {
}
}
""";

public static final String REGISTER_LIKE = """
{
"time": "2024-02-10T12:55:35.127794",
"status": 200,
"code": "200",
"message": "요청에 성공하였습니다.",
"result": {
"message": "Question Type의 3번째 댓글 좋아요을(를) 생성했습니다."
}
}
""";
public static final String CANCEL_LIKE = """
{
"time": "2024-02-10T13:01:49.26421",
"status": 200,
"code": "200",
"message": "요청에 성공하였습니다.",
"result": {
"message": "Question Type의 3번째 댓글 좋아요을(를) 삭제했습니다."
}
}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.api.ttoklip.domain.newsletter.scarp.entity.NewsletterScrap;
import com.api.ttoklip.domain.privacy.domain.Interest;
import com.api.ttoklip.domain.privacy.domain.Profile;
import com.api.ttoklip.domain.question.like.entity.CommentLike;
import com.api.ttoklip.domain.question.post.domain.Question;
import com.api.ttoklip.domain.town.community.like.entity.CommunityLike;
import com.api.ttoklip.domain.town.community.post.entity.Community;
Expand Down Expand Up @@ -107,6 +108,10 @@ public class Member extends BaseEntity {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<NewsletterScrap> newsletterScraps = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CommentLike> commentLikes = new ArrayList<>();


public MemberEditorBuilder toEditor() {
return MemberEditor.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,39 @@ public SuccessResponse<Message> delete(final @PathVariable Long commentId) {
return new SuccessResponse<>(message);
}

/* LIKE */
@Operation(summary = "질문 댓글 좋아요 추가", description = "질문 ID에 해당하는 댓글에 좋아요를 추가합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "좋아요 추가 성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = SuccessResponse.class),
examples = @ExampleObject(
name = "SuccessResponse",
value = QuestionResponseConstant.REGISTER_LIKE,
description = "질문 댓글의 좋아요를 추가했습니다."
)))})
@PostMapping("/like/{commentId}")
public SuccessResponse<Message> registerLike(final @PathVariable Long commentId) {
Message message = questionCommentService.registerLike(commentId);
return new SuccessResponse<>(message);
}

@Operation(summary = "질문 댓글 좋아요 취소", description = "질문 ID에 해당하는 댓글에 좋아요를 취소합니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "좋아요 취소 성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = SuccessResponse.class),
examples = @ExampleObject(
name = "SuccessResponse",
value = QuestionResponseConstant.CANCEL_LIKE,
description = "꿀팁의 스크랩을 취소했습니다."
)))})
@DeleteMapping("/like/{commentId}")
public SuccessResponse<Message> cancleLike(final @PathVariable Long commentId) {
Message message = questionCommentService.cancleLike(commentId);
return new SuccessResponse<>(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
import com.api.ttoklip.domain.common.comment.Comment;
import com.api.ttoklip.domain.common.comment.dto.request.CommentCreateRequest;
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.question.like.entity.CommentLike;
import com.api.ttoklip.domain.question.post.domain.Question;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Getter
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@DiscriminatorValue(value = "Question")
public class QuestionComment extends Comment {
Expand All @@ -26,6 +23,9 @@ public class QuestionComment extends Comment {
@JoinColumn(name = "question_id")
private Question question;

@OneToMany(mappedBy = "questionComment", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CommentLike> commentLikes = new ArrayList<>();

@Builder
private QuestionComment(String content, Comment parent, Question question, Member member) {
super(content, parent, member); // Comment 클래스의 생성자 호출
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
import com.api.ttoklip.domain.common.report.dto.ReportCreateRequest;
import com.api.ttoklip.domain.common.report.service.ReportService;
import com.api.ttoklip.domain.question.comment.domain.QuestionComment;
import com.api.ttoklip.domain.question.like.service.CommentLikeService;
import com.api.ttoklip.domain.question.post.domain.Question;
import com.api.ttoklip.domain.question.post.service.QuestionPostService;
import com.api.ttoklip.domain.question.post.service.QuestionCommonService;
import com.api.ttoklip.global.success.Message;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class QuestionCommentService {

private final QuestionPostService questionPostService;
private final CommentService commentService;
private final ReportService reportService;
private final QuestionCommonService questionCommonService;
private final CommentLikeService commentLikeService;

/* -------------------------------------------- CREATE -------------------------------------------- */

@Transactional
public Message register(final Long postId, final CommentCreateRequest request) {
Question findQuestion = questionPostService.findQuestionById(postId);
Question findQuestion = questionCommonService.getQuestion(postId);

// comment 부모 찾기
Long parentCommentId = request.getParentCommentId();
Expand Down Expand Up @@ -95,5 +98,23 @@ public Message delete(final Long commentId) {
commentService.deleteById(commentId);
return Message.deleteCommentSuccess(QuestionComment.class, commentId);
}

/* -------------------------------------------- DELETE 끝 -------------------------------------------- */


/* -------------------------------------------- LIKE -------------------------------------------- */
@Transactional
public Message registerLike(Long commentId) {
commentLikeService.registerLike(commentId);
return Message.likePostSuccess(Question.class, commentId);
}

@Transactional
public Message cancleLike(Long commentId) {
commentLikeService.cancelLike(commentId);
return Message.likePostCancel(Question.class, commentId);
}

/* -------------------------------------------- LIKE 끝 -------------------------------------------- */

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.api.ttoklip.domain.question.like.entity;

import com.api.ttoklip.domain.common.base.BaseTimeEntity;
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.question.comment.domain.QuestionComment;
import com.api.ttoklip.domain.question.post.domain.Question;
import jakarta.persistence.*;
import lombok.*;

import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class CommentLike extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private QuestionComment questionComment;

public static CommentLike from(final QuestionComment questionComment) {
return CommentLike.builder()
.member(getCurrentMember())
.questionComment(questionComment)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.api.ttoklip.domain.question.like.repository;

import com.api.ttoklip.domain.question.like.entity.CommentLike;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;


public interface CommentLikeRepository extends JpaRepository<CommentLike, Long>, CommentLikeRepositoryCustom {

Optional<CommentLike> findByQuestionCommentIdAndMemberId(Long commentId, Long memberId);

boolean existsByQuestionCommentIdAndMemberId(Long commentId, Long memberId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.api.ttoklip.domain.question.like.repository;

public interface CommentLikeRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.api.ttoklip.domain.question.like.service;

import com.api.ttoklip.domain.question.comment.domain.QuestionComment;
import com.api.ttoklip.domain.question.like.entity.CommentLike;
import com.api.ttoklip.domain.question.like.repository.CommentLikeRepository;
import com.api.ttoklip.domain.question.post.service.QuestionCommonService;
import com.api.ttoklip.global.exception.ApiException;
import com.api.ttoklip.global.exception.ErrorType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CommentLikeService {

private final CommentLikeRepository commentLikeRepository;
private final QuestionCommonService questionCommonService;

// 좋아요 생성
public void registerLike(final Long commentId) {
Long currentMemberId = getCurrentMember().getId();
boolean exists = commentLikeRepository.existsByQuestionCommentIdAndMemberId(commentId, currentMemberId);
if (exists) {
return; // 이미 스크랩이 존재하면 좋아요를 생성하지 않고 return
}

QuestionComment findQuestionComment = questionCommonService.getQuestionComment(commentId);
CommentLike commentLike = CommentLike.from(findQuestionComment);
commentLikeRepository.save(commentLike);
}

// 좋아요 취소
public void cancelLike(final Long commentId) {
// commentId (댓글 ID)
QuestionComment findQuestionComment = questionCommonService.getQuestionComment(commentId);
Long findQuestionCommentId = findQuestionComment.getId();
Long currentMemberId = getCurrentMember().getId();

CommentLike commentLike = commentLikeRepository.findByQuestionCommentIdAndMemberId(findQuestionCommentId, currentMemberId)
.orElseThrow(() -> new ApiException(ErrorType.LIKE_NOT_FOUND));

// 자격 검증: 이 단계에서는 findByQuestionCommentIdAndMemberId 결과가 존재하므로, 현재 사용자가 좋아요를 누른 것입니다.
// 별도의 자격 검증 로직이 필요 없으며, 바로 삭제를 진행할 수 있습니다.
commentLikeRepository.deleteById(commentLike.getId());
}

public boolean existsByQuestionCommentIdAndMemberId(final Long commentId) {
Long currentMemberId = getCurrentMember().getId();
return commentLikeRepository.existsByQuestionCommentIdAndMemberId(commentId, currentMemberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;


@Tag(name = "Question Post", description = "꿀팁공유해요 게시판 API입니다.")
@RestController
@RequestMapping("/api/v1/question/post")
Expand Down Expand Up @@ -66,7 +69,8 @@ public SuccessResponse<Message> register(final @Validated @ModelAttribute Questi
)))})
@GetMapping("/{postId}")
public SuccessResponse<QuestionSingleResponse> getSinglePost(final @PathVariable Long postId) {
QuestionSingleResponse response = questionPostService.getSinglePost(postId);
Long commentId = getCurrentMember().getId();
QuestionSingleResponse response = questionPostService.getSinglePost(postId, commentId);
return new SuccessResponse<>(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.api.ttoklip.domain.member.domain.Member;
import com.api.ttoklip.domain.question.comment.domain.QuestionComment;
import com.api.ttoklip.domain.question.image.domain.QuestionImage;
import com.api.ttoklip.domain.question.like.entity.CommentLike;
import com.api.ttoklip.domain.question.post.dto.request.QuestionCreateRequest;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ public class QuestionSingleResponse {
@Schema(description = "질문에 대한 댓글 목록")
private List<CommentResponse> commentResponses;

public static QuestionSingleResponse of(final Question question, final List<QuestionComment> activeComments) {
@Schema(description = "현재 사용자의 해당 댓글 좋아요 여부")
private boolean likedByCurrentUser;

public static QuestionSingleResponse of(final Question question,
final List<QuestionComment> activeComments,
final boolean likedByCurrentUser) {
// 시간 포멧팅
String formattedCreatedDate = getFormattedCreatedDate(question);

Expand All @@ -65,6 +70,7 @@ public static QuestionSingleResponse of(final Question question, final List<Ques
.writer(question.getMember().getNickname())
.writtenTime(formattedCreatedDate)
.category(question.getCategory()) // 한글 카테고리 이름으로 반환
.likedByCurrentUser(likedByCurrentUser)
.commentCount(commentCount)
.imageUrls(imageResponses)
.commentResponses(commentResponses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
import com.api.ttoklip.domain.question.comment.domain.QuestionComment;
import com.api.ttoklip.domain.question.post.domain.Question;
import java.util.List;

import com.api.ttoklip.domain.town.community.post.entity.Community;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface QuestionRepositoryCustom {

Question findByIdActivated(final Long questionId);

QuestionComment findByCommentIdActivated(final Long commentId);

Question findByIdFetchJoin(final Long questionPostId);
List<QuestionComment> findActiveCommentsByQuestionId(final Long questionId);
Page<Question> matchCategoryPaging(final Category category, final Pageable pageable);
Expand Down
Loading

0 comments on commit 290bb6b

Please sign in to comment.