Skip to content

Commit 006a251

Browse files
authored
Merge pull request #111 from yennanliu/blog-dev-008-post-reply-feature
blog-dev-008-post-reply-feature - add post comment feature
2 parents b1065a9 + 526b402 commit 006a251

File tree

14 files changed

+257
-4
lines changed

14 files changed

+257
-4
lines changed

springBootBlog/sql/comment.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- DDL for comment table
2+
3+
INSERT INTO comment(`author_id`,`post_id`,`comment_content`, `create_time`, `update_time`)
4+
values
5+
(1, 1, "some comment", now(), now()),
6+
(2, 2, "some comment", now(), now()),
7+
(3, 3, "some comment", now(), now()),
8+
(2, 1, "ahahahahahahaah", now(), now());
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.yen.mdblog.controller;
2+
3+
import com.yen.mdblog.entity.Po.Author;
4+
import com.yen.mdblog.entity.Po.Comment;
5+
import com.yen.mdblog.entity.Po.Post;
6+
import com.yen.mdblog.entity.Vo.CreateComment;
7+
import com.yen.mdblog.entity.Vo.CreatePost;
8+
import com.yen.mdblog.service.CommentService;
9+
import com.yen.mdblog.util.PostUtil;
10+
import lombok.extern.log4j.Log4j2;
11+
import org.springframework.beans.BeanUtils;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.stereotype.Controller;
14+
import org.springframework.ui.Model;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestMethod;
18+
19+
import java.security.Principal;
20+
import java.time.LocalDateTime;
21+
import java.util.Date;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
@Controller
26+
@RequestMapping("/comment")
27+
@Log4j2
28+
public class CommentController {
29+
30+
@Autowired
31+
CommentService commentService;
32+
33+
// @GetMapping("/{postId}")
34+
// public String getComments(){
35+
//
36+
// }
37+
38+
@RequestMapping(value="/create", method= RequestMethod.POST)
39+
public String createComment(CreateComment request, Model model, Principal principal){
40+
41+
log.info(">>> create comment start ...");
42+
Comment comment = new Comment();
43+
// comment.setAuthorId(request.getAuthorId());
44+
comment.setPostId(request.getPostId());
45+
comment.setCommentContent(request.getCommentContent());
46+
comment.setCreateTime(new Date()); //?
47+
48+
System.out.println(">>> (CommentController) create new comment = " + comment.toString());
49+
log.info(">>>> create post end ...");
50+
commentService.insertComment(comment);
51+
model.addAttribute("user", principal.getName());
52+
return "comment_success";
53+
}
54+
55+
}

