Skip to content

Commit

Permalink
[박현석]
Browse files Browse the repository at this point in the history
-좋아요 기능 완료
- 댓글 목록 미완료

#12

#13
  • Loading branch information
DESKTOP-I633S8U\user committed Mar 28, 2023
1 parent 68749a7 commit 4f01669
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class FileManagerService {

public static final String FILE_UPLOAD_PATH = "/Users/hsp9781/web_hsp/spring_project/upload/hoicegram/images";
public static final String FILE_UPLOAD_PATH = "D:\\web_hsp\\spring_project\\upload\\hoicegram\\images";

// "/Users/hsp9781/web_hsp/spring_project/upload/hoicegram/images"
// "/Users/hsp9781/web_hsp/spring_project/upload/memo/images"
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/hsp/hoicegram/post/bo/PostBO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.web.multipart.MultipartFile;

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.dao.PostDAO;
import com.hsp.hoicegram.post.like.bo.LikeBO;
import com.hsp.hoicegram.post.model.Post;
Expand All @@ -27,6 +29,9 @@ public class PostBO {
@Autowired
private LikeBO likeBO;

@Autowired
private CommentBO commentBO;

public int addPost(int userId, String content, MultipartFile file) {
String imagePath = FileManagerService.saveFile(userId, file);
return postDAO.insertPost(userId, content, imagePath);
Expand All @@ -44,6 +49,7 @@ public List<PostDetail> getPostList(int userId) {
User user = userBO.getUserById(post.getUserId());
int likeCount = likeBO.getLikeCount(post.getId());
boolean isLike = likeBO.isLike(userId, post.getId());
Comment comment = commentBO.getComment(userId, post.getId());

PostDetail postDetail = new PostDetail();

Expand All @@ -54,6 +60,7 @@ public List<PostDetail> getPostList(int userId) {
postDetail.setNickname(user.getNickname());
postDetail.setLikeCount(likeCount);
postDetail.setLike(isLike);
postDetail.setContent(comment.getContent());

postDetailList.add(postDetail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,20 @@
import com.hsp.hoicegram.post.comment.bo.CommentBO;

@RestController
@RequestMapping("/post")
@RequestMapping("/post/comment")
public class CommentRestController {

@Autowired
private CommentBO commentBO;

@PostMapping("/comment")
public Map<String, String> addComment(
@RequestParam("text") String text
, @RequestParam("postId") int postId
@PostMapping("/create")
public Map<String, String> createComment(
@RequestParam("postId") int postId
, @RequestParam("content") String content
, HttpSession session) {

int userId = (Integer)session.getAttribute("userId");

int count = commentBO.addComment(userId, postId, text);
int count = commentBO.addComment(userId, postId, content);

Map<String, String> resultMap = new HashMap<>();

Expand Down
10 changes: 8 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,18 +1,24 @@
package com.hsp.hoicegram.post.comment.bo;


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;

@Service
public class CommentBO {

@Autowired
private CommentDAO commentDAO;

public int addComment(int userId, int postId, String text) {
return commentDAO.insertComment(userId, postId, text);
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.hsp.hoicegram.post.comment.model.Comment;

@Repository
public interface CommentDAO {

public int insertComment(
@Param("userId") int userId
, @Param("postId")int postId
, @Param("text") String text);
, @Param("content") String content);

public Comment selectComment(
@Param("userId") int userId
, @Param("postId")int postId);

}
51 changes: 51 additions & 0 deletions src/main/java/com/hsp/hoicegram/post/comment/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.hsp.hoicegram.post.comment.model;

import java.util.Date;

public class Comment {
private int id;
private int postId;
private int userId;
private String content;
private Date createdAt;
private Date updatedAt;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPostId() {
return postId;
}
public void setPostId(int postId) {
this.postId = postId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/hsp/hoicegram/post/like/LikeRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ public Map<String, String> addLike(

return resultMap;
}

@GetMapping("/unlike")
public Map<String, String> unlike(
@RequestParam("postId") int postId
, HttpSession session) {

int userId = (Integer)session.getAttribute("userId");

int count = likeBO.unLike(userId, postId);

Map<String, String> resultMap = new HashMap<>();

if(count == 0) {
resultMap.put("result", "fail");
} else {
resultMap.put("result", "success");
}

return resultMap;
}





Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/hsp/hoicegram/post/like/bo/LikeBO.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public boolean isLike(int userId, int postId) {
}
}

public int unLike(int userId, int postId) {
return likeDAO.deleteLike(userId, postId);

}



}
4 changes: 4 additions & 0 deletions src/main/java/com/hsp/hoicegram/post/like/dao/LikeDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public int selectCountLikeByUserId(
@Param("userId") int userId
, @Param("postId") int postId);

public int deleteLike(
@Param("userId") int userId
, @Param("postId") int postId);


}
2 changes: 0 additions & 2 deletions src/main/java/com/hsp/hoicegram/post/model/PostDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public class PostDetail {
private String imagePath;





public String getComment() {
return comment;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ spring:
suffix: .jsp
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/hoicegram
url: jdbc:mysql://localhost:3306/hoicegram_0316
username: root
password: hyunsuk2@
password: root
16 changes: 14 additions & 2 deletions src/main/resources/mappers/commentMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@
(
#{userId}
, #{postId}
, #{text}
, #{content}
, now()
, now()
)
</insert>

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


</insert>


</mapper>
10 changes: 10 additions & 0 deletions src/main/resources/mappers/likeMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
`userId` = #{userId}
AND `postId` = #{postId}
</select>

<delete id="deleteLike" parameterType="map">
DELETE FROM
`like`
WHERE
`userId` = #{userId}
AND `postId` = #{postId}
</delete>




</mapper>
4 changes: 1 addition & 3 deletions src/main/resources/static/css/post/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ header {
font-size:40px;
}

.unlike-icon {
color:red;
}


footer {
height:70px;
Expand Down
69 changes: 60 additions & 9 deletions src/main/webapp/WEB-INF/jsp/post/list.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,28 @@
<div class="like d-flex align-items-center ml-1">
<c:choose>
<c:when test="${post.like }">
<i class="bi bi-heart-fill"></i>
<i class="bi bi-heart-fill text-danger unlike-icon" data-post-id="${post.id }"></i>
</c:when>
<c:otherwise>
<i class="bi bi-heart like-icon" data-post-id="${post.id }"></i>
</c:otherwise>
</c:choose>
<span class="font-weight-bold ml-2">좋아요 </span> <span class="ml-2">${post.likeCount }개</span>
</div>
<div class="comment mt-4 ml-1">
<div class="comment-box">
<div class="comment mt-4 ">
<div class="comment-box ml-1">
<div class="d-flex mt-2">
<div class="font-weight-bold">hspaaaaar_k</div><div class="ml-3">곧 보자구~~</div>
<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>
</div>
<div class="input-group mt-3">
<input type="text" class="form-control col-5">
<div class="input-group-append">
<button class="btn btn-secondary" type="button">게시</button>
</div>
<input type="text" class="form-control col-5" id="commentInput${post.id }">
<div class="input-group-append">
<button class="btn btn-secondary comment-btn" data-post-id="${post.id }" type="button">게시</button>
</div>
</div>
</div>
</div>
Expand All @@ -93,7 +93,58 @@


<script>
$(document).ready(function() {
$(document).ready(function() {
$(".comment-btn").on("click", function() {
let postId = $(this).data("post-id");
// 버튼에 매칭된 input태그를 객체화 시키기
// 1. 버튼 바로 앞 태그를 객체화 한다.
// let comment = $(this).prev().val(); 내 태그에서는 바로 앞에 있지 않다.
// 2. id에 post.id값
let comment = $("#commentInput" + postId).val();
$.ajax({
type:"post"
, url:"/post/comment/create"
, data:{"postId":postId, "content":comment}
, success:function(data) {
if(data.result == "success") {
location.reload();
} else {
alert("댓글쓰기 실패");
}
}
, error:function() {
alert("댓글쓰기 에러");
}
});
});
$(".unlike-icon").on("click", function() {
let postId = $(this).data("post-id");
$.ajax({
type:"get"
, url:"/post/unlike"
, data:{"postId":postId}
, success:function(data) {
if(data.result == "success") {
location.reload();
} else {
alert("좋아요 취소 실패");
}
}
, error:function() {
alert("좋아요 취소 에러");
}
});
});
$(".like-icon").on("click", function() {
let postId = $(this).data("post-id");
Expand Down

0 comments on commit 4f01669

Please sign in to comment.