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

✨ 피드, 댓글, 좋아요 api 구현 #181

Merged
merged 7 commits into from
May 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public enum ErrorStatus implements BaseErrorCode {
POST_SELF_FOLLOW(HttpStatus.BAD_REQUEST, "POST4001", "자신을 팔로우 했습니다."),
POST_ALREADY_FOLLOW(HttpStatus.BAD_REQUEST, "POST4002", "이미 팔로우 했습니다."),
POST_FOLLOW_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4003", "해당 팔로우를 찾을수 없습니다."),
POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4004", "포스트를 찾을수 없습니다."),
POST_COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST,"POST4005", "해당 댓글을 찾을 수 없습니다."),
POST_COMMENT_NOT_UPDATE(HttpStatus.BAD_REQUEST, "POST4006", "해당 댓글을 수정할 수 없습니다."),


//Category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public enum SuccessStatus implements BaseCode {

//POST
POST_FOLLOW_SUCCESS(HttpStatus.OK, "POST2001", "팔로우 성공입니다."),
POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다.")
POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다."),
POST_UPLOAD_SUCCESS(HttpStatus.OK, "POST2003", "포스트 업로드 성공입니다."),
POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다."),
POST_DELETE_SUCCESS(HttpStatus.OK, "POST2005", "포스트 삭제 성공입니다."),
POST_LIKE_SUCCESS(HttpStatus.OK, "POST2006", "포스트 좋아요 성공입니다."),
POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다."),
POST_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포스트 댓글 달기 성공입니다."),
POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다."),
POST_UPDATE_COMMENT_SUCCESS(HttpStatus.OK, "POST2010", "포스트 댓글 수정 성공입니다."),
POST_COMMENT_LIKE_SUCCESS(HttpStatus.OK, "POST2011", "포스트 댓글 좋아요 성공입니다."),
POST_DELETE_COMMENT_LIKE_SUCCESS(HttpStatus.OK,"POST2012", "포스트 댓글 좋아요 취소 성공입니다.")
;

private final HttpStatus httpStatus;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/umc/TheGoods/converter/post/PostConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.umc.TheGoods.converter.post;


import com.umc.TheGoods.domain.community.Comment;
import com.umc.TheGoods.domain.community.Post;
import com.umc.TheGoods.domain.images.PostImg;
import com.umc.TheGoods.domain.mapping.comment.CommentLike;
import com.umc.TheGoods.domain.mapping.post.PostLike;
import com.umc.TheGoods.domain.member.Follow;
import com.umc.TheGoods.domain.member.Member;

Expand All @@ -11,4 +17,44 @@ public static Follow toFollow(Member follower, Member following){
.following(following)
.build();
}

public static Post toPost(Member member, String content){

return Post.builder()
.content(content)
.member(member)
.build();
}

public static PostImg toPostImg(String url, Post post){

return PostImg.builder()
.post(post)
.url(url)
.build();
}

public static PostLike toPostLike(Member member, Post post){

return PostLike.builder()
.member(member)
.post(post)
.build();
}

public static Comment toComment(Member member, Post post, String comment){

return Comment.builder()
.content(comment)
.post(post)
.member(member)
.build();
}

