Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static CommentAllResponse from(PostComment comment) {
CommentAllResponse response = new CommentAllResponse();
response.setId(comment.getId());
response.setContent(comment.getContent());
response.setAuthorName(comment.getMember().getName()); // Member에서 이름 가져오기
response.setAuthorName(comment.getAuthorName()); // Member에서 이름 가져오기
response.setCreatedAt(comment.getCreateDate());
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Entity
@Getter
@Setter
Expand All @@ -23,4 +25,14 @@ public class PostComment extends BaseEntity {

private String role;

public Boolean isAuthor( Member member) {
return Objects.equals(this.member.getId(), member.getId());
}

public String getAuthorName() {
return member.getName();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import com.back.domain.post.comment.entity.PostComment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostCommentRepository extends JpaRepository<PostComment, Long> {

@Query("SELECT c FROM PostComment c JOIN FETCH c.member WHERE c.post.id = :postId")
List<PostComment> findCommentsWithMemberByPostId(@Param("postId") Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ public void createComment(Member member, Long postId, CommentCreateRequest comme

}

@Transactional
public List<CommentAllResponse> getAllPostCommentResponse(Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

List<PostComment> listPostComment = post.getComments();
validatePostExists(postId);

List<PostComment> listPostComment = postCommentRepository.findCommentsWithMemberByPostId(postId);

return listPostComment.stream()
.map(CommentAllResponse::from)
.toList();
}

@Transactional
public void removePostComment(Long postId, CommentDeleteRequest commentDeleteRequest, Member member) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
validatePostExists(postId);

PostComment postComment = postCommentRepository.findById(commentDeleteRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
PostComment postComment = getPostCommentById(commentDeleteRequest.getCommentId());
Member author = postComment.getMember();


Expand All @@ -67,9 +68,9 @@ public void removePostComment(Long postId, CommentDeleteRequest commentDeleteReq

@Transactional
public void updatePostComment(Long postId, CommentModifyRequest commentModifyRequest, Member member) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
validatePostExists(postId);

PostComment postComment = postCommentRepository.findById(commentModifyRequest.getCommentId()).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
PostComment postComment = getPostCommentById(commentModifyRequest.getCommentId());
Member author = postComment.getMember();


Expand All @@ -79,4 +80,19 @@ public void updatePostComment(Long postId, CommentModifyRequest commentModifyReq

postComment.setContent(commentModifyRequest.getContent());
}



private void validatePostExists(Long postId) {
if (!postRepository.existsById(postId)) {
throw new ServiceException("400", "해당 Id의 게시글이 없습니다.");
}
}

private PostComment getPostCommentById(Long commentId) {
return postCommentRepository.findById(commentId).orElseThrow(() -> new ServiceException("400", "해당 Id의 댓글이 없습니다."));
}



}
66 changes: 66 additions & 0 deletions back/src/main/java/com/back/domain/post/like/entity/PostLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.back.domain.post.like.entity;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.post.entity.Post;
import com.back.global.jpa.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "post_like", uniqueConstraints = {
@UniqueConstraint(columnNames = {"member_id", "post_id"})
})
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostLike extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
private Post post;

@Enumerated(EnumType.STRING)
private LikeStatus status;

public enum LikeStatus {
LIKE, DISLIKE
}

@Builder(access = AccessLevel.PRIVATE)
private PostLike(Member member, Post post, LikeStatus status) {
this.member = member;
this.post = post;
this.status = status;
}

public static PostLike create(Member member, Post post, LikeStatus status) {
if (member == null) {
throw new IllegalArgumentException("Member cannot be null");
}
if (post == null) {
throw new IllegalArgumentException("Post cannot be null");
}
if (status == null) {
throw new IllegalArgumentException("Status cannot be null");
}
return PostLike.builder()
.member(member)
.post(post)
.status(status)
.build();
}

public void updateStatus(LikeStatus status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.back.domain.post.like.repository;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.like.entity.PostLike;
import com.back.domain.post.post.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Optional<PostLike> findByMemberAndPost(Member member, Post post);

@Query("SELECT COUNT(pl) FROM PostLike pl WHERE pl.post.id = :postId AND pl.status = 'LIKE'")
int countLikesByPostId(@Param("postId") Long postId);

@Query("SELECT COUNT(pl) FROM PostLike pl WHERE pl.post.id = :postId AND pl.status = 'DISLIKE'")
int countDislikesByPostId(@Param("postId") Long postId);

boolean existsByMemberAndPost(Member member, Post post);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.back.domain.post.like.service;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.like.entity.PostLike;
import com.back.domain.post.like.repository.PostLikeRepository;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
import com.back.global.rq.Rq;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;
@Service
@RequiredArgsConstructor
public class PostLikeService {
private final Rq rq;
private final PostLikeRepository postLikeRepository;
private final PostService postService;


@Transactional
public void likePost(long postId) {
Member member = rq.getActor();
Post post = postService.findById(postId);

// 기존 좋아요/싫어요 기록 조회
Optional<PostLike> existingLike = postLikeRepository.findByMemberAndPost(member, post);

if (existingLike.isPresent()) {
PostLike postLike = existingLike.get();

if (postLike.getStatus() == PostLike.LikeStatus.LIKE) {
// 이미 좋아요 상태 -> 좋아요 취소
postLikeRepository.delete(postLike);
} else {
// 싫어요 상태 -> 좋아요로 변경
postLike.updateStatus(PostLike.LikeStatus.LIKE);
}
} else {
// 처음 좋아요
PostLike newLike = PostLike.create(member, post, PostLike.LikeStatus.LIKE);
postLikeRepository.save(newLike);

}
}

@Transactional
public void disLikePost(long postId) {
Member member = rq.getActor();
Post post = postService.findById(postId);

// 기존 좋아요/싫어요 기록 조회
Optional<PostLike> existingLike = postLikeRepository.findByMemberAndPost(member, post);

if (existingLike.isPresent()) {
PostLike postLike = existingLike.get();

if (postLike.getStatus() == PostLike.LikeStatus.DISLIKE) {
// 이미 싫어요 상태 -> 싫어요 취소
postLikeRepository.delete(postLike);
} else {
// 좋아요 상태 -> 싫어요로 변경
postLike.updateStatus(PostLike.LikeStatus.DISLIKE);
}
} else {
// 처음 싫어요
PostLike newDislike = PostLike.create(member, post, PostLike.LikeStatus.DISLIKE);
postLikeRepository.save(newDislike);
}
}

public int showDisLikeCount(Long postId) {
return postLikeRepository.countDislikesByPostId(postId);
}

public int showLikeCount(Long postId) {
return postLikeRepository.countLikesByPostId(postId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.post.post.controller;

import com.back.domain.member.member.entity.Member;
import com.back.domain.post.like.service.PostLikeService;
import com.back.domain.post.post.dto.*;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.service.PostService;
Expand All @@ -22,6 +23,7 @@
@RequestMapping("post/infor")
@RequiredArgsConstructor
public class InformationPostController {
private final PostLikeService postLikeService;
private final PostService postService;
private final Rq rq;

Expand Down Expand Up @@ -90,27 +92,36 @@ public RsData<Void> updatePost(@PathVariable Long post_id
return new RsData<>("200", "게시글 수정 성공", null);
}

@Operation(summary = "게시글 좋아요 + ")
@Operation(summary = "게시글 좋아요")
@PostMapping("/{post_id}/liked")
public RsData<Void> likePost(@PathVariable Long post_id) {
postService.likePost(post_id);
postLikeService.likePost(post_id);

return new RsData<>("200", "게시글 좋아요 성공", null);
}

@Operation(summary = "게시글 좋아요 (Show)")
@GetMapping("/{post_id}/liked")
public RsData<PostLikedResponse> getlike(@PathVariable Long post_id) {
int likeCount = postService.showLikeCount(post_id);
public RsData<PostLikedResponse> getLike(@PathVariable Long post_id) {
int likeCount = postLikeService.showLikeCount(post_id);
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);

return new RsData<>("200", "게시글 좋아요 조회 성공", postLikedResponse);
}

@Operation(summary = "게시글 싫어요 (Show)")
@GetMapping("/{post_id}/Disliked")
public RsData<PostLikedResponse> getDisLike(@PathVariable Long post_id) {
int likeCount = postLikeService.showDisLikeCount(post_id);
PostLikedResponse postLikedResponse = new PostLikedResponse(likeCount);

return new RsData<>("200", "게시글 싫어요 조회 성공", postLikedResponse);
}

@Operation(summary = "게시글 싫어요")
@PostMapping("/{post_id}/disliked")
public RsData<PostLikedResponse> disLikePost(@PathVariable Long post_id) {
postService.disLikePost(post_id);
postLikeService.disLikePost(post_id);

return new RsData<>("200", "게시글 싫어요 성공", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ public class PostAllResponse {
private String authorName;
private LocalDateTime createdAt;
private int viewCount;
private int like;

public PostAllResponse(Post post) {
this.id = post.getId();
this.title = post.getTitle();
this.authorName = post.getAuthorName();
this.createdAt = post.getCreateDate();
this.viewCount = post.getViewCount();
this.like = post.getLiked();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.back.domain.post.post.dto;

import lombok.Data;

@Data
public class PostLikedResponse {
int liked;
private int likeCount;

public PostLikedResponse(int liked) {
this.liked= liked;
public PostLikedResponse(int likeCount) {
this.likeCount = likeCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ public class PostSingleResponse {
private String authorName;
private LocalDateTime createdAt;
private int viewCount;
private int like;

public PostSingleResponse(Post post) {
this.id = post.getId();
this.title = post.getTitle();
this.authorName = post.getAuthorName();
this.createdAt = post.getCreateDate();
this.viewCount = post.getViewCount();
this.like = post.getLiked();
}
}
Loading