Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Post 삭제 및 모든 좋아요 기능, 조회수 기능, 투표 기능 구현(#46) #48

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions backend/src/main/java/aimo/backend/common/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import aimo.backend.domains.comment.entity.ParentComment;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.post.dto.PostPreviewResponse;
import aimo.backend.domains.post.dto.SavePostRequest;
import aimo.backend.domains.post.dto.requset.SavePostRequest;
import aimo.backend.domains.post.dto.response.FindPostAndCommentsByIdResponse;
import aimo.backend.domains.post.dto.response.FindPostAndCommentsByIdResponse.ParentCommentDto;
import aimo.backend.domains.post.dto.response.FindPostsByPostTypeResponse;
Expand All @@ -30,18 +29,6 @@ public static Post toEntity(SavePostRequest request, Member member) {
.build();
}

public static PostPreviewResponse toPreviewResponse(Post post) {
return new PostPreviewResponse(
post.getId(),
post.getTitle(),
getPreview(post.getSummaryAi()),
post.getPostLikes().size(),
post.getPostViews().size(),
post.getCommentsCount(),
post.getCreatedAt()
);
}

private static String getPreview(String summaryAi) {
return summaryAi.length() > PREVIEW_CONTENT_LENGTH ?
summaryAi.substring(0, PREVIEW_CONTENT_LENGTH) + "..." : summaryAi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import aimo.backend.common.dto.DataResponse;
import aimo.backend.domains.comment.dto.request.SaveChildCommentRequest;
import aimo.backend.domains.like.service.ChildCommentLikeService;
import aimo.backend.domains.comment.service.ChildCommentService;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.util.memberLoader.MemberLoader;
Expand All @@ -24,6 +25,7 @@ public class ChildCommentController {

private final ChildCommentService childCommentService;
private final MemberLoader memberLoader;
private final ChildCommentLikeService childCommentLikeService;

@PostMapping("/{postId}/comments/{parentCommentId}/child")
public ResponseEntity<DataResponse<Void>> saveChildComment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import aimo.backend.common.entity.BaseEntity;
import aimo.backend.domains.comment.costants.CommentConstants;
import aimo.backend.domains.like.entity.ChildCommentLike;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.post.entity.Post;
import jakarta.persistence.Column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import aimo.backend.common.entity.BaseEntity;
import aimo.backend.domains.comment.costants.CommentConstants;
import aimo.backend.domains.like.entity.ParentCommentLike;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.post.entity.Post;
import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static aimo.backend.common.exception.ErrorCode.*;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -67,4 +65,10 @@ public void validateAndDeleteChildComment(Member member, Long childCommentId) {
childCommentRepository.findById(childCommentId)
.ifPresent(ChildComment::deleteChildCommentSoftly);
}

// id로 자식 댓글 조회
public ChildComment findById(Long childCommentId) {
return childCommentRepository.findById(childCommentId)
.orElseThrow(() -> ApiException.from(CHILD_COMMENT_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package aimo.backend.domains.like.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import aimo.backend.common.dto.DataResponse;
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.service.ChildCommentLikeService;
import aimo.backend.domains.like.service.ParentCommentLikeService;
import aimo.backend.domains.like.service.PostLikeService;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.util.memberLoader.MemberLoader;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/v1/")
@RequiredArgsConstructor
public class LikeController {

private final PostLikeService postLikeService;
private final MemberLoader memberLoader;
private final ChildCommentLikeService childCommentLikeService;
private final ParentCommentLikeService parentCommentLikeService;

@PostMapping("/{postId}/likes")
public ResponseEntity<DataResponse<Void>> likePost(
@PathVariable Long postId,
@RequestParam("likeType") LikeType likeType
) {
Member member = memberLoader.getMember();
postLikeService.likePost(member, postId, likeType);

return ResponseEntity.ok(DataResponse.noContent());
}

@PostMapping("/comments/child/{childCommentId}/like")
public ResponseEntity<DataResponse<Void>> likeChildComment(
@PathVariable Long childCommentId,
@RequestParam("likeType") LikeType likeType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likeType으로 분리한게 인상적이네요. 기존에 설계한 API를 더 발전시키려는 고민이 묻어나서 보기 좋습니다.

) {
Member member = memberLoader.getMember();
childCommentLikeService.likeChildComment(member, childCommentId, likeType);

return ResponseEntity.ok(DataResponse.ok());
}

@PostMapping("/comments/{parentCommentId}/like")
public ResponseEntity<DataResponse<Void>> likeParentComment(
@PathVariable Long parentCommentId,
@RequestParam("likeType") LikeType likeType
) {
Member member = memberLoader.getMember();
parentCommentLikeService.likeParentComment(member, parentCommentId, likeType);

return ResponseEntity.ok(DataResponse.ok());
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package aimo.backend.domains.comment.entity;
package aimo.backend.domains.like.entity;

import static lombok.AccessLevel.*;

import aimo.backend.common.entity.Like;
import aimo.backend.domains.post.entity.Post;
import aimo.backend.domains.comment.entity.ChildComment;
import aimo.backend.domains.member.entity.Member;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -26,4 +27,10 @@ public class ChildCommentLike extends Like {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "child_comment_id")
private ChildComment childComment;

@Builder
private ChildCommentLike(Member member, ChildComment childComment) {
super(member);
this.childComment = childComment;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package aimo.backend.common.entity;
package aimo.backend.domains.like.entity;

import static jakarta.persistence.GenerationType.*;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import aimo.backend.common.entity.BaseEntity;
import aimo.backend.domains.member.entity.Member;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
Expand All @@ -30,4 +31,8 @@ public abstract class Like extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

public Like(Member member) {
this.member = member;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package aimo.backend.domains.comment.entity;
package aimo.backend.domains.like.entity;

import static lombok.AccessLevel.*;

import aimo.backend.common.entity.Like;
import aimo.backend.domains.post.entity.Post;
import aimo.backend.domains.comment.entity.ParentComment;
import aimo.backend.domains.member.entity.Member;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -26,4 +27,10 @@ public class ParentCommentLike extends Like {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_comment_id")
private ParentComment parentComment;

@Builder
private ParentCommentLike(Member member, ParentComment parentComment) {
super(member);
this.parentComment = parentComment;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package aimo.backend.domains.post.entity;
package aimo.backend.domains.like.entity;

import static lombok.AccessLevel.*;

import aimo.backend.common.entity.Like;
import aimo.backend.domains.member.entity.Member;
import aimo.backend.domains.post.entity.Post;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -25,4 +27,10 @@ public class PostLike extends Like {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

@Builder
private PostLike(Post post, Member member) {
super(member);
this.post = post;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aimo.backend.domains.like.model;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum LikeType {
LIKE("좋아요"),
CANCEL_LIKE("좋아요 취소")
;

private final String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aimo.backend.domains.like.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import aimo.backend.domains.like.entity.ChildCommentLike;

public interface ChildCommentLikeRepository extends JpaRepository<ChildCommentLike, Long> {

void deleteByMember_IdAndChildComment_Id(Long id, Long childCommentId);

boolean existsByChildComment_IdAndMember_Id(Long childCommentId, Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aimo.backend.domains.like.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import aimo.backend.domains.like.entity.ParentCommentLike;

public interface ParentCommentLikeRepository extends JpaRepository<ParentCommentLike, Long> {

void deleteByParentComment_Id(Long id, Long parentCommentId);

Boolean existsByParentComment_IdAndMember_Id(Long parentCommentId, Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aimo.backend.domains.like.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import aimo.backend.domains.like.entity.PostLike;

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

void deleteByMember_IdAndPost_Id(Long id, Long postId);

boolean existsByPost_IdAndMember_Id(Long postId, Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aimo.backend.domains.like.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aimo.backend.domains.comment.entity.ChildComment;
import aimo.backend.domains.comment.service.ChildCommentService;
import aimo.backend.domains.like.entity.ChildCommentLike;
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.repository.ChildCommentLikeRepository;
import aimo.backend.domains.member.entity.Member;
import lombok.RequiredArgsConstructor;

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

private final ChildCommentLikeRepository childCommentLikeRepository;
private final ChildCommentService childCommentService;

public void likeChildComment(Member member, Long childCommentId, LikeType likeType) {
ChildComment childComment = childCommentService.findById(childCommentId);

if (likeType == LikeType.LIKE) {
// 라이크가 이미 존재하면 무시
if (childCommentLikeRepository.existsByChildComment_IdAndMember_Id(childCommentId, member.getId())) {
return;
}

childCommentLikeRepository.save(ChildCommentLike.builder()
.childComment(childComment)
.member(member)
.build());
} else {
childCommentLikeRepository.deleteByMember_IdAndChildComment_Id(member.getId(), childCommentId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aimo.backend.domains.like.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aimo.backend.domains.comment.entity.ParentComment;
import aimo.backend.domains.comment.service.ParentCommentService;
import aimo.backend.domains.like.entity.ParentCommentLike;
import aimo.backend.domains.like.model.LikeType;
import aimo.backend.domains.like.repository.ParentCommentLikeRepository;
import aimo.backend.domains.member.entity.Member;
import lombok.RequiredArgsConstructor;

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

private final ParentCommentLikeRepository parentCommentLikeRepository;
private final ParentCommentService parentCommentService;

public void likeParentComment(Member member, Long parentCommentId, LikeType likeType) {
ParentComment parentComment = parentCommentService.findById(parentCommentId);

if (likeType == LikeType.LIKE) {
// 라이크가 이미 존재하면 무시
if (parentCommentLikeRepository.existsByParentComment_IdAndMember_Id(parentCommentId, member.getId())) {
return;
}

parentCommentLikeRepository.save(ParentCommentLike.builder()
.parentComment(parentComment)
.member(member)
.build());
} else {
parentCommentLikeRepository.deleteByParentComment_Id(member.getId(), parentCommentId);
}
}
}
Loading