Skip to content

Commit

Permalink
[박현석]
Browse files Browse the repository at this point in the history
- 댓글 목록

#12
  • Loading branch information
DESKTOP-I633S8U\user committed Mar 29, 2023
1 parent 4f01669 commit 647b507
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 29 deletions.
30 changes: 21 additions & 9 deletions src/main/java/com/hsp/hoicegram/post/bo/PostBO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,28 +39,39 @@ public int addPost(int userId, String content, MultipartFile file) {

public List<PostDetail> getPostList(int userId) {



// controller에서 원하는(jsp에서 사용할 데이터 형태를 만들어준다. BO의 역할)

List<Post> postList = postDAO.selectPostList();

// 한 카드 박스안에 포스트 뿐만 아니라 작성자, 댓글, 좋아요의 기능이 필요할때
List<PostDetail> postDetailList = new ArrayList<>();

for(Post post:postList) {


for(Post post:postList) { // post에서 조회한것 + 필요한 컬럼 = List<PostDetail>에 넣기

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<CommentDetail> 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);
}
Expand All @@ -70,5 +81,6 @@ public List<PostDetail> getPostList(int userId) {

}



}
29 changes: 27 additions & 2 deletions src/main/java/com/hsp/hoicegram/post/comment/bo/CommentBO.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
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 {

@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<CommentDetail> getCommentList(int postId) {

List<Comment> commentList = commentDAO.selectCommentList(postId);
List<CommentDetail> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Comment> selectCommentList(@Param("postId")int postId);

}
Original file line number Diff line number Diff line change
@@ -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;
}



}
23 changes: 15 additions & 8 deletions src/main/java/com/hsp/hoicegram/post/model/PostDetail.java
Original file line number Diff line number Diff line change
@@ -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> commentDetail; // 리스트도 멤벼변수에 가능
private String content;
private String imagePath;




public String getComment() {
return comment;

public List<CommentDetail> getCommentDetail() {
return commentDetail;
}
public void setComment(String comment) {
this.comment = comment;
public void setCommentDetail(List<CommentDetail> commentDetail) {
this.commentDetail = commentDetail;
}
public boolean isLike() {
return isLike;
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/mappers/commentMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
)
</insert>

<select id="selectComment" parameterType="map" resultType="com.hsp.hoicegram.post.comment.model.Comment">
<select id="selectCommentList" parameterType="map" resultType="com.hsp.hoicegram.post.comment.model.Comment">
SELECT
`content`
`id`
, `userId`
, `postId`
, `content`
, `createdAt`
, `updatedAt`
FROM
`comment`
WHERE
`postId` = #{postId}
AND `userId` = #{userId}
</select>


Expand Down
7 changes: 3 additions & 4 deletions src/main/webapp/WEB-INF/jsp/post/list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@
</div>
<div class="comment mt-4 ">
<div class="comment-box ml-1">
<c:forEach var="comment" items="${post.commentDetail }">
<div class="d-flex mt-2">
<div class="font-weight-bold">${post.nickname }</div><div class="ml-3">${post.comment }</div>
</div>
<div class="d-flex mt-2">
<div class="font-weight-bold">dabinnn0415</div><div class="ml-3">어딩ㅁ?</div>
<div class="font-weight-bold">${comment.userNickname }</div><div class="ml-3">${comment.content }</div>
</div>
</c:forEach>
</div>
<div class="input-group mt-3">
<input type="text" class="form-control col-5" id="commentInput${post.id }">
Expand Down

0 comments on commit 647b507

Please sign in to comment.