public static CommentLike toCommentLike(Member member, Comment comment){
return CommentLike.builder()
.comment(comment)
.member(member)
.build();
}
}
17 changes: 14 additions & 3 deletions src/main/java/com/umc/TheGoods/domain/community/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.umc.TheGoods.domain.mapping.comment.CommentLike;
import com.umc.TheGoods.domain.mapping.comment.CommentMention;
import com.umc.TheGoods.domain.member.Member;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -15,6 +15,9 @@
@Entity
@Table(name = "comment")
@Getter
@Builder
@DynamicInsert
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정
public class Comment extends BaseDateTimeEntity {

Expand All @@ -31,6 +34,10 @@ public class Comment extends BaseDateTimeEntity {
@JoinColumn(name = "post_id")
private Post post;

@ColumnDefault("0")
@Column(nullable = false)
private Integer likesCount;

/**
* 대댓글 관련
* parentComment: 부모 댓글을 참조하는 필드
Expand Down Expand Up @@ -60,4 +67,8 @@ public void setContent(String newContent) {
public void removeCommentLike(Long id) {
this.commentLikeList.removeIf((like) -> like.getId() == id);
}

public void updateParent(Comment comment) {
this.parentComment = comment;
}
}
21 changes: 18 additions & 3 deletions src/main/java/com/umc/TheGoods/domain/community/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.umc.TheGoods.domain.images.PostImg;
import com.umc.TheGoods.domain.mapping.post.PostLike;
import com.umc.TheGoods.domain.member.Member;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -15,6 +15,9 @@
@Entity
@Table(name = "post")
@Getter
@Builder
@DynamicInsert
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정
public class Post extends BaseDateTimeEntity {

Expand All @@ -38,4 +41,16 @@ public class Post extends BaseDateTimeEntity {

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<PostImg> postImgList = new ArrayList<>();

@ColumnDefault("0")
@Column(nullable = false)
private Long viewCount;

@ColumnDefault("0")
@Column(nullable = false)
private Integer likesCount;

public void updatePost(String content){
this.content = content;
}
}
14 changes: 11 additions & 3 deletions src/main/java/com/umc/TheGoods/domain/images/PostImg.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.umc.TheGoods.domain.images;

import com.umc.TheGoods.domain.community.Post;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import com.umc.TheGoods.domain.item.Item;
import lombok.*;

import javax.persistence.*;

@Entity
@Table(name = "post_img")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostImg {

Expand All @@ -24,4 +25,11 @@ public class PostImg {

@Column(columnDefinition = "TEXT", nullable = false)
private String url;

public void setPost(Post post){
if(this.post != null)
post.getPostImgList().remove(this);
this.post = post;
post.getPostImgList().add(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import com.umc.TheGoods.domain.community.Comment;
import com.umc.TheGoods.domain.member.Member;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import javax.persistence.*;

@Entity
@Table(name = "comment_like")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CommentLike {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.umc.TheGoods.domain.common.BaseDateTimeEntity;
import com.umc.TheGoods.domain.community.Post;
import com.umc.TheGoods.domain.member.Member;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import javax.persistence.*;

@Entity
@Table(name = "post_like")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정
public class PostLike extends BaseDateTimeEntity {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.umc.TheGoods.repository.post;


import com.umc.TheGoods.domain.mapping.comment.CommentLike;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CommentLikeRepository extends JpaRepository<CommentLike, Long> {

Optional<CommentLike> findByMember_IdAndComment_Id(Long memberId, Long commentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.TheGoods.repository.post;

import com.umc.TheGoods.domain.community.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface CommentRepository extends JpaRepository<Comment, Long> {
@Modifying(clearAutomatically = true)
@Query("update Comment c SET c.content= :comment WHERE c.id= :commentId")
void updateComment(Long commentId, String comment);

@Modifying(clearAutomatically = true)
@Query("UPDATE Comment c SET c.likesCount = c.likesCount +1 WHERE c.id= :commentId")
void updateLikeCount(Long commentId);

@Modifying(clearAutomatically = true)
@Query("UPDATE Comment c SET c.likesCount = c.likesCount -1 WHERE c.id= :commentId")
void updateUnlikeCount(Long commentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.umc.TheGoods.repository.post;

import com.umc.TheGoods.domain.images.PostImg;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PostImgRepository extends JpaRepository<PostImg, Long> {
List<PostImg> findByPostId(Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.umc.TheGoods.repository.post;


import com.umc.TheGoods.domain.mapping.post.PostLike;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

Optional<PostLike> findByMember_IdAndPost_Id(Long memberId, Long postId);
}
12 changes: 12 additions & 0 deletions src/main/java/com/umc/TheGoods/repository/post/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

import com.umc.TheGoods.domain.community.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface PostRepository extends JpaRepository<Post, Long> {
//Page<Post> getPostsOrderByLikes(Pageable pageable);

@Modifying(clearAutomatically = true)
@Query("UPDATE Post p SET p.likesCount = p.likesCount +1 WHERE p.id= :postId")
void updateLikeCount(Long postId);

@Modifying(clearAutomatically = true)
@Query("update Post p SET p.likesCount = p.likesCount -1 WHERE p.id= :postId")
void updateUnlikeCount(Long postId);


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
package com.umc.TheGoods.service.PostService;

import com.umc.TheGoods.domain.member.Member;
import com.umc.TheGoods.web.dto.post.PostRequestDto;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

public interface PostCommandService {

void follow(Long followingId, Member follower);

void deleteFollow(Long followingId, Long followerId);

void registerPost(Member member, String content, List<MultipartFile> multipartFileList);

void updatePost(Member member, Long postId, String content, List<MultipartFile> multipartFileList);

void deletePost(Member member, Long postId);

void likePost(Member member, Long postId);


void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request);

void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request);

void likeComment(Member member, Long commentId);


}
Loading
Loading