From 647b507316c68c973bdb803b1dfd05f669087320 Mon Sep 17 00:00:00 2001 From: "DESKTOP-I633S8U\\user" <93star2@gmail.com> Date: Wed, 29 Mar 2023 16:52:37 +0900 Subject: [PATCH] =?UTF-8?q?[=EB=B0=95=ED=98=84=EC=84=9D]=20-=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EB=AA=A9=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hspark9781/Hoicegram#12 --- .../com/hsp/hoicegram/post/bo/PostBO.java | 30 ++++++++++----- .../hoicegram/post/comment/bo/CommentBO.java | 29 +++++++++++++- .../post/comment/dao/CommentDAO.java | 6 +-- .../post/comment/model/CommentDetail.java | 38 +++++++++++++++++++ .../hsp/hoicegram/post/model/PostDetail.java | 23 +++++++---- src/main/resources/mappers/commentMapper.xml | 8 ++-- src/main/webapp/WEB-INF/jsp/post/list.jsp | 7 ++-- 7 files changed, 112 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/hsp/hoicegram/post/comment/model/CommentDetail.java diff --git a/src/main/java/com/hsp/hoicegram/post/bo/PostBO.java b/src/main/java/com/hsp/hoicegram/post/bo/PostBO.java index fd01c3d..889321e 100644 --- a/src/main/java/com/hsp/hoicegram/post/bo/PostBO.java +++ b/src/main/java/com/hsp/hoicegram/post/bo/PostBO.java @@ -9,7 +9,7 @@ import com.hsp.hoicegram.common.FileManagerService; import com.hsp.hoicegram.post.comment.bo.CommentBO; -import com.hsp.hoicegram.post.comment.model.Comment; +import com.hsp.hoicegram.post.comment.model.CommentDetail; import com.hsp.hoicegram.post.dao.PostDAO; import com.hsp.hoicegram.post.like.bo.LikeBO; import com.hsp.hoicegram.post.model.Post; @@ -39,28 +39,39 @@ public int addPost(int userId, String content, MultipartFile file) { public List getPostList(int userId) { + + // controller에서 원하는(jsp에서 사용할 데이터 형태를 만들어준다. BO의 역할) + List postList = postDAO.selectPostList(); + // 한 카드 박스안에 포스트 뿐만 아니라 작성자, 댓글, 좋아요의 기능이 필요할때 List postDetailList = new ArrayList<>(); - for(Post post:postList) { + + + for(Post post:postList) { // post에서 조회한것 + 필요한 컬럼 = List에 넣기 - User user = userBO.getUserById(post.getUserId()); + // 포스트 모델에는 userId밖에 없기 때문에 userId를 통해 user테이블을 조회에서 아이디를 가져오고 + User user = userBO.getUserById(post.getUserId()); // user정보를 객체에 저장해야 한다. + int likeCount = likeBO.getLikeCount(post.getId()); boolean isLike = likeBO.isLike(userId, post.getId()); - Comment comment = commentBO.getComment(userId, post.getId()); + + // user와 같은 이유 + List commentList = commentBO.getCommentList(post.getId()); + PostDetail postDetail = new PostDetail(); - postDetail.setId(post.getId()); + postDetail.setId(post.getId()); // post 멤버변수에서 가져올수 있는 거는 가져오기 postDetail.setContent(post.getContent()); postDetail.setImagePath(post.getImagePath()); postDetail.setUserId(post.getUserId()); - postDetail.setNickname(user.getNickname()); - postDetail.setLikeCount(likeCount); - postDetail.setLike(isLike); - postDetail.setContent(comment.getContent()); + postDetail.setNickname(user.getNickname()); // 작성자 닉네임 -> PostDetail의 멤벼변수 + postDetail.setLikeCount(likeCount); // 좋아요 개수 -> PostDetail의 멤벼변수 + postDetail.setLike(isLike); // 좋아요 눌렀는지 여부 -> PostDetail의 멤벼변수 + postDetail.setCommentDetail(commentList); // 댓글 목록들 -> comment의 리스트 postDetailList.add(postDetail); } @@ -70,5 +81,6 @@ public List getPostList(int userId) { } + } diff --git a/src/main/java/com/hsp/hoicegram/post/comment/bo/CommentBO.java b/src/main/java/com/hsp/hoicegram/post/comment/bo/CommentBO.java index 641d69c..0f5e886 100644 --- a/src/main/java/com/hsp/hoicegram/post/comment/bo/CommentBO.java +++ b/src/main/java/com/hsp/hoicegram/post/comment/bo/CommentBO.java @@ -1,11 +1,17 @@ package com.hsp.hoicegram.post.comment.bo; +import java.util.ArrayList; +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.hsp.hoicegram.post.comment.dao.CommentDAO; import com.hsp.hoicegram.post.comment.model.Comment; +import com.hsp.hoicegram.post.comment.model.CommentDetail; +import com.hsp.hoicegram.user.bo.UserBO; +import com.hsp.hoicegram.user.model.User; @Service public class CommentBO { @@ -13,12 +19,31 @@ public class CommentBO { @Autowired private CommentDAO commentDAO; + @Autowired + private UserBO userBO; + public int addComment(int userId, int postId, String content) { return commentDAO.insertComment(userId, postId, content); } - public Comment getComment(int userId, int postId) { - return commentDAO.selectComment(userId, postId); + public List getCommentList(int postId) { + + List commentList = commentDAO.selectCommentList(postId); + List commentDetailList = new ArrayList<>(); + + + for(Comment comment:commentList) { + + User user = userBO.getUserById(comment.getUserId()); + + CommentDetail commentDetail = new CommentDetail(); + commentDetail.setId(comment.getId()); + commentDetail.setUserNickname(user.getNickname()); + commentDetail.setContent(comment.getContent()); + commentDetailList.add(commentDetail); + } + + return commentDetailList; } } diff --git a/src/main/java/com/hsp/hoicegram/post/comment/dao/CommentDAO.java b/src/main/java/com/hsp/hoicegram/post/comment/dao/CommentDAO.java index 3b6aa60..4610687 100644 --- a/src/main/java/com/hsp/hoicegram/post/comment/dao/CommentDAO.java +++ b/src/main/java/com/hsp/hoicegram/post/comment/dao/CommentDAO.java @@ -1,5 +1,7 @@ package com.hsp.hoicegram.post.comment.dao; +import java.util.List; + import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @@ -13,8 +15,6 @@ public int insertComment( , @Param("postId")int postId , @Param("content") String content); - public Comment selectComment( - @Param("userId") int userId - , @Param("postId")int postId); + public List selectCommentList(@Param("postId")int postId); } diff --git a/src/main/java/com/hsp/hoicegram/post/comment/model/CommentDetail.java b/src/main/java/com/hsp/hoicegram/post/comment/model/CommentDetail.java new file mode 100644 index 0000000..ffccbc3 --- /dev/null +++ b/src/main/java/com/hsp/hoicegram/post/comment/model/CommentDetail.java @@ -0,0 +1,38 @@ +package com.hsp.hoicegram.post.comment.model; + +public class CommentDetail { // comment에 없는 필요한 변수들 가져오기 + + private int id; + private int userId; + private String userNickname; + private String content; + + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public int getUserId() { + return userId; + } + public void setUserId(int userId) { + this.userId = userId; + } + public String getUserNickname() { + return userNickname; + } + public void setUserNickname(String userNickname) { + this.userNickname = userNickname; + } + public String getContent() { + return content; + } + public void setContent(String content) { + this.content = content; + } + + + +} diff --git a/src/main/java/com/hsp/hoicegram/post/model/PostDetail.java b/src/main/java/com/hsp/hoicegram/post/model/PostDetail.java index bcc7474..0512454 100644 --- a/src/main/java/com/hsp/hoicegram/post/model/PostDetail.java +++ b/src/main/java/com/hsp/hoicegram/post/model/PostDetail.java @@ -1,23 +1,30 @@ package com.hsp.hoicegram.post.model; +import java.util.List; + +import com.hsp.hoicegram.post.comment.model.Comment; +import com.hsp.hoicegram.post.comment.model.CommentDetail; + public class PostDetail { private int id; private int userId; - private String nickname; - private int likeCount; - private boolean isLike; - private String comment; + private String nickname; // 추가 + private int likeCount; // 추가 + private boolean isLike; // 추가 + private List commentDetail; // 리스트도 멤벼변수에 가능 private String content; private String imagePath; + - public String getComment() { - return comment; + + public List getCommentDetail() { + return commentDetail; } - public void setComment(String comment) { - this.comment = comment; + public void setCommentDetail(List commentDetail) { + this.commentDetail = commentDetail; } public boolean isLike() { return isLike; diff --git a/src/main/resources/mappers/commentMapper.xml b/src/main/resources/mappers/commentMapper.xml index ccd5117..0f0adda 100644 --- a/src/main/resources/mappers/commentMapper.xml +++ b/src/main/resources/mappers/commentMapper.xml @@ -27,16 +27,18 @@ ) - SELECT - `content` + `id` + , `userId` + , `postId` + , `content` , `createdAt` , `updatedAt` FROM `comment` WHERE `postId` = #{postId} - AND `userId` = #{userId} diff --git a/src/main/webapp/WEB-INF/jsp/post/list.jsp b/src/main/webapp/WEB-INF/jsp/post/list.jsp index 7c180ac..efaa0ad 100644 --- a/src/main/webapp/WEB-INF/jsp/post/list.jsp +++ b/src/main/webapp/WEB-INF/jsp/post/list.jsp @@ -68,12 +68,11 @@
+
-
${post.nickname }
${post.comment }
-
-
-
dabinnn0415
어딩ㅁ?
+
${comment.userNickname }
${comment.content }
+