create comment
This commit is contained in:
70
src/main/java/io/spring/api/CommentsApi.java
Normal file
70
src/main/java/io/spring/api/CommentsApi.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package io.spring.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import io.spring.api.exception.InvalidRequestException;
|
||||
import io.spring.api.exception.ResourceNotFoundException;
|
||||
import io.spring.application.comment.CommentData;
|
||||
import io.spring.application.comment.CommentQueryService;
|
||||
import io.spring.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
import io.spring.core.comment.Comment;
|
||||
import io.spring.core.comment.CommentRepository;
|
||||
import io.spring.core.user.User;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.xml.ws.Response;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/articles/{slug}/comments")
|
||||
public class CommentsApi {
|
||||
private ArticleRepository articleRepository;
|
||||
private CommentRepository commentRepository;
|
||||
private CommentQueryService commentQueryService;
|
||||
|
||||
@Autowired
|
||||
public CommentsApi(ArticleRepository articleRepository,
|
||||
CommentRepository commentRepository,
|
||||
CommentQueryService commentQueryService) {
|
||||
this.articleRepository = articleRepository;
|
||||
this.commentRepository = commentRepository;
|
||||
this.commentQueryService = commentQueryService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<CommentData> createComment(@PathVariable("slug") String slug,
|
||||
@AuthenticationPrincipal User user,
|
||||
@Valid @RequestBody NewCommentParam newCommentParam,
|
||||
BindingResult bindingResult) {
|
||||
Article article = findArticle(slug);
|
||||
if (bindingResult.hasErrors()) {
|
||||
throw new InvalidRequestException(bindingResult);
|
||||
}
|
||||
Comment comment = new Comment(newCommentParam.getBody(), user.getId(), article.getId());
|
||||
commentRepository.save(comment);
|
||||
return ResponseEntity.status(201).body(commentQueryService.findById(comment.getId(), user).get());
|
||||
}
|
||||
|
||||
private Article findArticle(String slug) {
|
||||
return articleRepository.findBySlug(slug).map(article -> article).orElseThrow(ResourceNotFoundException::new);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@JsonRootName("comment")
|
||||
class NewCommentParam {
|
||||
@NotBlank(message = "can't be empty")
|
||||
private String body;
|
||||
}
|
||||
23
src/main/java/io/spring/application/comment/CommentData.java
Normal file
23
src/main/java/io/spring/application/comment/CommentData.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package io.spring.application.comment;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import io.spring.application.profile.ProfileData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonRootName("comment")
|
||||
public class CommentData {
|
||||
private String id;
|
||||
private String body;
|
||||
private String articleId;
|
||||
private DateTime createdAt;
|
||||
private DateTime updatedAt;
|
||||
@JsonProperty("author")
|
||||
private ProfileData profileData;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.spring.application.comment;
|
||||
|
||||
import io.spring.application.profile.UserRelationshipQueryService;
|
||||
import io.spring.core.user.User;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CommentQueryService {
|
||||
private CommentReadService commentReadService;
|
||||
private UserRelationshipQueryService userRelationshipQueryService;
|
||||
|
||||
public CommentQueryService(CommentReadService commentReadService, UserRelationshipQueryService userRelationshipQueryService) {
|
||||
this.commentReadService = commentReadService;
|
||||
this.userRelationshipQueryService = userRelationshipQueryService;
|
||||
}
|
||||
|
||||
public Optional<CommentData> findById(String id, User user) {
|
||||
CommentData commentData = commentReadService.findById(id);
|
||||
if (commentData == null) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
commentData.getProfileData().setFollowing(
|
||||
userRelationshipQueryService.isUserFollowing(
|
||||
user.getId(),
|
||||
commentData.getProfileData().getId()));
|
||||
}
|
||||
return Optional.ofNullable(commentData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.spring.application.comment;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface CommentReadService {
|
||||
CommentData findById(String id);
|
||||
}
|
||||
27
src/main/java/io/spring/core/comment/Comment.java
Normal file
27
src/main/java/io/spring/core/comment/Comment.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package io.spring.core.comment;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(of = "id")
|
||||
public class Comment {
|
||||
private String id;
|
||||
private String body;
|
||||
private String userId;
|
||||
private String articleId;
|
||||
private DateTime createdAt;
|
||||
|
||||
public Comment(String body, String userId, String articleId) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.body = body;
|
||||
this.userId = userId;
|
||||
this.articleId = articleId;
|
||||
this.createdAt = new DateTime();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.spring.core.comment;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CommentRepository {
|
||||
void save(Comment comment);
|
||||
|
||||
Optional<Comment> findById(String id);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.spring.infrastructure.comment;
|
||||
|
||||
import io.spring.core.comment.Comment;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface CommentMapper {
|
||||
void insert(@Param("comment") Comment comment);
|
||||
|
||||
Comment findById(@Param("id") String id);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.spring.infrastructure.comment;
|
||||
|
||||
import io.spring.core.comment.Comment;
|
||||
import io.spring.core.comment.CommentRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class MyBatisCommentRepository implements CommentRepository {
|
||||
private CommentMapper commentMapper;
|
||||
|
||||
@Autowired
|
||||
public MyBatisCommentRepository(CommentMapper commentMapper) {
|
||||
this.commentMapper = commentMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Comment comment) {
|
||||
commentMapper.insert(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Comment> findById(String id) {
|
||||
return Optional.ofNullable(commentMapper.findById(id));
|
||||
}
|
||||
}
|
||||
@@ -37,3 +37,12 @@ create table article_tags (
|
||||
article_id varchar(255) not null,
|
||||
tag_id varchar(255) not null
|
||||
);
|
||||
|
||||
create table comments (
|
||||
id varchar(255) primary key,
|
||||
body text,
|
||||
article_id varchar(255),
|
||||
user_id varchar(255),
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -1,6 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="io.spring.application.article.ArticleReadService">
|
||||
<sql id="profileColumns">
|
||||
U.id userId,
|
||||
U.username userUsername,
|
||||
U.bio userBio,
|
||||
U.image userImage
|
||||
</sql>
|
||||
<sql id="selectArticleData">
|
||||
select
|
||||
A.id articleId,
|
||||
@@ -11,10 +17,7 @@
|
||||
A.created_at articleCreatedAt,
|
||||
A.updated_at articleUpdatedAt,
|
||||
T.name as tagName,
|
||||
U.id userId,
|
||||
U.username userUsername,
|
||||
U.bio userBio,
|
||||
U.image userImage
|
||||
<include refid="profileColumns"/>
|
||||
from
|
||||
articles A
|
||||
left join article_tags AT on A.id = AT.article_id
|
||||
|
||||
32
src/main/resources/mapper/CommentMapper.xml
Normal file
32
src/main/resources/mapper/CommentMapper.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="io.spring.infrastructure.comment.CommentMapper">
|
||||
<insert id="insert">
|
||||
insert into comments(id, body, user_id, article_id, created_at, updated_at)
|
||||
values (
|
||||
#{comment.id},
|
||||
#{comment.body},
|
||||
#{comment.userId},
|
||||
#{comment.articleId},
|
||||
#{comment.createdAt},
|
||||
#{comment.createdAt}
|
||||
)
|
||||
</insert>
|
||||
<select id="findById" resultMap="comment">
|
||||
select
|
||||
id commentId,
|
||||
body commentBody,
|
||||
user_id commentUserId,
|
||||
article_id commentArticleId,
|
||||
created_at commentCreatedAt
|
||||
from comments
|
||||
where id = #{id}
|
||||
</select>
|
||||
<resultMap id="comment" type="io.spring.core.comment.Comment">
|
||||
<id column="commentId" property="id"/>
|
||||
<result column="commentBody" property="body"/>
|
||||
<result column="commentUserId" property="userId"/>
|
||||
<result column="commentArticleId" property="articleId"/>
|
||||
<result column="commentCreatedAt" property="createdAt"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
23
src/main/resources/mapper/CommentReadService.xml
Normal file
23
src/main/resources/mapper/CommentReadService.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="io.spring.application.comment.CommentReadService">
|
||||
<select id="findById" resultMap="commentData">
|
||||
SELECT
|
||||
C.id commentId,
|
||||
C.body commentBody,
|
||||
C.created_at commentCreatedAt,
|
||||
<include refid="io.spring.application.article.ArticleReadService.profileColumns"/>
|
||||
from comments C
|
||||
left join users U
|
||||
on C.user_id = U.id
|
||||
where C.id = #{id}
|
||||
</select>
|
||||
|
||||
<resultMap id="commentData" type="io.spring.application.comment.CommentData">
|
||||
<id column="commentId" property="id"/>
|
||||
<result column="commentBody" property="body"/>
|
||||
<result column="commentCreatedAt" property="createdAt"/>
|
||||
<result column="commentCreatedAt" property="updatedAt"/>
|
||||
<association property="profileData" resultMap="io.spring.application.article.ArticleReadService.profileData"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user