refactoring

This commit is contained in:
aisensiy 2017-08-25 11:34:41 +08:00
parent aa4f40ba74
commit cea03e01b9
16 changed files with 31 additions and 50 deletions

View File

@ -3,7 +3,7 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException; import io.spring.api.exception.InvalidRequestException;
import io.spring.application.UserQueryService; import io.spring.application.UserQueryService;
import io.spring.application.UserWithToken; import io.spring.application.data.UserWithToken;
import io.spring.application.data.UserData; import io.spring.application.data.UserData;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.core.user.UserRepository; import io.spring.core.user.UserRepository;

View File

@ -3,7 +3,7 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException; import io.spring.api.exception.InvalidRequestException;
import io.spring.application.UserQueryService; import io.spring.application.UserQueryService;
import io.spring.application.UserWithToken; import io.spring.application.data.UserWithToken;
import io.spring.application.data.UserData; import io.spring.application.data.UserData;
import io.spring.core.service.JwtService; import io.spring.core.service.JwtService;
import io.spring.core.user.EncryptService; import io.spring.core.user.EncryptService;

View File

@ -2,10 +2,11 @@ package io.spring.application;
import io.spring.application.data.ArticleData; import io.spring.application.data.ArticleData;
import io.spring.application.data.ArticleDataList; import io.spring.application.data.ArticleDataList;
import io.spring.application.data.ArticleFavoriteCount;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.infrastructure.mybatis.readservice.ArticleFavoritesReadService;
import io.spring.infrastructure.mybatis.readservice.ArticleReadService; import io.spring.infrastructure.mybatis.readservice.ArticleReadService;
import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService; import io.spring.infrastructure.mybatis.readservice.UserRelationshipQueryService;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,15 +23,15 @@ import static java.util.stream.Collectors.toList;
public class ArticleQueryService { public class ArticleQueryService {
private ArticleReadService articleReadService; private ArticleReadService articleReadService;
private UserRelationshipQueryService userRelationshipQueryService; private UserRelationshipQueryService userRelationshipQueryService;
private ArticleFavoritesQueryService articleFavoritesQueryService; private ArticleFavoritesReadService articleFavoritesReadService;
@Autowired @Autowired
public ArticleQueryService(ArticleReadService articleReadService, public ArticleQueryService(ArticleReadService articleReadService,
UserRelationshipQueryService userRelationshipQueryService, UserRelationshipQueryService userRelationshipQueryService,
ArticleFavoritesQueryService articleFavoritesQueryService) { ArticleFavoritesReadService articleFavoritesReadService) {
this.articleReadService = articleReadService; this.articleReadService = articleReadService;
this.userRelationshipQueryService = userRelationshipQueryService; this.userRelationshipQueryService = userRelationshipQueryService;
this.articleFavoritesQueryService = articleFavoritesQueryService; this.articleFavoritesReadService = articleFavoritesReadService;
} }
public Optional<ArticleData> findById(String id, User user) { public Optional<ArticleData> findById(String id, User user) {
@ -89,7 +90,7 @@ public class ArticleQueryService {
} }
private void setFavoriteCount(List<ArticleData> articles) { private void setFavoriteCount(List<ArticleData> articles) {
List<ArticleFavoriteCount> favoritesCounts = articleFavoritesQueryService.articlesFavoriteCount(articles.stream().map(ArticleData::getId).collect(toList())); List<ArticleFavoriteCount> favoritesCounts = articleFavoritesReadService.articlesFavoriteCount(articles.stream().map(ArticleData::getId).collect(toList()));
Map<String, Integer> countMap = new HashMap<>(); Map<String, Integer> countMap = new HashMap<>();
favoritesCounts.forEach(item -> { favoritesCounts.forEach(item -> {
countMap.put(item.getId(), item.getCount()); countMap.put(item.getId(), item.getCount());
@ -98,7 +99,7 @@ public class ArticleQueryService {
} }
private void setIsFavorite(List<ArticleData> articles, User currentUser) { private void setIsFavorite(List<ArticleData> articles, User currentUser) {
Set<String> favoritedArticles = articleFavoritesQueryService.userFavorites(articles.stream().map(articleData -> articleData.getId()).collect(toList()), currentUser); Set<String> favoritedArticles = articleFavoritesReadService.userFavorites(articles.stream().map(articleData -> articleData.getId()).collect(toList()), currentUser);
articles.forEach(articleData -> { articles.forEach(articleData -> {
if (favoritedArticles.contains(articleData.getId())) { if (favoritedArticles.contains(articleData.getId())) {
@ -108,8 +109,8 @@ public class ArticleQueryService {
} }
private void fillExtraInfo(String id, User user, ArticleData articleData) { private void fillExtraInfo(String id, User user, ArticleData articleData) {
articleData.setFavorited(articleFavoritesQueryService.isUserFavorite(user.getId(), id)); articleData.setFavorited(articleFavoritesReadService.isUserFavorite(user.getId(), id));
articleData.setFavoritesCount(articleFavoritesQueryService.articleFavoriteCount(id)); articleData.setFavoritesCount(articleFavoritesReadService.articleFavoriteCount(id));
articleData.getProfileData().setFollowing( articleData.getProfileData().setFollowing(
userRelationshipQueryService.isUserFollowing( userRelationshipQueryService.isUserFollowing(
user.getId(), user.getId(),
@ -129,8 +130,3 @@ public class ArticleQueryService {
} }
} }
@Value
class ArticleFavoriteCount {
private String id;
private Integer count;
}

View File

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

View File

@ -0,0 +1,9 @@
package io.spring.application.data;
import lombok.Value;
@Value
public class ArticleFavoriteCount {
private String id;
private Integer count;
}

View File

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

View File

@ -1,5 +1,6 @@
package io.spring.application; package io.spring.infrastructure.mybatis.readservice;
import io.spring.application.data.ArticleFavoriteCount;
import io.spring.core.user.User; import io.spring.core.user.User;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -10,7 +11,7 @@ import java.util.Set;
@Mapper @Mapper
@Component @Component
public interface ArticleFavoritesQueryService { public interface ArticleFavoritesReadService {
boolean isUserFavorite(@Param("userId") String userId, @Param("articleId") String articleId); boolean isUserFavorite(@Param("userId") String userId, @Param("articleId") String articleId);
int articleFavoriteCount(@Param("articleId") String articleId); int articleFavoriteCount(@Param("articleId") String articleId);

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?> <?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" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.spring.application.ArticleFavoritesQueryService"> <mapper namespace="io.spring.infrastructure.mybatis.readservice.ArticleFavoritesReadService">
<select id="isUserFavorite" resultType="java.lang.Boolean"> <select id="isUserFavorite" resultType="java.lang.Boolean">
select count(1) from article_favorites where user_id = #{userId} and article_id = #{articleId} select count(1) from article_favorites where user_id = #{userId} and article_id = #{articleId}
</select> </select>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?> <?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" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="transfer.data"> <mapper namespace="transfer.data">
<resultMap id="favoriteCount" type="io.spring.application.ArticleFavoriteCount"> <resultMap id="favoriteCount" type="io.spring.application.data.ArticleFavoriteCount">
<constructor> <constructor>
<idArg column="id" javaType="string"/> <idArg column="id" javaType="string"/>
<arg column="favoriteCount" javaType="integer"/> <arg column="favoriteCount" javaType="integer"/>

View File

@ -1,13 +0,0 @@
package io.spring.api;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
public abstract class ApiTestBase {
@Before
public void setUp() throws Exception {
}
}

View File

@ -89,7 +89,6 @@ public class ArticleApiTest extends TestWithCurrentUser {
Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId()); Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId());
DateTime time = new DateTime();
ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user); ArticleData articleData = TestHelper.getArticleDataFromArticleAndUser(article, user);
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
@ -168,7 +167,6 @@ public class ArticleApiTest extends TestWithCurrentUser {
String title = "new-title"; String title = "new-title";
String body = "new body"; String body = "new body";
String description = "new description"; String description = "new description";
Map<String, Object> updateParam = prepareUpdateParam(title, body, description);
User anotherUser = new User("test@test.com", "test", "123123", "", ""); User anotherUser = new User("test@test.com", "test", "123123", "", "");

View File

@ -46,13 +46,12 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
private ArticleQueryService articleQueryService; private ArticleQueryService articleQueryService;
private Article article; private Article article;
private User anotherUser;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
RestAssuredMockMvc.mockMvc(mvc); RestAssuredMockMvc.mockMvc(mvc);
anotherUser = new User("other@test.com", "other", "123", "", ""); User anotherUser = new User("other@test.com", "other", "123", "", "");
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId()); article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
ArticleData articleData = new ArticleData( ArticleData articleData = new ArticleData(

View File

@ -5,7 +5,6 @@ import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig; import io.spring.api.security.WebSecurityConfig;
import io.spring.application.ProfileQueryService; import io.spring.application.ProfileQueryService;
import io.spring.application.data.ProfileData; import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.user.FollowRelation; import io.spring.core.user.FollowRelation;
import io.spring.core.user.User; import io.spring.core.user.User;
import org.junit.Before; import org.junit.Before;
@ -27,7 +26,6 @@ import static org.mockito.Mockito.when;
@WebMvcTest(ProfileApi.class) @WebMvcTest(ProfileApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class}) @Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class ProfileApiTest extends TestWithCurrentUser { public class ProfileApiTest extends TestWithCurrentUser {
private Article article;
private User anotherUser; private User anotherUser;
@Autowired @Autowired

View File

@ -13,7 +13,7 @@ import java.util.Optional;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
abstract class TestWithCurrentUser extends ApiTestBase { abstract class TestWithCurrentUser {
@MockBean @MockBean
protected UserRepository userRepository; protected UserRepository userRepository;
@ -46,10 +46,8 @@ abstract class TestWithCurrentUser extends ApiTestBase {
when(jwtService.getSubFromToken(eq(token))).thenReturn(Optional.of(user.getId())); when(jwtService.getSubFromToken(eq(token))).thenReturn(Optional.of(user.getId()));
} }
@Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
userFixture(); userFixture();
} }
} }

View File

@ -31,7 +31,7 @@ import static org.mockito.Mockito.when;
@WebMvcTest(UsersApi.class) @WebMvcTest(UsersApi.class)
@Import({WebSecurityConfig.class, UserQueryService.class, NaiveEncryptService.class, JacksonCustomizations.class}) @Import({WebSecurityConfig.class, UserQueryService.class, NaiveEncryptService.class, JacksonCustomizations.class})
public class UsersApiTest extends ApiTestBase { public class UsersApiTest {
@Autowired @Autowired
private MockMvc mvc; private MockMvc mvc;

View File

@ -31,13 +31,12 @@ public class MyBatisArticleRepositoryTest {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
private User user;
private Article article; private Article article;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default"); User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
userRepository.save(user); 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());
} }