Skip to content

Commit

Permalink
#14 Fix: 게시글 좋아요 여부, 개수, 조회수, 댓글수 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
diddnwjd committed Mar 4, 2024
1 parent 9e9d1bd commit 9a3b978
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/ixxp/culpop/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ixxp.culpop.dto.post.PostDetailResponse;
import com.ixxp.culpop.dto.post.PostRequest;
import com.ixxp.culpop.dto.post.PostResponse;
import com.ixxp.culpop.entity.User;
import com.ixxp.culpop.security.UserDetailsImpl;
import com.ixxp.culpop.service.CommentService;
import com.ixxp.culpop.service.PostService;
Expand All @@ -15,6 +16,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequiredArgsConstructor
Expand All @@ -41,8 +43,10 @@ public ResponseEntity<List<PostResponse>> getPost(@RequestParam(name = "category

// 게시글 개별 조회
@GetMapping("/{postId}")
public ResponseEntity<PostDetailResponse> getPostDetail(@PathVariable int postId) {
return ResponseEntity.ok(postService.getPostDetail(postId));
public ResponseEntity<PostDetailResponse> getPostDetail(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable int postId) {
User user = Optional.ofNullable(userDetails).map(UserDetailsImpl::getUser).orElse(new User());
return ResponseEntity.ok(postService.getPostDetail(user, postId));
}

// 게시글 수정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class PostDetailResponse {
private LocalDateTime createdAt;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDateTime modifiedAt;
private String cateName;
private boolean likeCheck;
private int likeCount;
private int viewCount;
private String cateName;
private int commentCount;
}
1 change: 1 addition & 0 deletions src/main/java/com/ixxp/culpop/mapper/CommentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public interface CommentMapper {
void insertComment(Comment comment);
Comment selectCommentDetail(int commentId);
int countCommentsByPostId(int postId);
void updateComment(Comment comment);
void deleteComment(int commentId);
}
1 change: 1 addition & 0 deletions src/main/java/com/ixxp/culpop/mapper/PostLikeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
public interface PostLikeMapper {
void insertPostLike(PostLike postLike);
boolean checkPostLike(int userId, int postId);
int countLikesByPostId(int postId);
void deletePostLike(PostLike postLike);
}
2 changes: 2 additions & 0 deletions src/main/java/com/ixxp/culpop/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface PostMapper {
Post selectPostDetail(int postId);
List<Post> selectSearchPost(String word, int offset);
List<Post> selectPostByUserId(int userId);
int selectPostViewCount(int postId);
void updatePostViewCount(int postId);
void updatePost(Post post);
void deletePost(int postId);
}
16 changes: 11 additions & 5 deletions src/main/java/com/ixxp/culpop/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.ixxp.culpop.entity.PostLike;
import com.ixxp.culpop.entity.User;
import com.ixxp.culpop.mapper.CategoryMapper;
import com.ixxp.culpop.mapper.CommentMapper;
import com.ixxp.culpop.mapper.PostLikeMapper;
import com.ixxp.culpop.mapper.PostMapper;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +25,7 @@ public class PostService {
private final CategoryMapper categoryMapper;
private final PostMapper postMapper;
private final PostLikeMapper postLikeMapper;
private final CommentMapper commentMapper;

// 게시글 등록
@Transactional
Expand All @@ -49,18 +51,22 @@ public List<PostResponse> getPost(String category, int page) {
}

// 게시글 개별 조회
public PostDetailResponse getPostDetail(int postId) {
public PostDetailResponse getPostDetail(User user, int postId) {
Post post = postMapper.selectPostDetail(postId);
if (post == null) {
throw new IllegalArgumentException("post가 존재하지 않습니다.");
}

// postlike 확인 후 개수만 가져오기
int likeCount = 0;
// 댓글 id, 개수 가져오기
boolean likeCheck = (user != null) && postLikeMapper.checkPostLike(user.getId(), postId);
int likeCount = postLikeMapper.countLikesByPostId(postId);

postMapper.updatePostViewCount(postId);
int viewCount = postMapper.selectPostViewCount(postId);

int commentCount = commentMapper.countCommentsByPostId(postId);

return new PostDetailResponse(postId, post.getUser().getUsername(), post.getTitle(), post.getContent()
, post.getCreatedAt(), post.getModifiedAt(), likeCount, post.getViewCount(), post.getCategory().getCateName());
, post.getCreatedAt(), post.getModifiedAt(), post.getCategory().getCateName(), likeCheck, likeCount, viewCount, commentCount);
}

// 게시글 수정
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/mapper/comment-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
GROUP BY c.id, c.content, c.secret, c.parentId, c.createdAt, u.id, u.username, p.id
ORDER BY c.createdAt;
</select>
<select id="countCommentsByPostId" parameterType="int" resultType="int">
SELECT COUNT(*) FROM Comment WHERE postId = #{postId};
</select>
<update id="updateComment" parameterType="Comment">
UPDATE Comment SET content=#{content}, secret=#{secret}, parentId=#{parentId}
WHERE id=#{id}
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/mapper/post-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
GROUP BY p.id, p.title, p.createdAt, u.id, u.username, c.id, c.cateName
ORDER BY p.createdAt
</select>
<select id="selectPostViewCount" parameterType="int" resultType="int">
SELECT viewCount FROM Post WHERE id = #{postId};
</select>
<update id="updatePostViewCount" parameterType="int">
UPDATE Post SET viewCount = viewCount+1 WHERE id=#{postId}
</update>
<update id="updatePost" parameterType="post">
UPDATE Post SET title=#{title}, content=#{content}, categoryId=#{category.id}
WHERE id=#{id}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/mapper/postlike-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<select id="checkPostLike" parameterType="int" resultType="boolean">
SELECT IF(COUNT(*) = 1, 1, 0) FROM PostLike WHERE userId=#{userId} and postId=#{postId}
</select>
<select id="countLikesByPostId" parameterType="int" resultType="int">
SELECT COUNT(*) FROM PostLike WHERE postId = #{postId};
</select>
<delete id="deletePostLike">
DELETE FROM PostLike WHERE userId =#{user.id} and postId=#{post.id}
</delete>
Expand Down

0 comments on commit 9a3b978

Please sign in to comment.