create comment

This commit is contained in:
aisensiy
2017-08-15 16:36:07 +08:00
parent 9dbb008dcb
commit 445311ee1b
15 changed files with 479 additions and 4 deletions

View 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;
}

View 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;
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View 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();
}
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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));
}
}