Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions springBootBlog/sql/comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- DDL for comment table

INSERT INTO comment(`author_id`,`post_id`,`comment_content`, `create_time`, `update_time`)
values
(1, 1, "some comment", now(), now()),
(2, 2, "some comment", now(), now()),
(3, 3, "some comment", now(), now()),
(2, 1, "ahahahahahahaah", now(), now());
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.yen.mdblog.controller;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.entity.Po.Comment;
import com.yen.mdblog.entity.Po.Post;
import com.yen.mdblog.entity.Vo.CreateComment;
import com.yen.mdblog.entity.Vo.CreatePost;
import com.yen.mdblog.service.CommentService;
import com.yen.mdblog.util.PostUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/comment")
@Log4j2
public class CommentController {

@Autowired
CommentService commentService;

// @GetMapping("/{postId}")
// public String getComments(){
//
// }

@RequestMapping(value="/create", method= RequestMethod.POST)
public String createComment(CreateComment request, Model model, Principal principal){

log.info(">>> create comment start ...");
Comment comment = new Comment();
// comment.setAuthorId(request.getAuthorId());
comment.setPostId(request.getPostId());
comment.setCommentContent(request.getCommentContent());
comment.setCreateTime(new Date()); //?

System.out.println(">>> (CommentController) create new comment = " + comment.toString());
log.info(">>>> create post end ...");
commentService.insertComment(comment);
model.addAttribute("user", principal.getName());
return "comment_success";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import com.github.pagehelper.PageInfo;
import com.yen.mdblog.entity.Dto.SearchRequest;
import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.entity.Po.Comment;
import com.yen.mdblog.entity.Po.Post;
import com.yen.mdblog.entity.Vo.CreateComment;
import com.yen.mdblog.entity.Vo.CreatePost;
import com.yen.mdblog.mapper.PostMapper;
import com.yen.mdblog.repository.PostRepository;
import com.yen.mdblog.service.AuthorService;
import com.yen.mdblog.service.CommentService;
import com.yen.mdblog.service.PostService;
import com.yen.mdblog.util.PostUtil;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -51,6 +54,9 @@ public class PostController {
@Autowired
PostMapper postMapper;

@Autowired
CommentService commentService;

@GetMapping("/all")
public String getPaginatedPosts(
@RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
Expand Down Expand Up @@ -102,7 +108,19 @@ public String getPostById(@PathVariable long id, Model model, Principal principa

Optional<Post> postOptional = postRepository.findById(id);
if (postOptional.isPresent()) {

model.addAttribute("post", postOptional.get());
model.addAttribute("comment", new CreateComment());

// load comment
// TODO : double check whether should do below here or in CommentController
List<Comment> commentList = commentService.getCommentsByPostId(id);
System.out.println(">>> comment len = " + commentList.size());
// only add to model when comment size > 0
if (commentList.size() > 0){
model.addAttribute("comments", commentList);
}

} else {
model.addAttribute("error", "no-post");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Data;
import lombok.ToString;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.*;

@Data
Expand Down
37 changes: 37 additions & 0 deletions springBootBlog/src/main/java/com/yen/mdblog/entity/Po/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.yen.mdblog.entity.Po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.*;
import java.util.*;

@Data
@Entity
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "comment")
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Integer id;

@Column
private Integer authorId;

@Column
private Long postId;

@Column(columnDefinition = "TEXT")
private String commentContent;

@Column
private Date createTime;

@Column
private Date updateTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yen.mdblog.entity.Vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateComment {

private long id;
private Integer authorId;
private Long postId;
private String commentContent;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public interface AuthorMapper {

public int getAuthorCount();

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

public void updateAuthor(Author author);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.yen.mdblog.mapper;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.entity.Po.Comment;
import com.yen.mdblog.entity.Po.Post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface CommentMapper {

public List<Comment> getCommentByPostId(Long postId);

public void insertComment(Comment comment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- https://dotblogs.com.tw/zjh/2018/09/28/mybatis_1 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yen.mdblog.mapper.CommentMapper">

<!-- public List<Comment> getCommentByPostId(Long postId); -->
<select id="getCommentByPostId" resultType="com.yen.mdblog.entity.Po.Comment" parameterType="long">
SELECT * FROM comment where post_id = #{postId}
</select>

<!-- public void insertComment(Comment comment); -->
<!-- <insert id="insertComment">-->
<!-- INSERT INTO comment(`author_id`,`post_id`,`comment_content`, `create_time`, `update_time`)-->
<!-- values(#{authorId}, #{postId}, #{commentContent}, #{createTime}, #{updateTime})-->
<!-- </insert>-->
<insert id="insertComment">
INSERT INTO comment(`post_id`, `comment_content`)
values(#{postId}, #{commentContent})
</insert>

</mapper>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.yen.mdblog.service;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.entity.Po.Post;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.yen.mdblog.service;

import com.yen.mdblog.entity.Po.Comment;

import java.util.List;

public interface CommentService {

List<Comment> getCommentsByPostId(Long postId);

void insertComment(Comment comment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yen.mdblog.service.impl;

import com.yen.mdblog.entity.Po.Comment;
import com.yen.mdblog.mapper.CommentMapper;
import com.yen.mdblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

@Autowired
CommentMapper commentMapper;

@Override
public List<Comment> getCommentsByPostId(Long postId) {
return commentMapper.getCommentByPostId(postId);
}

@Override
public void insertComment(Comment comment) {
commentMapper.insertComment(comment);
}

}
16 changes: 16 additions & 0 deletions springBootBlog/src/main/resources/templates/comment_success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">

<head>
<!-- <meta charset="UTF-8">-->
<meta charset="UTF-8" name=”viewport” content=”width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes” />
<title>Add new comment complete</title>
</head>
<body>
<div th:insert="header"></div>

<h2>Comment OK</h2>

</div>
</body>
</html>
25 changes: 25 additions & 0 deletions springBootBlog/src/main/resources/templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,30 @@ <h1 th:utext="${post.title}">Title</h1>
<p th:utext="${post.content}">Content</p>
</article>
</div>

<!-- create comment -->
<body>
<!--<form action="#" th:action="@{/posts/create}" th:object="${CreatePost}" method="post" align="left">-->
<form action="#" th:action="@{/comment/create}" th:object="${comment}" method="post" align="left">
<p>Content: <textarea name="commentContent" rows="3" cols="50" th:field="*{commentContent}">your content</textarea></p>

<input type="text" name="postId" th:value="*{postId}" placeholder="post id">

<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>

<!-- view comment -->
<h3>Comment</h3>
<div th:if="${not #lists.isEmpty(comments)}" th:each="comment : ${comments}" class="card">
<div class="card-body">
<!-- <p th:text="'id: ' + ${greeting.id}" />-->
<p class="card-text" th:text="'ID: ' + ${comment.id}">Id</p>
<p class="card-text" th:text="'Author: ' + ${comment.authorId}">commenter Id</p>
<p class="card-text" th:text="'Content: ' + ${comment.commentContent}">content</p>
<p class="card-text" th:text="'Time: ' + ${comment.createTime}">comment time</p>
</div>
</div>

</body>
</html>