Skip to content

Commit

Permalink
✨ Feat: 사용자가 좋아요 누른 답글 반환되는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy authored Sep 23, 2024
2 parents 3bf2350 + ea6c07d commit f593cdc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public Response<?> deleteLike(@PathVariable Long commentId,
return Response.createSuccessWithNoData("댓글 좋아요 취소 완료");
}


@Operation(summary = "좋아요 누른 댓글 목록 조회", description = "사용자가 좋아요를 누른 댓글의 목록을 조회하기 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@GetMapping("/{memberId}/list")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import cotato.growingpain.member.domain.entity.Member;
import cotato.growingpain.member.repository.MemberRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -59,10 +58,9 @@ public List<Comment> getLikedComments(Long memberId) {

return commentLikes.stream()
.map(CommentLike::getComment)
.collect(Collectors.toList());
.toList();
}


private Comment findCommentId(Long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(() -> new AppException(ErrorCode.COMMENT_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package cotato.growingpain.replycomment.controller;

import cotato.growingpain.common.Response;
import cotato.growingpain.replycomment.domain.entity.ReplyComment;
import cotato.growingpain.replycomment.dto.response.ReplyCommentListResponse;
import cotato.growingpain.replycomment.service.ReplyCommentLikeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -21,15 +25,15 @@
@Tag(name = "답글 좋아요", description = "답글 좋아요 관련된 api")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/reply-comment/likes/{replyCommentId}")
@RequestMapping("/api/reply-comment/likes")
@Slf4j
public class ReplyCommentLikeController {

private final ReplyCommentLikeService replyCommentLikeService;

@Operation(summary = "답글 좋아요 등록", description = "답글 좋아요 등록을 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@PostMapping("")
@PostMapping("/{replyCommentId}")
@ResponseStatus(HttpStatus.CREATED)
public Response<?> registerLike(@PathVariable Long replyCommentId,
@AuthenticationPrincipal Long memberId) {
Expand All @@ -41,12 +45,23 @@ public Response<?> registerLike(@PathVariable Long replyCommentId,

@Operation(summary = "답글 좋아요 취소", description = "답글 좋아요 취소를 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@DeleteMapping("")
@DeleteMapping("/{replyCommentId}")
@ResponseStatus(HttpStatus.OK)
public Response<?> deleteLike(@PathVariable Long replyCommentId,
@AuthenticationPrincipal Long memberId) {

replyCommentLikeService.deleteLike(replyCommentId, memberId);
return Response.createSuccessWithNoData("답글 좋아요 취소 완료");
}

@Operation(summary = "좋아요 누른 답글 목록 조회", description = "사용자가 좋아요를 누른 답글의 목록을 조회하기 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@GetMapping("/{memberId}/list")
@ResponseStatus(HttpStatus.OK)
public Response<ReplyCommentListResponse> getLikeReplyComments(@AuthenticationPrincipal Long memberId) {
log.info("사용자가 좋아요를 누른 답글 목록 요청: memberId {}", memberId);
List<ReplyComment> likedReplyComments = replyCommentLikeService.getLikedReplyComments(memberId);
ReplyCommentListResponse replyCommentListResponse = ReplyCommentListResponse.from(likedReplyComments);
return Response.createSuccess("좋아요를 누른 댓글 목록 조회 완료", replyCommentListResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package cotato.growingpain.replycomment.dto.response;

import cotato.growingpain.replycomment.domain.entity.ReplyComment;
import java.util.List;

public record ReplyCommentListResponse(
List<ReplyCommentResponse> replyCommentList
) {
public static ReplyCommentListResponse from(List<ReplyComment> replyComments) {
List<ReplyCommentResponse> replyCommentResponses = replyComments.stream()
.map(ReplyCommentResponse::from)
.toList();
return new ReplyCommentListResponse(replyCommentResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cotato.growingpain.member.domain.entity.Member;
import cotato.growingpain.replycomment.domain.entity.ReplyComment;
import cotato.growingpain.replycomment.domain.entity.ReplyCommentLike;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -17,4 +18,5 @@ public interface ReplyCommentLikeRepository extends JpaRepository<ReplyCommentLi


Optional<ReplyCommentLike> findAllByMemberAndReplyComment(Member member, ReplyComment replyComment);
List<ReplyCommentLike> findAllByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cotato.growingpain.replycomment.domain.entity.ReplyCommentLike;
import cotato.growingpain.replycomment.repository.ReplyCommentLikeRepository;
import cotato.growingpain.replycomment.repository.ReplyCommentRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -41,11 +42,12 @@ public void registerLike(Long replyCommentId, Long memberId) {
}

@Transactional
public void deleteLike(Long replyCommentId, Long memberId) {
public void deleteLike(Long replyCommentId, Long memberId) {

ReplyComment replyComment = findReplyCommentById(replyCommentId);
Member member = findMemberById(memberId);
ReplyCommentLike replyCommentLike = replyCommentLikeRepository.findAllByMemberAndReplyComment(member, replyComment)
ReplyCommentLike replyCommentLike = replyCommentLikeRepository.findAllByMemberAndReplyComment(member,
replyComment)
.orElseThrow(() -> new AppException(ErrorCode.REPLY_COMMENT_LIKE_NOT_FOUND));

replyCommentLike.decreaseReplyCommentLikeCount(member, replyComment);
Expand All @@ -60,4 +62,13 @@ private ReplyComment findReplyCommentById(Long replyCommentId) {
private Member findMemberById(Long memberId) {
return memberRepository.getReferenceById(memberId);
}

@Transactional
public List<ReplyComment> getLikedReplyComments(Long memberId) {
List<ReplyCommentLike> replyCommentLikes = replyCommentLikeRepository.findAllByMemberId(memberId);

return replyCommentLikes.stream()
.map(ReplyCommentLike::getReplyComment)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ public void deleteReplyComment(Long replyCommentId, Long memberId) {
replyComment.deleteReplyComment();
replyCommentRepository.save(replyComment);
}
}
}

0 comments on commit f593cdc

Please sign in to comment.