should create article success

This commit is contained in:
aisensiy
2017-08-15 10:47:18 +08:00
parent b1e8632c3b
commit 00f778c087
32 changed files with 825 additions and 37 deletions

View File

@@ -0,0 +1,67 @@
package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.article.ArticleQueryService;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
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.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;
@RestController
@RequestMapping(path = "/articles")
public class ArticlesApi {
private ArticleRepository articleRepository;
private ArticleQueryService articleQueryService;
@Autowired
public ArticlesApi(ArticleRepository articleRepository, ArticleQueryService articleQueryService) {
this.articleRepository = articleRepository;
this.articleQueryService = articleQueryService;
}
@PostMapping
public ResponseEntity createArticle(@Valid @RequestBody NewArticleParam newArticleParam,
BindingResult bindingResult,
@AuthenticationPrincipal User user) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
Article article = new Article(
articleRepository.toSlug(
newArticleParam.getTitle()),
newArticleParam.getTitle(),
newArticleParam.getDescription(),
newArticleParam.getBody(),
newArticleParam.getTagList(),
user.getId());
articleRepository.save(article);
return ResponseEntity.ok(articleQueryService.findById(article.getId(), user).get());
}
}
@Getter
@JsonRootName("article")
@NoArgsConstructor
class NewArticleParam {
@NotBlank(message = "can't be empty")
private String title;
@NotBlank(message = "can't be empty")
private String description;
@NotBlank(message = "can't be empty")
private String body;
private String[] tagList;
}

View File

@@ -0,0 +1,31 @@
package io.spring.application.article;
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;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonRootName("article")
public class ArticleData {
private String id;
private String slug;
private String title;
private String description;
private String body;
private boolean favorited;
private int favoritesCount;
private DateTime createdAt;
private DateTime updatedAt;
private List<String> tagList;
@JsonProperty("author")
private ProfileData profileData;
}

View File

@@ -0,0 +1,13 @@
package io.spring.application.article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Mapper
@Component
public interface ArticleFavoritesQueryService {
boolean isUserFavorite(@Param("userId") String userId, @Param("articleId") String articleId);
int articleFavoriteCount(@Param("articleId") String articleId);
}

View File

@@ -0,0 +1,39 @@
package io.spring.application.article;
import io.spring.application.profile.UserRelationshipQueryService;
import io.spring.core.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class ArticleQueryService {
private ArticleReadService articleReadService;
private UserRelationshipQueryService userRelationshipQueryService;
private ArticleFavoritesQueryService articleFavoritesQueryService;
@Autowired
public ArticleQueryService(ArticleReadService articleReadService,
UserRelationshipQueryService userRelationshipQueryService,
ArticleFavoritesQueryService articleFavoritesQueryService) {
this.articleReadService = articleReadService;
this.userRelationshipQueryService = userRelationshipQueryService;
this.articleFavoritesQueryService = articleFavoritesQueryService;
}
public Optional<ArticleData> findById(String id, User user) {
ArticleData articleData = articleReadService.ofId(id);
if (articleData == null) {
return Optional.empty();
} else {
articleData.setFavorited(articleFavoritesQueryService.isUserFavorite(user.getId(), id));
articleData.setFavoritesCount(articleFavoritesQueryService.articleFavoriteCount(id));
articleData.getProfileData().setFollowing(
userRelationshipQueryService.isUserFollowing(
user.getId(),
articleData.getProfileData().getId()));
return Optional.of(articleData);
}
}
}

View File

@@ -0,0 +1,11 @@
package io.spring.application.article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
@Mapper
public interface ArticleReadService {
ArticleData ofId(@Param("id") String id);
}

View File

@@ -0,0 +1,18 @@
package io.spring.application.profile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProfileData {
@JsonIgnore
private String id;
private String username;
private String bio;
private String image;
private boolean following;
}

View File

@@ -0,0 +1,11 @@
package io.spring.application.profile;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
@Mapper
public interface UserRelationshipQueryService {
boolean isUserFollowing(@Param("userId") String userId, @Param("anotherUserId") String anotherUserId);
}

View File

@@ -11,10 +11,8 @@ import javax.persistence.Id;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@JsonRootName("user")
public class UserData {
@Id
private String id;
private String email;
private String username;

View File

@@ -16,12 +16,12 @@ public class UserQueryService {
}
public UserWithToken fetchNewAuthenticatedUser(String username) {
UserData userData = userReadService.findOne(username);
UserData userData = userReadService.findByUsername(username);
return new UserWithToken(userData, jwtService.toToken(userData));
}
public UserWithToken fetchCurrentUser(String username, String token) {
return new UserWithToken(userReadService.findOne(username), token);
return new UserWithToken(userReadService.findByUsername(username), token);
}
}

