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

Feat/#101/게시글 댓글 #107

Merged
merged 30 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
19dac56
refactor: userSpaceDao를 이용하여 UserSpace 객체 호출
arkchive Aug 15, 2024
efaed6b
refactor: targetId 타입 변경
arkchive Aug 15, 2024
bb39591
feat: 게시글 전체 댓글 조회 dto 생성
arkchive Aug 15, 2024
e101cc4
feat: 댓글 오류 추가
arkchive Aug 15, 2024
2b69fb4
feat: PostException 삭제
arkchive Aug 15, 2024
ac17ed2
refactor: PostComment -> Comment 이름 변경
arkchive Aug 15, 2024
31d36f0
feat: 댓글 관련 좋아요 로직 생성 및 게시글 관련 좋아요 로직 수정
arkchive Aug 15, 2024
64cad8e
feat: 빌더 생성 및 오타 수정
arkchive Aug 15, 2024
e0eacb5
feat: 특정 게시글 댓글 조회 api 개발
arkchive Aug 15, 2024
da5dfab
refactor: comment 이름 변경으로 인한 빌더 오류 수정
arkchive Aug 15, 2024
72d862b
feat: LikeDao 분리
arkchive Aug 16, 2024
fce6af4
feat: 댓글 생성 dto 생성
arkchive Aug 16, 2024
62aab18
feat: Comment 빌더 생성
arkchive Aug 16, 2024
62e7cdb
feat: LikeDao 분리로 인한 수정
arkchive Aug 16, 2024
345c3c5
feat: 게시글 상세 조회 시 댓글 리스트 반환 변경
arkchive Aug 16, 2024
6b5a957
feat: 댓글 생성 api 개발
arkchive Aug 16, 2024
a08a71c
feat: pull origin develop
arkchive Aug 16, 2024
0b21e97
refactor: 댓글 생성 시 댓글 아이디 반환되도록 리팩토링
arkchive Aug 16, 2024
df4c27a
Merge branch 'develop' of https://github.com/KUIT-Space/KUIT_Space_Ba…
arkchive Aug 16, 2024
22a44a6
feat: 대댓글 유효성 검사
arkchive Aug 17, 2024
c2fc113
refactor: 오류 수정
arkchive Aug 17, 2024
1f153f0
feat: 댓글 좋아요 api 개발
arkchive Aug 17, 2024
7ae8c7a
feat: 변수명 수정
arkchive Aug 17, 2024
669ce2d
refactor: 유효성 검사 로직 수정
arkchive Aug 17, 2024
6db690a
feat: 게시글, 댓글 유효성 검사 추가
arkchive Aug 17, 2024
be0a751
refactor: 댓글 유효성 검사 로직 수정 (isReply 삭제)
arkchive Aug 18, 2024
0757242
Merge branch 'develop' of https://github.com/KUIT-Space/KUIT_Space_Ba…
arkchive Aug 18, 2024
a3c8cd8
refactor: validateUserInSpace 로직 삭제
arkchive Aug 19, 2024
796a94c
Merge branch 'develop' of https://github.com/KUIT-Space/KUIT_Space_Ba…
arkchive Aug 19, 2024
084ab94
feat: @CheckUserSpace(required = false)추가
arkchive Aug 19, 2024
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
54 changes: 54 additions & 0 deletions src/main/java/space/space_spring/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package space.space_spring.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.comment.request.CreateCommentRequest;
import space.space_spring.dto.comment.response.ReadCommentsResponse;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.CommentService;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/space/{spaceId}/board/post/{postId}/comment")
@Slf4j
public class CommentController {
private final CommentService commentService;

// 댓글 조회
@GetMapping
public BaseResponse<List<ReadCommentsResponse>> getComments(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId
) {
// TODO 1: 유저가 스페이스에 속하는 지 검증
commentService.validateUserInSpace(userId, spaceId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

UserSpaceValidateIntercpterUserSpaceValidateIntercpterURL 보시면 "/space/{spaceId}인 컨트롤러는 Interceptor 적용됩니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 반영했습니다!


// TODO 2: 특정 게시글의 댓글 리스트 get
List<ReadCommentsResponse> comments = commentService.getCommentsByPost(postId, userId);
return new BaseResponse<>(comments);
}

// 댓글 생성
@PostMapping
public BaseResponse<CreateCommentRequest.Response> createComment(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId,
@RequestBody @Valid CreateCommentRequest.Request createCommentRequest
) {
// TODO 1: 유저가 스페이스에 속하는 지 검증
commentService.validateUserInSpace(userId, spaceId);

// TODO 2: 작성한 댓글 저장 및 아이디 반환
Long commentId = commentService.createComment(userId, postId, createCommentRequest);
CreateCommentRequest.Response response = CreateCommentRequest.Response.of(commentId);
return new BaseResponse<>(response);
}

}
57 changes: 54 additions & 3 deletions src/main/java/space/space_spring/controller/LikeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public BaseResponse<String> likePost(
// TODO 1: 유저가 스페이스에 속하는지 검증
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: 유저가 해당 게시글에 이미 좋아요를 눌렀는지 검증
likeService.validateAlreadyLiked(userId, postId);
// TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증
likeService.validateAlreadyLikedPost(userId, postId);

// TODO 3: 좋아요 로직 수행
likeService.likePost(userId, postId);
Expand All @@ -46,11 +46,62 @@ public BaseResponse<String> unlikePost(
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증
likeService.validateNotLikedYet(userId, postId);
likeService.validateNotLikedPost(userId, postId);

// TODO 3: 좋아요 취소 로직 수행
likeService.unlikePost(userId, postId);

return new BaseResponse<>("게시글에 좋아요를 취소하였습니다.");
}

// 댓글 좋아요
@PostMapping("/comment/{commentId}/like")
public BaseResponse<String> likeComment(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId,
@PathVariable Long commentId
) {
// TODO 1: 유저가 스페이스에 속하는지 검증
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: 댓글이 게시글에 속하는지 검증
likeService.validateCommentInPost(postId, commentId);

// TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증
likeService.validateAlreadyLikedComment(userId, commentId);

// TODO 4: 좋아요 로직 수행
likeService.likeComment(userId, commentId);


return new BaseResponse<>("댓글에 좋아요를 눌렀습니다.");
}



// 댓글 좋아요 취소
@DeleteMapping("/comment/{commentId}/like")
public BaseResponse<String> unlikeComment(
@JwtLoginAuth Long userId,
@PathVariable Long spaceId,
@PathVariable Long postId,
@PathVariable Long commentId
) {
// TODO 1: 유저가 스페이스에 속하는지 검증
likeService.validateUserInSpace(userId, spaceId);

// TODO 2: 댓글이 게시글에 속하는지 검증
likeService.validateCommentInPost(postId, commentId);

// TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증
likeService.validateNotLikedComment(userId, commentId);

// TODO 4: 좋아요 취소 로직 수행
likeService.unlikeComment(userId, commentId);


return new BaseResponse<>("댓글에 좋아요를 취소하였습니다.");
}

}
25 changes: 25 additions & 0 deletions src/main/java/space/space_spring/dao/CommentDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package space.space_spring.dao;

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 space.space_spring.entity.Comment;
import space.space_spring.entity.Post;

import java.util.List;

@Repository
public interface CommentDao extends JpaRepository<Comment, Long> {
// 특정 게시글의 댓글을 가져오는 메서드
List<Comment> findByPost(Post post);

// 대댓글 수를 계산하는 메서드
int countByTargetId(Long targetId);

// 유저가 특정 댓글을 좋아요 했는지 확인하는 메서드
@Query("SELECT CASE WHEN COUNT(l) > 0 THEN true ELSE false END " +
"FROM CommentLike l WHERE l.comment.commentId = :commentId AND l.user.userId = :userId")
boolean isUserLikedComment(@Param("commentId") Long commentId, @Param("userId") Long userId);

}
14 changes: 14 additions & 0 deletions src/main/java/space/space_spring/dao/CommentLikeDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package space.space_spring.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import space.space_spring.entity.Comment;
import space.space_spring.entity.CommentLike;
import space.space_spring.entity.User;

import java.util.Optional;

@Repository
public interface CommentLikeDao extends JpaRepository<CommentLike, Long> {
Optional<CommentLike> findByUserAndComment(User user, Comment comment);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package space.space_spring.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import space.space_spring.entity.Post;
import space.space_spring.entity.PostLike;
import space.space_spring.entity.User;
import space.space_spring.entity.*;

import java.util.Optional;

public interface LikeDao extends JpaRepository<PostLike, Long> {
public interface PostLikeDao extends JpaRepository<PostLike, Long> {
Optional<PostLike> findByUserAndPost(User user, Post post);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package space.space_spring.dto.comment.request;

import com.mongodb.lang.Nullable;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
import space.space_spring.entity.Comment;
import space.space_spring.entity.Post;
import space.space_spring.entity.User;


public class CreateCommentRequest {
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class Request {
@NotBlank
private String content;
private Long targetId = null;

@Builder
public Comment toEntity(User user, Post post) {
return Comment.builder()
.user(user)
.post(post)
.content(content)
.targetId(targetId)
.build();
}
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class Response {
private Long CommentId;

public static Response of(Long commentId) {
return new Response(commentId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package space.space_spring.dto.comment.response;

import lombok.*;
import space.space_spring.entity.Comment;
import space.space_spring.entity.UserSpace;
import space.space_spring.util.post.ConvertCreatedDate;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ReadCommentsResponse {
private Long commentId;

private String userProfileImg;
private String userName;

private String content;
private String time;

private int commentCount;
private int likeCount;
private boolean isLike;
private Long targetId;

public static ReadCommentsResponse of(Comment comment, UserSpace userSpace, boolean isLike, int commentCount) {
return ReadCommentsResponse.builder()
.commentId(comment.getCommentId())
.userName(userSpace != null ? userSpace.getUserName() : null)
.userProfileImg(userSpace != null ? userSpace.getUserProfileImg() : null)
.content(comment.getContent())
.time(ConvertCreatedDate.setCreatedDate(comment.getCreatedAt()))
.commentCount(commentCount)
.likeCount(comment.getLikeCount())
.isLike(isLike)
.targetId(comment.getTargetId())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package space.space_spring.dto.post.response;

import lombok.*;
import space.space_spring.dto.comment.response.ReadCommentsResponse;
import space.space_spring.entity.Post;
import space.space_spring.entity.PostComment;
import space.space_spring.entity.PostImage;
import space.space_spring.entity.UserSpace;
import space.space_spring.util.post.ConvertCreatedDate;
Expand Down Expand Up @@ -33,11 +33,11 @@ public class ReadPostDetailResponse {

// 댓글 및 좋아요
private int commentCount;
private List<PostComment> postComments;
private List<ReadCommentsResponse> Comments;
private int likeCount;
private boolean isLike;

public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean isLike) {
public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean isLike, List<ReadCommentsResponse> comments) {
List<String> postImageUrls = post.getPostImages().stream()
.map(PostImage::getPostImgUrl)
.toList();
Expand All @@ -52,8 +52,8 @@ public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean
.content(post.getContent())
.time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt()))
.type(post.getType())
.commentCount(post.getPostComments().size())
.postComments(post.getPostComments())
.commentCount(post.getComments().size())
.Comments(comments)
.likeCount(post.getLikeCount())
.isLike(isLike)
.postImage(postImageUrls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static ReadPostsResponse of(Post post, int postCount, UserSpace userSpace
.time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt()))
.type(post.getType())
.postCount(postCount)
.commentCount(post.getPostComments().size())
.commentCount(post.getComments().size())
.likeCount(post.getLikeCount())
.isLike(isLike)
.postImage(postImageUrls)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package space.space_spring.entity;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "Post_Comment")
@Getter
public class PostComment extends BaseEntity{
@NoArgsConstructor
public class Comment extends BaseEntity{
@Id
@GeneratedValue
@Column(name = "comment_id")
Expand All @@ -24,11 +27,25 @@ public class PostComment extends BaseEntity{
private String content;

@Column(name = "comment_like")
private int like;
private int likeCount;

@Column(name = "isReply")
private boolean isReply;

@Column(name = "comment_target_id")
private String targetId;
private Long targetId;

@Builder
public Comment(User user, Post post, String content, Boolean isReply, Long targetId) {
this.user = user;
this.post = post;
this.content = content;
this.targetId = targetId;
}

public void increaseLikeCount() {
this.likeCount++;
}

public void decreaseLikeCount() {
this.likeCount--;
}
}
11 changes: 9 additions & 2 deletions src/main/java/space/space_spring/entity/CommentLike.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package space.space_spring.entity;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -15,10 +16,16 @@ public class CommentLike extends BaseEntity{
private Long commentLikeId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "space_post_id")
private Post post;
@JoinColumn(name = "post_comment_id")
private Comment comment;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Builder
public CommentLike(User user, Comment comment) {
this.user = user;
this.comment = comment;
}
}
2 changes: 1 addition & 1 deletion src/main/java/space/space_spring/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Post extends BaseEntity {
private List<PostImage> postImages;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostComment> postComments;
private List<Comment> Comments;

@Builder
public Post(User user, Space space, String title, String content, String type, List<PostImage> postImages) {
Expand Down
Loading
Loading