structure update

This commit is contained in:
aisensiy 2017-08-18 17:08:27 +08:00
parent ce6a56e1db
commit b97bf72311
68 changed files with 225 additions and 183 deletions

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -36,7 +37,7 @@ public class JacksonCustomizations {
if (value == null) {
gen.writeNull();
} else {
gen.writeString(value.toString());
gen.writeString(ISODateTimeFormat.dateTime().withZoneUTC().print(value));
}
}
}

View File

@ -3,9 +3,9 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.NoAuthorizationException;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.AuthorizationService;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.core.service.AuthorizationService;
import io.spring.application.data.ArticleData;
import io.spring.application.ArticleQueryService;
import io.spring.core.article.ArticleRepository;
import io.spring.core.user.User;
import lombok.Getter;

View File

@ -1,8 +1,8 @@
package io.spring.api;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.data.ArticleData;
import io.spring.application.ArticleQueryService;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.favorite.ArticleFavorite;

View File

@ -3,7 +3,7 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.Page;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.ArticleQueryService;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.user.User;

View File

@ -4,9 +4,9 @@ import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.api.exception.NoAuthorizationException;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.AuthorizationService;
import io.spring.application.comment.CommentData;
import io.spring.application.comment.CommentQueryService;
import io.spring.core.service.AuthorizationService;
import io.spring.application.data.CommentData;
import io.spring.application.CommentQueryService;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.comment.Comment;

View File