View File

@@ -1,8 +1,13 @@
package io.spring.application.user;
import org.springframework.data.repository.CrudRepository;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
public interface UserReadService extends CrudRepository<UserData, String> {
@Component
@Mapper
public interface UserReadService {
UserData findByUsername(@Param("username") String username);
}

View File

@@ -0,0 +1,41 @@
package io.spring.core.article;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
@Getter
@NoArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class Article {
private String userId;
private String id;
private String slug;
private String title;
private String description;
private String body;
private List<Tag> tags;
private DateTime createdAt;
private DateTime updatedAt;
public Article(String slug, String title, String description, String body, String[] tagList, String userId) {
this.id = UUID.randomUUID().toString();
this.slug = slug;
this.title = title;
this.description = description;
this.body = body;
this.tags = Arrays.stream(tagList).collect(toSet()).stream().map(Tag::new).collect(toList());
this.userId = userId;
this.createdAt = new DateTime();
this.updatedAt = new DateTime();
}
}

View File

@@ -0,0 +1,11 @@
package io.spring.core.article;
import java.util.Optional;
public interface ArticleRepository {
String toSlug(String title);
void save(Article article);
Optional<Article> findById(String id);
}

View File

@@ -0,0 +1,20 @@
package io.spring.core.article;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = "name")
public class Tag {
private String id;
private String name;
public Tag(String name) {
this.id = UUID.randomUUID().toString();
this.name = name;
}
}

View File

@@ -9,7 +9,7 @@ import java.util.UUID;
@Getter
@NoArgsConstructor
@EqualsAndHashCode(of = {"username"})
@EqualsAndHashCode(of = {"id"})
public class User {
private String id;
private String email;

View File

@@ -0,0 +1,21 @@
package io.spring.infrastructure.article;
import io.spring.core.article.Article;
import io.spring.core.article.Tag;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Component
@Mapper
public interface ArticleMapper {
void insert(@Param("article") Article article);
Article findById(@Param("id") String id);
boolean findTag(@Param("tagName") String tagName);
void insertTag(@Param("tag") Tag tag);
void insertArticleTagRelation(@Param("articleId") String articleId, @Param("tagId") String tagId);
}

View File

@@ -0,0 +1,38 @@
package io.spring.infrastructure.article;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class MyBatisArticleRepository implements ArticleRepository {
private ArticleMapper articleMapper;
public MyBatisArticleRepository(ArticleMapper articleMapper) {
this.articleMapper = articleMapper;
}
@Override
public String toSlug(String title) {
return title.toLowerCase().replace(' ', '-');
}
@Override
public void save(Article article) {
articleMapper.insert(article);
for (Tag tag : article.getTags()) {
if (!articleMapper.findTag(tag.getName())) {
articleMapper.insertTag(tag);
}
articleMapper.insertArticleTagRelation(article.getId(), tag.getId());
}
}
@Override
public Optional<Article> findById(String id) {
return Optional.ofNullable(articleMapper.findById(id));
}
}

View File

@@ -0,0 +1,44 @@
package io.spring.infrastructure.mybatis;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.joda.time.DateTime;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
@MappedTypes(DateTime.class)
public class DateTimeHandler implements TypeHandler<DateTime> {
private static final Calendar UTC_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
@Override
public void setParameter(PreparedStatement ps, int i, DateTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, parameter != null ? new Timestamp(parameter.getMillis()) : null, UTC_CALENDAR);
}
@Override
public DateTime getResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName, UTC_CALENDAR);
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
}
@Override
public DateTime getResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex, UTC_CALENDAR);
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
}
@Override
public DateTime getResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp ts = cs.getTimestamp(columnIndex, UTC_CALENDAR);
return ts != null ? new DateTime(ts.getTime()) : null;
}
}

View File