springBootBlog/src/main/java/com/yen/mdblog/controller/PostController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import com.github.pagehelper.PageInfo;
55
import com.yen.mdblog.entity.Dto.SearchRequest;
66
import com.yen.mdblog.entity.Po.Author;
7+
import com.yen.mdblog.entity.Po.Comment;
78
import com.yen.mdblog.entity.Po.Post;
9+
import com.yen.mdblog.entity.Vo.CreateComment;
810
import com.yen.mdblog.entity.Vo.CreatePost;
911
import com.yen.mdblog.mapper.PostMapper;
1012
import com.yen.mdblog.repository.PostRepository;
1113
import com.yen.mdblog.service.AuthorService;
14+
import com.yen.mdblog.service.CommentService;
1215
import com.yen.mdblog.service.PostService;
1316
import com.yen.mdblog.util.PostUtil;
1417
import lombok.extern.log4j.Log4j2;
@@ -51,6 +54,9 @@ public class PostController {
5154
@Autowired
5255
PostMapper postMapper;
5356

57+
@Autowired
58+
CommentService commentService;
59+
5460
@GetMapping("/all")
5561
public String getPaginatedPosts(
5662
@RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
@@ -102,7 +108,19 @@ public String getPostById(@PathVariable long id, Model model, Principal principa
102108

103109
Optional<Post> postOptional = postRepository.findById(id);
104110
if (postOptional.isPresent()) {
111+
105112
model.addAttribute("post", postOptional.get());
113+
model.addAttribute("comment", new CreateComment());
114+
115+
// load comment
116+
// TODO : double check whether should do below here or in CommentController
117+
List<Comment> commentList = commentService.getCommentsByPostId(id);
118+
System.out.println(">>> comment len = " + commentList.size());
119+
// only add to model when comment size > 0
120+
if (commentList.size() > 0){
121+
model.addAttribute("comments", commentList);
122+
}
123+
106124
} else {
107125
model.addAttribute("error", "no-post");
108126
}

springBootBlog/src/main/java/com/yen/mdblog/entity/Po/Author.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import lombok.Data;
44
import lombok.ToString;
55
import javax.persistence.*;
6-
import java.time.LocalDateTime;
76
import java.util.*;
87

98
@Data
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.yen.mdblog.entity.Po;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
import javax.persistence.*;
8+
import java.util.*;
9+
10+
@Data
11+
@Entity
12+
@ToString
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@Table(name = "comment")
16+
public class Comment {
17+
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.IDENTITY)
20+
@Column
21+
private Integer id;
22+
23+
@Column
24+
private Integer authorId;
25+
26+
@Column
27+
private Long postId;
28+
29+
@Column(columnDefinition = "TEXT")
30+
private String commentContent;
31+
32+
@Column
33+
private Date createTime;
34+
35+
@Column
36+
private Date updateTime;
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.yen.mdblog.entity.Vo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import java.time.LocalDateTime;
7+
8+
@Data
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
public class CreateComment {
12+
13+
private long id;
14+
private Integer authorId;
15+
private Long postId;
16+
private String commentContent;
17+
private LocalDateTime createTime;
18+
private LocalDateTime updateTime;
19+
}

springBootBlog/src/main/java/com/yen/mdblog/mapper/AuthorMapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public interface AuthorMapper {
1616

1717
public int getAuthorCount();
1818

19-
// @Insert("INSERT INTO authors(`id`,`email`,`name`,`url`) values(#{id}, #{email}, #{name}, #{url})")
20-
// @Options(useGeneratedKeys = true, keyProperty = "id")
2119
public void insertAuthor(Author author);
2220

2321
public void updateAuthor(Author author);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.yen.mdblog.mapper;
2+
3+
import com.yen.mdblog.entity.Po.Author;
4+
import com.yen.mdblog.entity.Po.Comment;
5+
import com.yen.mdblog.entity.Po.Post;
6+
import org.apache.ibatis.annotations.Mapper;
7+
import org.apache.ibatis.annotations.Param;
8+
9+
import java.util.List;
10+
11+
@Mapper
12+
public interface CommentMapper {
13+
14+
public List<Comment> getCommentByPostId(Long postId);
15+
16+
public void insertComment(Comment comment);
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!-- https://dotblogs.com.tw/zjh/2018/09/28/mybatis_1 -->
3+
<!DOCTYPE mapper
4+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
5+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
6+
<mapper namespace="com.yen.mdblog.mapper.CommentMapper">
7+
8+
<!-- public List<Comment> getCommentByPostId(Long postId); -->
9+
<select id="getCommentByPostId" resultType="com.yen.mdblog.entity.Po.Comment" parameterType="long">
10+
SELECT * FROM comment where post_id = #{postId}
11+
</select>
12+
13+
<!-- public void insertComment(Comment comment); -->
14+
<!-- <insert id="insertComment">-->
15+
<!-- INSERT INTO comment(`author_id`,`post_id`,`comment_content`, `create_time`, `update_time`)-->
16+
<!-- values(#{authorId}, #{postId}, #{commentContent}, #{createTime}, #{updateTime})-->
17+
<!-- </insert>-->
18+
<insert id="insertComment">
19+
INSERT INTO comment(`post_id`, `comment_content`)
20+
values(#{postId}, #{commentContent})
21+
</insert>
22+
23+
</mapper>

springBootBlog/src/main/java/com/yen/mdblog/service/AuthorService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.yen.mdblog.service;
22

33
import com.yen.mdblog.entity.Po.Author;
4-
import com.yen.mdblog.entity.Po.Post;
54

65
import java.util.List;
76

0 commit comments

Comments
 (0)