@ -2,8 +2,9 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.user.UserQueryService;
import io.spring.application.user.UserWithToken;
import io.spring.application.UserQueryService;
import io.spring.application.UserWithToken;
import io.spring.application.data.UserData;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import lombok.Getter;
@ -40,11 +41,15 @@ public class CurrentUserApi {
@GetMapping
public ResponseEntity currentUser(@AuthenticationPrincipal User currentUser,
@RequestHeader(value = "Authorization") String authorization) {
return ResponseEntity.ok(userResponse(userQueryService.fetchCurrentUser(currentUser.getUsername(), authorization.split(" ")[1])));
UserData userData = userQueryService.findById(currentUser.getId()).get();
return ResponseEntity.ok(userResponse(
new UserWithToken(userData, authorization.split(" ")[1])
));
}
@PutMapping
public ResponseEntity updateProfile(@AuthenticationPrincipal User currentUser,
@RequestHeader("Authorization") String token,
@Valid @RequestBody UpdateUserParam updateUserParam,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
@ -59,7 +64,10 @@ public class CurrentUserApi {
updateUserParam.getBio(),
updateUserParam.getImage());
userRepository.save(currentUser);
return ResponseEntity.ok(userResponse(userQueryService.fetchNewAuthenticatedUser(currentUser.getUsername())));
UserData userData = userQueryService.findById(currentUser.getId()).get();
return ResponseEntity.ok(userResponse(
new UserWithToken(userData, token.split(" ")[1])
));
}
private void checkUniquenessOfUsernameAndEmail(User currentUser, UpdateUserParam updateUserParam, BindingResult bindingResult) {

View File

@ -1,8 +1,8 @@
package io.spring.api;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.profile.ProfileData;
import io.spring.application.profile.ProfileQueryService;
import io.spring.application.data.ProfileData;
import io.spring.application.ProfileQueryService;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;

View File

@ -1,6 +1,6 @@
package io.spring.api;
import io.spring.application.tag.TagsQueryService;
import io.spring.application.TagsQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

View File

@ -2,8 +2,10 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.user.UserQueryService;
import io.spring.application.user.UserWithToken;
import io.spring.application.UserQueryService;
import io.spring.application.UserWithToken;
import io.spring.application.data.UserData;
import io.spring.core.service.JwtService;
import io.spring.core.user.EncryptService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
@ -32,32 +34,25 @@ public class UsersApi {
private UserQueryService userQueryService;
private String defaultImage;
private EncryptService encryptService;
private JwtService jwtService;
@Autowired
public UsersApi(UserRepository userRepository,
UserQueryService userQueryService,
EncryptService encryptService,
@Value("${image.default}") String defaultImage) {
@Value("${image.default}") String defaultImage,
JwtService jwtService) {
this.userRepository = userRepository;
this.userQueryService = userQueryService;
this.encryptService = encryptService;
this.defaultImage = defaultImage;
this.jwtService = jwtService;
}
@RequestMapping(path = "/users", method = POST)
public ResponseEntity createUser(@Valid @RequestBody RegisterParam registerParam, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
if (userRepository.findByUsername(registerParam.getUsername()).isPresent()) {
bindingResult.rejectValue("username", "DUPLICATED", "duplicated username");
throw new InvalidRequestException(bindingResult);
}
checkInput(registerParam, bindingResult);
if (userRepository.findByEmail(registerParam.getEmail()).isPresent()) {
bindingResult.rejectValue("email", "DUPLICATED", "duplicated email");
throw new InvalidRequestException(bindingResult);
}
User user = new User(
registerParam.getEmail(),
registerParam.getUsername(),
@ -65,14 +60,33 @@ public class UsersApi {
"",
defaultImage);
userRepository.save(user);
return ResponseEntity.status(201).body(userResponse(userQueryService.fetchNewAuthenticatedUser(user.getUsername())));
UserData userData = userQueryService.findById(user.getId()).get();
return ResponseEntity.status(201).body(userResponse(new UserWithToken(userData, jwtService.toToken(user))));
}
private void checkInput(@Valid @RequestBody RegisterParam registerParam, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
if (userRepository.findByUsername(registerParam.getUsername()).isPresent()) {
bindingResult.rejectValue("username", "DUPLICATED", "duplicated username");
}
if (userRepository.findByEmail(registerParam.getEmail()).isPresent()) {
bindingResult.rejectValue("email", "DUPLICATED", "duplicated email");
}
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
}
}
@RequestMapping(path = "/users/login", method = POST)
public ResponseEntity userLogin(@Valid @RequestBody LoginParam loginParam, BindingResult bindingResult) {
Optional<User> optional = userRepository.findByEmail(loginParam.getEmail());
if (optional.isPresent() && encryptService.check(loginParam.getPassword(), optional.get().getPassword())) {
return ResponseEntity.ok(userResponse(userQueryService.fetchNewAuthenticatedUser(optional.get().getUsername())));
UserData userData = userQueryService.findById(optional.get().getId()).get();
return ResponseEntity.ok(userResponse(new UserWithToken(userData, jwtService.toToken(optional.get()))));
} else {
bindingResult.rejectValue("password", "INVALID", "invalid email or password");
throw new InvalidRequestException(bindingResult);

View File

@ -1,7 +1,6 @@
package io.spring.api.security;
import io.spring.application.JwtService;
import io.spring.core.user.User;
import io.spring.core.service.JwtService;
import io.spring.core.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

View File

@ -1,4 +1,4 @@
package io.spring.application.article;
package io.spring.application;
import io.spring.core.user.User;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,8 +1,10 @@
package io.spring.application.article;
package io.spring.application;
import io.spring.application.Page;
import io.spring.application.profile.UserRelationshipQueryService;
import io.spring.application.data.ArticleData;
import io.spring.application.data.ArticleDataList;
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import io.spring.core.user.User;
import io.spring.infrastructure.mybatis.readservice.ArticleReadService;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,7 +1,9 @@
package io.spring.application.comment;
package io.spring.application;
import io.spring.application.profile.UserRelationshipQueryService;
import io.spring.application.data.CommentData;
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import io.spring.core.user.User;
import io.spring.infrastructure.mybatis.readservice.CommentReadService;
import org.springframework.stereotype.Service;
import java.util.List;

View File

@ -1,8 +1,10 @@
package io.spring.application.profile;
package io.spring.application;
import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService;
import io.spring.application.data.ProfileData;
import io.spring.application.data.UserData;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import io.spring.core.user.User;
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@ -1,5 +1,6 @@
package io.spring.application.tag;
package io.spring.application;
import io.spring.infrastructure.mybatis.readservice.TagReadService;
import org.springframework.stereotype.Service;
import java.util.List;

View File

@ -0,0 +1,24 @@
package io.spring.application;
import io.spring.application.data.UserData;
import io.spring.core.service.JwtService;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class UserQueryService {
private UserReadService userReadService;
private JwtService jwtService;
public UserQueryService(UserReadService userReadService, JwtService jwtService) {
this.userReadService = userReadService;
this.jwtService = jwtService;
}
public Optional<UserData> findById(String id) {
return Optional.ofNullable(userReadService.findById(id));
}
}

View File

@ -1,5 +1,6 @@
package io.spring.application.user;
package io.spring.application;
import io.spring.application.data.UserData;
import lombok.Getter;
@Getter

View File

@ -1,7 +1,6 @@
package io.spring.application.article;
package io.spring.application.data;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.spring.application.profile.ProfileData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -1,4 +1,4 @@
package io.spring.application.article;
package io.spring.application.data;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

View File

@ -1,8 +1,7 @@
package io.spring.application.comment;
package io.spring.application.data;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.spring.application.profile.ProfileData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

View File

@ -1,4 +1,4 @@
package io.spring.application.profile;
package io.spring.application.data;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;

View File

@ -1,4 +1,4 @@
package io.spring.application.user;
package io.spring.application.data;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,25 +0,0 @@
package io.spring.application.user;
import io.spring.application.JwtService;
import org.springframework.stereotype.Service;
@Service
public class UserQueryService {
private UserReadService userReadService;
private JwtService jwtService;
public UserQueryService(UserReadService userReadService, JwtService jwtService) {
this.userReadService = userReadService;
this.jwtService = jwtService;
}
public UserWithToken fetchNewAuthenticatedUser(String username) {
UserData userData = userReadService.findByUsername(username);
return new UserWithToken(userData, jwtService.toToken(userData));
}
public UserWithToken fetchCurrentUser(String username, String token) {
return new UserWithToken(userReadService.findByUsername(username), token);
}
}

View File

@ -1,4 +1,4 @@
package io.spring.application;
package io.spring.core.service;
import io.spring.core.article.Article;
import io.spring.core.comment.Comment;

View File

@ -1,13 +1,13 @@
package io.spring.application;
package io.spring.core.service;
import io.spring.application.user.UserData;
import io.spring.core.user.User;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public interface JwtService {
String toToken(UserData userData);
String toToken(User user);
Optional<String> getSubFromToken(String token);
}

View File

@ -1,4 +1,4 @@
package io.spring.infrastructure.favorite;
package io.spring.infrastructure.mybatis.mapper;
import io.spring.core.favorite.ArticleFavorite;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,4 +1,4 @@
package io.spring.infrastructure.article;
package io.spring.infrastructure.mybatis.mapper;
import io.spring.core.article.Article;
import io.spring.core.article.Tag;

View File

@ -1,4 +1,4 @@
package io.spring.infrastructure.comment;
package io.spring.infrastructure.mybatis.mapper;
import io.spring.core.comment.Comment;
import org.apache.ibatis.annotations.Mapper;

View File

@ -1,4 +1,4 @@
package io.spring.infrastructure.user;
package io.spring.infrastructure.mybatis.mapper;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;

View File

@ -1,6 +1,7 @@
package io.spring.application.article;
package io.spring.infrastructure.mybatis.readservice;
import io.spring.application.Page;
import io.spring.application.data.ArticleData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

View File

@ -1,5 +1,6 @@
package io.spring.application.comment;
package io.spring.infrastructure.mybatis.readservice;
import io.spring.application.data.CommentData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

View File

@ -1,4 +1,4 @@
package io.spring.application.tag;
package io.spring.infrastructure.mybatis.readservice;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

View File

@ -1,5 +1,6 @@
package io.spring.application.user;
package io.spring.infrastructure.mybatis.readservice;
import io.spring.application.data.UserData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@ -9,5 +10,7 @@ import org.springframework.stereotype.Component;
public interface UserReadService {
UserData findByUsername(@Param("username") String username);
UserData findById(@Param("id") String id);
}

View File

@ -1,4 +1,4 @@
package io.spring.application.profile;
package io.spring.infrastructure.mybatis.readservice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

View File

@ -1,7 +1,8 @@
package io.spring.infrastructure.favorite;
package io.spring.infrastructure.repository;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.infrastructure.mybatis.mapper.ArticleFavoriteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

View File

@ -1,8 +1,9 @@
package io.spring.infrastructure.article;
package io.spring.infrastructure.repository;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import io.spring.infrastructure.mybatis.mapper.ArticleMapper;
import org.springframework.stereotype.Repository;
import java.util.Optional;

View File

@ -1,7 +1,8 @@
package io.spring.infrastructure.comment;
package io.spring.infrastructure.repository;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.infrastructure.mybatis.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@ -1,8 +1,9 @@
package io.spring.infrastructure.user;
package io.spring.infrastructure.repository;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

View File

@ -4,8 +4,8 @@ import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.core.service.JwtService;
import io.spring.core.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -26,9 +26,9 @@ public class DefaultJwtService implements JwtService {
}
@Override
public String toToken(UserData userData) {
public String toToken(User user) {
return Jwts.builder()
.setSubject(userData.getId())
.setSubject(user.getId())
.setExpiration(expireTimeFromNow())
.signWith(SignatureAlgorithm.HS512, secret)
.compact();

View File

@ -4,4 +4,4 @@ jwt.secret=nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCog
jwt.sessionTime=86400
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=mapper/*.xml
logging.level.io.spring.application.article.ArticleReadService=DEBUG
logging.level.io.spring.infrastructure.mybatis.readservice.ArticleReadService=DEBUG

View File

@ -1,6 +1,6 @@
<?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.favorite.ArticleFavoriteMapper">
<mapper namespace="io.spring.infrastructure.mybatis.mapper.ArticleFavoriteMapper">
<insert id="insert">
insert into article_favorites (article_id, user_id) values (#{articleFavorite.articleId}, #{articleFavorite.userId})
</insert>

View File

@ -1,6 +1,6 @@
<?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">
<mapper namespace="io.spring.application.ArticleFavoritesQueryService">
<select id="isUserFavorite" resultType="java.lang.Boolean">
select count(1) from article_favorites where user_id = #{userId} and article_id = #{articleId}
</select>
@ -28,7 +28,7 @@
and AF.user_id = #{currentUser.id}
</select>
<resultMap id="favoriteCount" type="io.spring.application.article.ArticleFavoriteCount">
<resultMap id="favoriteCount" type="io.spring.application.ArticleFavoriteCount">
<id column="id" property="id"/>
<result column="favoriteCount" property="count"/>
</resultMap>

View File

@ -1,6 +1,6 @@
<?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">
<mapper namespace="io.spring.infrastructure.mybatis.mapper.ArticleMapper">
<insert id="insert">
insert into articles(id, slug, title, description, body, user_id, created_at, updated_at)
values(

View File

@ -1,6 +1,6 @@
<?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">
<mapper namespace="io.spring.infrastructure.mybatis.readservice.ArticleReadService">
<sql id="profileColumns">
U.id userId,
U.username userUsername,
@ -81,6 +81,7 @@
<foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
order by A.created_at desc
</select>
<select id="findArticlesOfAuthors" resultMap="articleData">
<include refid="selectArticleData"/>
@ -101,7 +102,7 @@
<id javaType="string" column="articleId"/>
</resultMap>
<resultMap id="articleData" type="io.spring.application.article.ArticleData">
<resultMap id="articleData" type="io.spring.application.data.ArticleData">
<id column="articleId" property="id"/>
<result column="articleSlug" property="slug"/>
<result column="articleTitle" property="title"/>
@ -115,7 +116,7 @@
</collection>
</resultMap>
<resultMap id="profileData" type="io.spring.application.profile.ProfileData">
<resultMap id="profileData" type="io.spring.application.data.ProfileData">
<id column="userId" property="id"/>
<result column="userUsername" property="username"/>
<result column="userBio" property="bio"/>

View File

@ -1,6 +1,6 @@
<?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">
<mapper namespace="io.spring.infrastructure.mybatis.mapper.CommentMapper">
<insert id="insert">
insert into comments(id, body, user_id, article_id, created_at, updated_at)
values (

View File

@ -1,12 +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.comment.CommentReadService">
<mapper namespace="io.spring.infrastructure.mybatis.readservice.CommentReadService">
<sql id="selectCommentData">
SELECT
C.id commentId,
C.body commentBody,
C.created_at commentCreatedAt,
<include refid="io.spring.application.article.ArticleReadService.profileColumns"/>
<include refid="io.spring.infrastructure.mybatis.readservice.ArticleReadService.profileColumns"/>
from comments C
left join users U
on C.user_id = U.id
@ -21,11 +21,11 @@
where C.article_id = #{articleId}
</select>
<resultMap id="commentData" type="io.spring.application.comment.CommentData">
<resultMap id="commentData" type="io.spring.application.data.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"/>
<association property="profileData" resultMap="io.spring.infrastructure.mybatis.readservice.ArticleReadService.profileData"/>
</resultMap>
</mapper>

View File

@ -1,6 +1,6 @@
<?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.tag.TagReadService">
<mapper namespace="io.spring.infrastructure.mybatis.readservice.TagReadService">
<select id="all" resultType="string">
select name from tags
</select>

View File

@ -1,6 +1,6 @@
<?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.user.UserMapper">
<mapper namespace="io.spring.infrastructure.mybatis.mapper.UserMapper">
<insert id="insert">
insert into users (id, username, email, password, bio, image) values(
#{user.id},

View File

@ -1,7 +1,10 @@
<?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">
<mapper namespace="io.spring.infrastructure.mybatis.readservice.UserReadService">
<select id="findByUsername" resultType="io.spring.application.data.UserData">
select * from users where username = #{username}
</select>
<select id="findById" resultType="io.spring.application.data.UserData">
select * from users where id = #{id}
</select>
</mapper>

View File

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

View File

@ -1,7 +1,7 @@
package io.spring;
import io.spring.application.article.ArticleData;
import io.spring.application.profile.ProfileData;
import io.spring.application.data.ArticleData;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.user.User;
import org.joda.time.DateTime;

View File

@ -1,9 +1,9 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.profile.ProfileData;
import io.spring.application.data.ArticleData;
import io.spring.application.ArticleQueryService;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;

View File

@ -2,13 +2,14 @@ package io.spring.api;
import io.restassured.RestAssured;
import io.spring.TestHelper;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.profile.ProfileData;
import io.spring.application.data.ArticleData;
import io.spring.application.ArticleQueryService;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.user.User;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -127,7 +128,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
.statusCode(200)
.body("article.slug", equalTo(slug))
.body("article.body", equalTo(articleData.getBody()))
.body("article.createdAt", equalTo(time.toDateTimeISO().toString()));
.body("article.createdAt", equalTo(ISODateTimeFormat.dateTime().withZoneUTC().print(time)));
}

View File

@ -1,10 +1,9 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.comment.CommentData;
import io.spring.application.comment.CommentQueryService;
import io.spring.application.profile.ProfileData;
import io.spring.application.user.UserData;
import io.spring.application.CommentQueryService;
import io.spring.application.data.CommentData;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.comment.Comment;
@ -142,13 +141,8 @@ public class CommentsApiTest extends TestWithCurrentUser {
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
String token = jwtService.toToken(
new UserData(
anotherUser.getId(),
anotherUser.getEmail(),
anotherUser.getUsername(),
anotherUser.getBio(),
anotherUser.getImage()));
String token = jwtService.toToken(anotherUser);
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.of(anotherUser));
given()
.header("Authorization", "Token " + token)
.when()

View File

@ -1,16 +1,13 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.application.data.UserData;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
@ -18,9 +15,6 @@ import java.util.Map;
import java.util.Optional;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@ -93,6 +87,9 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
}});
}};
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
given()
@ -102,8 +99,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
.when()
.put("/user")
.then()
.statusCode(200)
.body("user.token", not(token));
.statusCode(200);
}
@Test

View File

@ -2,8 +2,8 @@ package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.Page;
import io.spring.application.article.ArticleDataList;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.data.ArticleDataList;
import io.spring.application.ArticleQueryService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -1,13 +1,11 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.profile.ProfileData;
import io.spring.application.profile.ProfileQueryService;
import io.spring.application.profile.UserRelationshipQueryService;
import io.spring.application.data.ProfileData;
import io.spring.application.ProfileQueryService;
import io.spring.core.article.Article;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -1,8 +1,8 @@
package io.spring.api;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService;
import io.spring.core.service.JwtService;
import io.spring.application.data.UserData;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -37,10 +37,11 @@ class TestWithCurrentUser {
user = new User(email, username, "123", "", defaultAvatar);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
when(userRepository.findById(eq(user.getId()))).thenReturn(Optional.of(user));
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
when(userReadService.findById(eq(user.getId()))).thenReturn(userData);
token = jwtService.toToken(userData);
token = jwtService.toToken(user);
}
}

View File

@ -1,9 +1,9 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService;
import io.spring.core.service.JwtService;
import io.spring.application.data.UserData;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import org.junit.Before;
@ -54,8 +54,9 @@ public class UsersApiTest {
String username = "johnjacob";
when(jwtService.toToken(any())).thenReturn("123");
UserData userData = new UserData("123", email, username, "", defaultAvatar);
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
User user = new User(email, username, "123", "", defaultAvatar);
UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userReadService.findById(any())).thenReturn(userData);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.empty());
@ -122,6 +123,7 @@ public class UsersApiTest {
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(new User(
email, username, "123", "bio", ""
)));
when(userRepository.findByEmail(any())).thenReturn(Optional.empty());
Map<String, Object> param = prepareRegisterParameter(email, username);
@ -179,6 +181,7 @@ public class UsersApiTest {
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userReadService.findByUsername(eq(username))).thenReturn(userData);
when(userReadService.findById(eq(user.getId()))).thenReturn(userData);
when(jwtService.toToken(any())).thenReturn("123");
Map<String, Object> param = new HashMap<String, Object>() {{

View File

@ -1,6 +1,9 @@
package io.spring.application.article;
import io.spring.application.ArticleQueryService;
import io.spring.application.Page;
import io.spring.application.data.ArticleData;
import io.spring.application.data.ArticleDataList;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.favorite.ArticleFavorite;
@ -8,9 +11,9 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.favorite.MyBatisArticleFavoriteRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
@ -22,10 +25,9 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@MybatisTest
@ -54,7 +56,7 @@ public class ArticleQueryServiceTest {
public void setUp() throws Exception {
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
userRepository.save(user);
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId(), new DateTime());
articleRepository.save(article);
}

View File

@ -1,5 +1,7 @@
package io.spring.application.comment;
import io.spring.application.CommentQueryService;
import io.spring.application.data.CommentData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.comment.Comment;
@ -7,9 +9,9 @@ import io.spring.core.comment.CommentRepository;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.comment.MyBatisCommentRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisCommentRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -1,8 +1,10 @@
package io.spring.application.profile;
import io.spring.application.ProfileQueryService;
import io.spring.application.data.ProfileData;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;

View File

@ -1,8 +1,9 @@
package io.spring.application.tag;
import io.spring.application.TagsQueryService;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;

View File

@ -5,7 +5,8 @@ import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.user.MyBatisUserRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -2,6 +2,7 @@ package io.spring.infrastructure.comment;
import io.spring.core.comment.Comment;
import io.spring.core.comment.CommentRepository;
import io.spring.infrastructure.repository.MyBatisCommentRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;

View File

@ -2,6 +2,7 @@ package io.spring.infrastructure.favorite;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
@ -21,7 +22,7 @@ public class MyBatisArticleFavoriteRepositoryTest {
private ArticleFavoriteRepository articleFavoriteRepository;
@Autowired
private ArticleFavoriteMapper articleFavoriteMapper;
private io.spring.infrastructure.mybatis.mapper.ArticleFavoriteMapper articleFavoriteMapper;
@Test
public void should_save_and_fetch_articleFavorite_success() throws Exception {

View File

@ -1,7 +1,7 @@
package io.spring.infrastructure.service;
import io.spring.application.JwtService;
import io.spring.application.user.UserData;
import io.spring.core.service.JwtService;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
@ -9,7 +9,7 @@ import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
public class DefaultJwtServiceTest {
@ -22,14 +22,12 @@ public class DefaultJwtServiceTest {
@Test
public void should_generate_and_parse_token() throws Exception {
String username = "aisensiy";
UserData userData = new UserData("123", "aisensiy@163.com", username, "", "");
String token = jwtService.toToken(userData);
User user = new User("email@email.com", "username", "123", "", "");
String token = jwtService.toToken(user);
assertThat(token, notNullValue());
Optional<String> optional = jwtService.getSubFromToken(token);
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is(username));
assertThat(optional.get(), is(user.getId()));
}
@Test

View File

@ -3,6 +3,7 @@ package io.spring.infrastructure.user;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;