@@ -4,4 +4,4 @@ image.default=https://static.productionready.io/images/smiley-cyrus.jpg
jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA
jwt.sessionTime=86400
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=mapper/*Mapper.xml
mybatis.mapper-locations=mapper/*.xml

View File

@@ -6,3 +6,34 @@ create table users (
bio text,
image varchar(511)
);
create table articles (
id varchar(255) primary key,
user_id varchar(255),
slug varchar(255) UNIQUE,
title varchar(255),
description text,
body text,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create table article_favorites (
article_id varchar(255) not null,
user_id varchar(255) not null
);
create table follows (
user_id varchar(255) not null,
follow_id varchar(255) not null
);
create table tags (
id varchar(255) primary key,
name varchar(255)
);
create table article_tags (
article_id varchar(255) not null,
tag_id varchar(255) not null
);

View File

@@ -0,0 +1,11 @@
<?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.ArticleFavoritesQueryService">
<select id="isUserFavorite" resultType="java.lang.Boolean">
select count(1) from article_favorites where user_id = #{userId} and article_id = #{articleId}
</select>
<select id="articleFavoriteCount" resultType="java.lang.Integer">
select count(1) from article_favorites where article_id = #{articleId}
</select>
</mapper>

View File

@@ -0,0 +1,60 @@
<?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.article.ArticleMapper">
<insert id="insert">
insert into articles(id, slug, title, description, body, user_id, created_at, updated_at)
values(
#{article.id},
#{article.slug},
#{article.title},
#{article.description},
#{article.body},
#{article.userId},
#{article.createdAt},
#{article.updatedAt})
</insert>
<insert id="insertTag">
insert into tags (id, name) values (#{tag.id}, #{tag.name})
</insert>
<insert id="insertArticleTagRelation">
insert into article_tags (article_id, tag_id) values(#{articleId}, #{tagId})
</insert>
<select id="findById" resultMap="article">
select
A.id articleId,
A.slug articleSlug,
A.title articleTitle,
A.description articleDescription,
A.body articleBody,
A.user_id articleUserId,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
T.id tagId,
T.name tagName
from articles A
left join article_tags AT on A.id = AT.article_id
left join tags T on T.id = AT.tag_id
where A.id = #{id}
</select>
<select id="findTag" resultType="java.lang.Boolean">
select count(*) from tags where name = #{tagName}
</select>
<resultMap id="article" type="io.spring.core.article.Article">
<id column="articleId" property="id"/>
<result column="articleUserId" property="userId"/>
<result column="articleTitle" property="title"/>
<result column="articleSlug" property="slug"/>
<result column="articleDescription" property="description"/>
<result column="articleBody" property="body"/>
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<collection property="tags" ofType="arraylist" resultMap="tag"/>
</resultMap>
<resultMap id="tag" type="io.spring.core.article.Tag">
<id column="tagId" property="id"/>
<result column="tagName" property="name"/>
</resultMap>
</mapper>

View File

@@ -0,0 +1,44 @@
<?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">
<select id="ofId" resultMap="articleData">
select
A.id articleId,
A.slug articleSlug,
A.title articleTitle,
A.description articleDescription,
A.body articleBody,
A.created_at articleCreatedAt,
A.updated_at articleUpdatedAt,
T.name as tagName,
U.id userId,
U.username userUsername,
U.bio userBio,
U.image userImage
from
articles A
left join article_tags AT on A.id = AT.article_id
left join tags T on T.id = AT.article_id
left join users U on U.id = A.user_id
where A.id = #{id}
</select>
<resultMap id="articleData" type="io.spring.application.article.ArticleData">
<id column="articleId" property="id"/>
<result column="articleSlug" property="slug"/>
<result column="articleTitle" property="title"/>
<result column="articleDescription" property="description"/>
<result column="articleBody" property="body"/>
<result column="articleCreatedAt" property="createdAt"/>
<result column="articleUpdatedAt" property="updatedAt"/>
<association property="profileData" resultMap="profileData"/>
<collection property="tagList" ofType="arraylist" column="tagName" javaType="string"/>
</resultMap>
<resultMap id="profileData" type="io.spring.application.profile.ProfileData">
<id column="userId" property="id"/>
<result column="userUsername" property="username"/>
<result column="userBio" property="bio"/>
<result column="userImage" property="image"/>
</resultMap>
</mapper>

View File

@@ -0,0 +1,7 @@
<?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.user.UserReadService">
<select id="findByUsername" resultType="io.spring.application.user.UserData">
select * from users where username = #{username}
</select>
</mapper>

View File

@@ -0,0 +1,7 @@
<?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.profile.UserRelationshipQueryService">
<select id="isUserFollowing" resultType="java.lang.Boolean">
select count(1) from follows where user_id = #{userId} and follow_id = #{anotherUserId}
</select>
</mapper>

View File

@@ -17,6 +17,11 @@
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeHandlers>
<typeHandler handler="io.spring.infrastructure.mybatis.DateTimeHandler"
javaType="org.joda.time.DateTime"/>
</typeHandlers>
<!-- Continue going here -->
</configuration>