From b97bf72311f0328c8bf401f39b4ea633a975ee67 Mon Sep 17 00:00:00 2001 From: aisensiy Date: Fri, 18 Aug 2017 17:08:27 +0800 Subject: [PATCH] structure update --- .../java/io/spring/JacksonCustomizations.java | 3 +- src/main/java/io/spring/api/ArticleApi.java | 6 +-- .../io/spring/api/ArticleFavoriteApi.java | 4 +- src/main/java/io/spring/api/ArticlesApi.java | 2 +- src/main/java/io/spring/api/CommentsApi.java | 6 +-- .../java/io/spring/api/CurrentUserApi.java | 16 +++++-- src/main/java/io/spring/api/ProfileApi.java | 4 +- src/main/java/io/spring/api/TagsApi.java | 2 +- src/main/java/io/spring/api/UsersApi.java | 46 ++++++++++++------- .../spring/api/security/JwtTokenFilter.java | 3 +- .../ArticleFavoritesQueryService.java | 2 +- .../{article => }/ArticleQueryService.java | 8 ++-- .../{comment => }/CommentQueryService.java | 6 ++- .../{profile => }/ProfileQueryService.java | 8 ++-- .../{tag => }/TagsQueryService.java | 3 +- .../spring/application/UserQueryService.java | 24 ++++++++++ .../application/{user => }/UserWithToken.java | 3 +- .../{article => data}/ArticleData.java | 3 +- .../{article => data}/ArticleDataList.java | 2 +- .../{comment => data}/CommentData.java | 3 +- .../{profile => data}/ProfileData.java | 2 +- .../application/{user => data}/UserData.java | 2 +- .../application/user/UserQueryService.java | 25 ---------- .../service}/AuthorizationService.java | 2 +- .../service}/JwtService.java | 6 +-- .../mapper}/ArticleFavoriteMapper.java | 2 +- .../mapper}/ArticleMapper.java | 2 +- .../mapper}/CommentMapper.java | 2 +- .../{user => mybatis/mapper}/UserMapper.java | 2 +- .../readservice}/ArticleReadService.java | 3 +- .../readservice}/CommentReadService.java | 3 +- .../mybatis/readservice}/TagReadService.java | 2 +- .../mybatis/readservice}/UserReadService.java | 5 +- .../UserRelationshipQueryService.java | 2 +- .../MyBatisArticleFavoriteRepository.java | 3 +- .../MyBatisArticleRepository.java | 3 +- .../MyBatisCommentRepository.java | 3 +- .../MyBatisUserRepository.java | 3 +- .../service/DefaultJwtService.java | 8 ++-- src/main/resources/application.properties | 2 +- .../mapper/ArticleFavoriteMapper.xml | 2 +- .../mapper/ArticleFavoritesQueryService.xml | 4 +- src/main/resources/mapper/ArticleMapper.xml | 2 +- .../resources/mapper/ArticleReadService.xml | 7 +-- src/main/resources/mapper/CommentMapper.xml | 2 +- .../resources/mapper/CommentReadService.xml | 8 ++-- src/main/resources/mapper/TagReadService.xml | 2 +- src/main/resources/mapper/UserMapper.xml | 2 +- src/main/resources/mapper/UserReadService.xml | 7 ++- .../mapper/UserRelationshipQueryService.xml | 2 +- src/test/java/io/spring/TestHelper.java | 4 +- .../io/spring/api/ArticleFavoriteApiTest.java | 6 +-- .../java/io/spring/api/ArticlesApiTest.java | 9 ++-- .../java/io/spring/api/CommentsApiTest.java | 16 ++----- .../io/spring/api/CurrentUserApiTest.java | 14 ++---- .../io/spring/api/ListArticleApiTest.java | 4 +- .../java/io/spring/api/ProfileApiTest.java | 6 +-- .../io/spring/api/TestWithCurrentUser.java | 11 +++-- src/test/java/io/spring/api/UsersApiTest.java | 13 ++++-- .../article/ArticleQueryServiceTest.java | 14 +++--- .../comment/CommentQueryServiceTest.java | 8 ++-- .../profile/ProfileQueryServiceTest.java | 4 +- .../application/tag/TagsQueryServiceTest.java | 3 +- .../article/MyBatisArticleRepositoryTest.java | 3 +- .../comment/MyBatisCommentRepositoryTest.java | 1 + .../MyBatisArticleFavoriteRepositoryTest.java | 3 +- .../service/DefaultJwtServiceTest.java | 14 +++--- .../user/MyBatisUserRepositoryTest.java | 1 + 68 files changed, 225 insertions(+), 183 deletions(-) rename src/main/java/io/spring/application/{article => }/ArticleFavoritesQueryService.java (94%) rename src/main/java/io/spring/application/{article => }/ArticleQueryService.java (95%) rename src/main/java/io/spring/application/{comment => }/CommentQueryService.java (88%) rename src/main/java/io/spring/application/{profile => }/ProfileQueryService.java (81%) rename src/main/java/io/spring/application/{tag => }/TagsQueryService.java (78%) create mode 100644 src/main/java/io/spring/application/UserQueryService.java rename src/main/java/io/spring/application/{user => }/UserWithToken.java (85%) rename src/main/java/io/spring/application/{article => data}/ArticleData.java (87%) rename src/main/java/io/spring/application/{article => data}/ArticleDataList.java (91%) rename src/main/java/io/spring/application/{comment => data}/CommentData.java (85%) rename src/main/java/io/spring/application/{profile => data}/ProfileData.java (90%) rename src/main/java/io/spring/application/{user => data}/UserData.java (88%) delete mode 100644 src/main/java/io/spring/application/user/UserQueryService.java rename src/main/java/io/spring/{application => core/service}/AuthorizationService.java (93%) rename src/main/java/io/spring/{application => core/service}/JwtService.java (60%) rename src/main/java/io/spring/infrastructure/{favorite => mybatis/mapper}/ArticleFavoriteMapper.java (90%) rename src/main/java/io/spring/infrastructure/{article => mybatis/mapper}/ArticleMapper.java (93%) rename src/main/java/io/spring/infrastructure/{comment => mybatis/mapper}/CommentMapper.java (89%) rename src/main/java/io/spring/infrastructure/{user => mybatis/mapper}/UserMapper.java (94%) rename src/main/java/io/spring/{application/article => infrastructure/mybatis/readservice}/ArticleReadService.java (89%) rename src/main/java/io/spring/{application/comment => infrastructure/mybatis/readservice}/CommentReadService.java (77%) rename src/main/java/io/spring/{application/tag => infrastructure/mybatis/readservice}/TagReadService.java (78%) rename src/main/java/io/spring/{application/user => infrastructure/mybatis/readservice}/UserReadService.java (64%) rename src/main/java/io/spring/{application/profile => infrastructure/mybatis/readservice}/UserRelationshipQueryService.java (90%) rename src/main/java/io/spring/infrastructure/{favorite => repository}/MyBatisArticleFavoriteRepository.java (89%) rename src/main/java/io/spring/infrastructure/{article => repository}/MyBatisArticleRepository.java (92%) rename src/main/java/io/spring/infrastructure/{comment => repository}/MyBatisCommentRepository.java (88%) rename src/main/java/io/spring/infrastructure/{user => repository}/MyBatisUserRepository.java (94%) diff --git a/src/main/java/io/spring/JacksonCustomizations.java b/src/main/java/io/spring/JacksonCustomizations.java index 40ff87f..5706237 100644 --- a/src/main/java/io/spring/JacksonCustomizations.java +++ b/src/main/java/io/spring/JacksonCustomizations.java @@ -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)); } } } diff --git a/src/main/java/io/spring/api/ArticleApi.java b/src/main/java/io/spring/api/ArticleApi.java index 98db69a..81ee7d9 100644 --- a/src/main/java/io/spring/api/ArticleApi.java +++ b/src/main/java/io/spring/api/ArticleApi.java @@ -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; diff --git a/src/main/java/io/spring/api/ArticleFavoriteApi.java b/src/main/java/io/spring/api/ArticleFavoriteApi.java index e966ba7..34f1ef6 100644 --- a/src/main/java/io/spring/api/ArticleFavoriteApi.java +++ b/src/main/java/io/spring/api/ArticleFavoriteApi.java @@ -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; diff --git a/src/main/java/io/spring/api/ArticlesApi.java b/src/main/java/io/spring/api/ArticlesApi.java index 07defd0..a4f72d3 100644 --- a/src/main/java/io/spring/api/ArticlesApi.java +++ b/src/main/java/io/spring/api/ArticlesApi.java @@ -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; diff --git a/src/main/java/io/spring/api/CommentsApi.java b/src/main/java/io/spring/api/CommentsApi.java index 1a3e2d3..eccbd18 100644 --- a/src/main/java/io/spring/api/CommentsApi.java +++ b/src/main/java/io/spring/api/CommentsApi.java @@ -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; diff --git a/src/main/java/io/spring/api/CurrentUserApi.java b/src/main/java/io/spring/api/CurrentUserApi.java index 54fa965..81e64f9 100644 --- a/src/main/java/io/spring/api/CurrentUserApi.java +++ b/src/main/java/io/spring/api/CurrentUserApi.java @@ -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) { diff --git a/src/main/java/io/spring/api/ProfileApi.java b/src/main/java/io/spring/api/ProfileApi.java index 1ea2c1d..63e8086 100644 --- a/src/main/java/io/spring/api/ProfileApi.java +++ b/src/main/java/io/spring/api/ProfileApi.java @@ -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; diff --git a/src/main/java/io/spring/api/TagsApi.java b/src/main/java/io/spring/api/TagsApi.java index 0c573cb..cab4c12 100644 --- a/src/main/java/io/spring/api/TagsApi.java +++ b/src/main/java/io/spring/api/TagsApi.java @@ -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; diff --git a/src/main/java/io/spring/api/UsersApi.java b/src/main/java/io/spring/api/UsersApi.java index e05fadb..baa0e86 100644 --- a/src/main/java/io/spring/api/UsersApi.java +++ b/src/main/java/io/spring/api/UsersApi.java @@ -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 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); diff --git a/src/main/java/io/spring/api/security/JwtTokenFilter.java b/src/main/java/io/spring/api/security/JwtTokenFilter.java index 40073e0..87ad73d 100644 --- a/src/main/java/io/spring/api/security/JwtTokenFilter.java +++ b/src/main/java/io/spring/api/security/JwtTokenFilter.java @@ -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; diff --git a/src/main/java/io/spring/application/article/ArticleFavoritesQueryService.java b/src/main/java/io/spring/application/ArticleFavoritesQueryService.java similarity index 94% rename from src/main/java/io/spring/application/article/ArticleFavoritesQueryService.java rename to src/main/java/io/spring/application/ArticleFavoritesQueryService.java index 386bef6..e03e0dd 100644 --- a/src/main/java/io/spring/application/article/ArticleFavoritesQueryService.java +++ b/src/main/java/io/spring/application/ArticleFavoritesQueryService.java @@ -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; diff --git a/src/main/java/io/spring/application/article/ArticleQueryService.java b/src/main/java/io/spring/application/ArticleQueryService.java similarity index 95% rename from src/main/java/io/spring/application/article/ArticleQueryService.java rename to src/main/java/io/spring/application/ArticleQueryService.java index 6f8a7e6..0e82cae 100644 --- a/src/main/java/io/spring/application/article/ArticleQueryService.java +++ b/src/main/java/io/spring/application/ArticleQueryService.java @@ -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; diff --git a/src/main/java/io/spring/application/comment/CommentQueryService.java b/src/main/java/io/spring/application/CommentQueryService.java similarity index 88% rename from src/main/java/io/spring/application/comment/CommentQueryService.java rename to src/main/java/io/spring/application/CommentQueryService.java index cff0602..332a467 100644 --- a/src/main/java/io/spring/application/comment/CommentQueryService.java +++ b/src/main/java/io/spring/application/CommentQueryService.java @@ -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; diff --git a/src/main/java/io/spring/application/profile/ProfileQueryService.java b/src/main/java/io/spring/application/ProfileQueryService.java similarity index 81% rename from src/main/java/io/spring/application/profile/ProfileQueryService.java rename to src/main/java/io/spring/application/ProfileQueryService.java index fffdc9f..548ac5f 100644 --- a/src/main/java/io/spring/application/profile/ProfileQueryService.java +++ b/src/main/java/io/spring/application/ProfileQueryService.java @@ -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; diff --git a/src/main/java/io/spring/application/tag/TagsQueryService.java b/src/main/java/io/spring/application/TagsQueryService.java similarity index 78% rename from src/main/java/io/spring/application/tag/TagsQueryService.java rename to src/main/java/io/spring/application/TagsQueryService.java index 16968ab..5350070 100644 --- a/src/main/java/io/spring/application/tag/TagsQueryService.java +++ b/src/main/java/io/spring/application/TagsQueryService.java @@ -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; diff --git a/src/main/java/io/spring/application/UserQueryService.java b/src/main/java/io/spring/application/UserQueryService.java new file mode 100644 index 0000000..8054355 --- /dev/null +++ b/src/main/java/io/spring/application/UserQueryService.java @@ -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 findById(String id) { + return Optional.ofNullable(userReadService.findById(id)); + } +} + diff --git a/src/main/java/io/spring/application/user/UserWithToken.java b/src/main/java/io/spring/application/UserWithToken.java similarity index 85% rename from src/main/java/io/spring/application/user/UserWithToken.java rename to src/main/java/io/spring/application/UserWithToken.java index f5d87a1..eb8b4c2 100644 --- a/src/main/java/io/spring/application/user/UserWithToken.java +++ b/src/main/java/io/spring/application/UserWithToken.java @@ -1,5 +1,6 @@ -package io.spring.application.user; +package io.spring.application; +import io.spring.application.data.UserData; import lombok.Getter; @Getter diff --git a/src/main/java/io/spring/application/article/ArticleData.java b/src/main/java/io/spring/application/data/ArticleData.java similarity index 87% rename from src/main/java/io/spring/application/article/ArticleData.java rename to src/main/java/io/spring/application/data/ArticleData.java index 94ece20..9e15c89 100644 --- a/src/main/java/io/spring/application/article/ArticleData.java +++ b/src/main/java/io/spring/application/data/ArticleData.java @@ -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; diff --git a/src/main/java/io/spring/application/article/ArticleDataList.java b/src/main/java/io/spring/application/data/ArticleDataList.java similarity index 91% rename from src/main/java/io/spring/application/article/ArticleDataList.java rename to src/main/java/io/spring/application/data/ArticleDataList.java index 828c326..da454ed 100644 --- a/src/main/java/io/spring/application/article/ArticleDataList.java +++ b/src/main/java/io/spring/application/data/ArticleDataList.java @@ -1,4 +1,4 @@ -package io.spring.application.article; +package io.spring.application.data; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; diff --git a/src/main/java/io/spring/application/comment/CommentData.java b/src/main/java/io/spring/application/data/CommentData.java similarity index 85% rename from src/main/java/io/spring/application/comment/CommentData.java rename to src/main/java/io/spring/application/data/CommentData.java index b16dea2..d2cdab5 100644 --- a/src/main/java/io/spring/application/comment/CommentData.java +++ b/src/main/java/io/spring/application/data/CommentData.java @@ -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; diff --git a/src/main/java/io/spring/application/profile/ProfileData.java b/src/main/java/io/spring/application/data/ProfileData.java similarity index 90% rename from src/main/java/io/spring/application/profile/ProfileData.java rename to src/main/java/io/spring/application/data/ProfileData.java index 97e2a7d..20e012f 100644 --- a/src/main/java/io/spring/application/profile/ProfileData.java +++ b/src/main/java/io/spring/application/data/ProfileData.java @@ -1,4 +1,4 @@ -package io.spring.application.profile; +package io.spring.application.data; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.AllArgsConstructor; diff --git a/src/main/java/io/spring/application/user/UserData.java b/src/main/java/io/spring/application/data/UserData.java similarity index 88% rename from src/main/java/io/spring/application/user/UserData.java rename to src/main/java/io/spring/application/data/UserData.java index b8a2e67..7df0c03 100644 --- a/src/main/java/io/spring/application/user/UserData.java +++ b/src/main/java/io/spring/application/data/UserData.java @@ -1,4 +1,4 @@ -package io.spring.application.user; +package io.spring.application.data; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/src/main/java/io/spring/application/user/UserQueryService.java b/src/main/java/io/spring/application/user/UserQueryService.java deleted file mode 100644 index 227f79c..0000000 --- a/src/main/java/io/spring/application/user/UserQueryService.java +++ /dev/null @@ -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); - } -} - diff --git a/src/main/java/io/spring/application/AuthorizationService.java b/src/main/java/io/spring/core/service/AuthorizationService.java similarity index 93% rename from src/main/java/io/spring/application/AuthorizationService.java rename to src/main/java/io/spring/core/service/AuthorizationService.java index f26b1c2..dd5cfb7 100644 --- a/src/main/java/io/spring/application/AuthorizationService.java +++ b/src/main/java/io/spring/core/service/AuthorizationService.java @@ -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; diff --git a/src/main/java/io/spring/application/JwtService.java b/src/main/java/io/spring/core/service/JwtService.java similarity index 60% rename from src/main/java/io/spring/application/JwtService.java rename to src/main/java/io/spring/core/service/JwtService.java index f4261be..d4b08c7 100644 --- a/src/main/java/io/spring/application/JwtService.java +++ b/src/main/java/io/spring/core/service/JwtService.java @@ -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 getSubFromToken(String token); } diff --git a/src/main/java/io/spring/infrastructure/favorite/ArticleFavoriteMapper.java b/src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleFavoriteMapper.java similarity index 90% rename from src/main/java/io/spring/infrastructure/favorite/ArticleFavoriteMapper.java rename to src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleFavoriteMapper.java index 84aea60..5fdaaeb 100644 --- a/src/main/java/io/spring/infrastructure/favorite/ArticleFavoriteMapper.java +++ b/src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleFavoriteMapper.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/article/ArticleMapper.java b/src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleMapper.java similarity index 93% rename from src/main/java/io/spring/infrastructure/article/ArticleMapper.java rename to src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleMapper.java index 239f781..9392d37 100644 --- a/src/main/java/io/spring/infrastructure/article/ArticleMapper.java +++ b/src/main/java/io/spring/infrastructure/mybatis/mapper/ArticleMapper.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/comment/CommentMapper.java b/src/main/java/io/spring/infrastructure/mybatis/mapper/CommentMapper.java similarity index 89% rename from src/main/java/io/spring/infrastructure/comment/CommentMapper.java rename to src/main/java/io/spring/infrastructure/mybatis/mapper/CommentMapper.java index bf48a15..a3b906e 100644 --- a/src/main/java/io/spring/infrastructure/comment/CommentMapper.java +++ b/src/main/java/io/spring/infrastructure/mybatis/mapper/CommentMapper.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/user/UserMapper.java b/src/main/java/io/spring/infrastructure/mybatis/mapper/UserMapper.java similarity index 94% rename from src/main/java/io/spring/infrastructure/user/UserMapper.java rename to src/main/java/io/spring/infrastructure/mybatis/mapper/UserMapper.java index 007d551..2f6505e 100644 --- a/src/main/java/io/spring/infrastructure/user/UserMapper.java +++ b/src/main/java/io/spring/infrastructure/mybatis/mapper/UserMapper.java @@ -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; diff --git a/src/main/java/io/spring/application/article/ArticleReadService.java b/src/main/java/io/spring/infrastructure/mybatis/readservice/ArticleReadService.java similarity index 89% rename from src/main/java/io/spring/application/article/ArticleReadService.java rename to src/main/java/io/spring/infrastructure/mybatis/readservice/ArticleReadService.java index 0e2274c..d082de4 100644 --- a/src/main/java/io/spring/application/article/ArticleReadService.java +++ b/src/main/java/io/spring/infrastructure/mybatis/readservice/ArticleReadService.java @@ -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; diff --git a/src/main/java/io/spring/application/comment/CommentReadService.java b/src/main/java/io/spring/infrastructure/mybatis/readservice/CommentReadService.java similarity index 77% rename from src/main/java/io/spring/application/comment/CommentReadService.java rename to src/main/java/io/spring/infrastructure/mybatis/readservice/CommentReadService.java index 9527bd8..d9fe01d 100644 --- a/src/main/java/io/spring/application/comment/CommentReadService.java +++ b/src/main/java/io/spring/infrastructure/mybatis/readservice/CommentReadService.java @@ -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; diff --git a/src/main/java/io/spring/application/tag/TagReadService.java b/src/main/java/io/spring/infrastructure/mybatis/readservice/TagReadService.java similarity index 78% rename from src/main/java/io/spring/application/tag/TagReadService.java rename to src/main/java/io/spring/infrastructure/mybatis/readservice/TagReadService.java index 5a77c52..a098918 100644 --- a/src/main/java/io/spring/application/tag/TagReadService.java +++ b/src/main/java/io/spring/infrastructure/mybatis/readservice/TagReadService.java @@ -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; diff --git a/src/main/java/io/spring/application/user/UserReadService.java b/src/main/java/io/spring/infrastructure/mybatis/readservice/UserReadService.java similarity index 64% rename from src/main/java/io/spring/application/user/UserReadService.java rename to src/main/java/io/spring/infrastructure/mybatis/readservice/UserReadService.java index 08998b9..c870ea4 100644 --- a/src/main/java/io/spring/application/user/UserReadService.java +++ b/src/main/java/io/spring/infrastructure/mybatis/readservice/UserReadService.java @@ -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); } diff --git a/src/main/java/io/spring/application/profile/UserRelationshipQueryService.java b/src/main/java/io/spring/infrastructure/mybatis/readservice/UserRelationshipQueryService.java similarity index 90% rename from src/main/java/io/spring/application/profile/UserRelationshipQueryService.java rename to src/main/java/io/spring/infrastructure/mybatis/readservice/UserRelationshipQueryService.java index 8d42c82..4a22380 100644 --- a/src/main/java/io/spring/application/profile/UserRelationshipQueryService.java +++ b/src/main/java/io/spring/infrastructure/mybatis/readservice/UserRelationshipQueryService.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleFavoriteRepository.java similarity index 89% rename from src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java rename to src/main/java/io/spring/infrastructure/repository/MyBatisArticleFavoriteRepository.java index 7670fcf..f80a1a1 100644 --- a/src/main/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepository.java +++ b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleFavoriteRepository.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java similarity index 92% rename from src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java rename to src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java index 52e2665..114fd34 100644 --- a/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java +++ b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/comment/MyBatisCommentRepository.java b/src/main/java/io/spring/infrastructure/repository/MyBatisCommentRepository.java similarity index 88% rename from src/main/java/io/spring/infrastructure/comment/MyBatisCommentRepository.java rename to src/main/java/io/spring/infrastructure/repository/MyBatisCommentRepository.java index a7b95b1..8db2251 100644 --- a/src/main/java/io/spring/infrastructure/comment/MyBatisCommentRepository.java +++ b/src/main/java/io/spring/infrastructure/repository/MyBatisCommentRepository.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/user/MyBatisUserRepository.java b/src/main/java/io/spring/infrastructure/repository/MyBatisUserRepository.java similarity index 94% rename from src/main/java/io/spring/infrastructure/user/MyBatisUserRepository.java rename to src/main/java/io/spring/infrastructure/repository/MyBatisUserRepository.java index ac08683..39d06db 100644 --- a/src/main/java/io/spring/infrastructure/user/MyBatisUserRepository.java +++ b/src/main/java/io/spring/infrastructure/repository/MyBatisUserRepository.java @@ -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; diff --git a/src/main/java/io/spring/infrastructure/service/DefaultJwtService.java b/src/main/java/io/spring/infrastructure/service/DefaultJwtService.java index f8a58f9..42713ad 100644 --- a/src/main/java/io/spring/infrastructure/service/DefaultJwtService.java +++ b/src/main/java/io/spring/infrastructure/service/DefaultJwtService.java @@ -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(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 062526c..418eeaa 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file +logging.level.io.spring.infrastructure.mybatis.readservice.ArticleReadService=DEBUG \ No newline at end of file diff --git a/src/main/resources/mapper/ArticleFavoriteMapper.xml b/src/main/resources/mapper/ArticleFavoriteMapper.xml index 7c1e48b..9fd4dac 100644 --- a/src/main/resources/mapper/ArticleFavoriteMapper.xml +++ b/src/main/resources/mapper/ArticleFavoriteMapper.xml @@ -1,6 +1,6 @@ - + insert into article_favorites (article_id, user_id) values (#{articleFavorite.articleId}, #{articleFavorite.userId}) diff --git a/src/main/resources/mapper/ArticleFavoritesQueryService.xml b/src/main/resources/mapper/ArticleFavoritesQueryService.xml index 8cafc77..faf7340 100644 --- a/src/main/resources/mapper/ArticleFavoritesQueryService.xml +++ b/src/main/resources/mapper/ArticleFavoritesQueryService.xml @@ -1,6 +1,6 @@ - + @@ -28,7 +28,7 @@ and AF.user_id = #{currentUser.id} - + diff --git a/src/main/resources/mapper/ArticleMapper.xml b/src/main/resources/mapper/ArticleMapper.xml index 19c609b..eedaf10 100644 --- a/src/main/resources/mapper/ArticleMapper.xml +++ b/src/main/resources/mapper/ArticleMapper.xml @@ -1,6 +1,6 @@ - + insert into articles(id, slug, title, description, body, user_id, created_at, updated_at) values( diff --git a/src/main/resources/mapper/ArticleReadService.xml b/src/main/resources/mapper/ArticleReadService.xml index c8ad921..822c2b1 100644 --- a/src/main/resources/mapper/ArticleReadService.xml +++ b/src/main/resources/mapper/ArticleReadService.xml @@ -1,6 +1,6 @@ - + U.id userId, U.username userUsername, @@ -81,6 +81,7 @@ #{id} + order by A.created_at desc - + - + \ No newline at end of file diff --git a/src/main/resources/mapper/TagReadService.xml b/src/main/resources/mapper/TagReadService.xml index 5902be2..0e8ceef 100644 --- a/src/main/resources/mapper/TagReadService.xml +++ b/src/main/resources/mapper/TagReadService.xml @@ -1,6 +1,6 @@ - + diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index 1635ab9..08e89b2 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -1,6 +1,6 @@ - + insert into users (id, username, email, password, bio, image) values( #{user.id}, diff --git a/src/main/resources/mapper/UserReadService.xml b/src/main/resources/mapper/UserReadService.xml index 511066c..edf53c1 100644 --- a/src/main/resources/mapper/UserReadService.xml +++ b/src/main/resources/mapper/UserReadService.xml @@ -1,7 +1,10 @@ - - select * from users where username = #{username} + \ No newline at end of file diff --git a/src/main/resources/mapper/UserRelationshipQueryService.xml b/src/main/resources/mapper/UserRelationshipQueryService.xml index 4639631..f6b8ab8 100644 --- a/src/main/resources/mapper/UserRelationshipQueryService.xml +++ b/src/main/resources/mapper/UserRelationshipQueryService.xml @@ -1,6 +1,6 @@ - + diff --git a/src/test/java/io/spring/TestHelper.java b/src/test/java/io/spring/TestHelper.java index 2e16528..d5975fc 100644 --- a/src/test/java/io/spring/TestHelper.java +++ b/src/test/java/io/spring/TestHelper.java @@ -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; diff --git a/src/test/java/io/spring/api/ArticleFavoriteApiTest.java b/src/test/java/io/spring/api/ArticleFavoriteApiTest.java index bd48099..c92d5fb 100644 --- a/src/test/java/io/spring/api/ArticleFavoriteApiTest.java +++ b/src/test/java/io/spring/api/ArticleFavoriteApiTest.java @@ -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; diff --git a/src/test/java/io/spring/api/ArticlesApiTest.java b/src/test/java/io/spring/api/ArticlesApiTest.java index ecd0792..0d53226 100644 --- a/src/test/java/io/spring/api/ArticlesApiTest.java +++ b/src/test/java/io/spring/api/ArticlesApiTest.java @@ -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))); } diff --git a/src/test/java/io/spring/api/CommentsApiTest.java b/src/test/java/io/spring/api/CommentsApiTest.java index fd158c6..b55f2e4 100644 --- a/src/test/java/io/spring/api/CommentsApiTest.java +++ b/src/test/java/io/spring/api/CommentsApiTest.java @@ -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() diff --git a/src/test/java/io/spring/api/CurrentUserApiTest.java b/src/test/java/io/spring/api/CurrentUserApiTest.java index e539f39..f988827 100644 --- a/src/test/java/io/spring/api/CurrentUserApiTest.java +++ b/src/test/java/io/spring/api/CurrentUserApiTest.java @@ -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 diff --git a/src/test/java/io/spring/api/ListArticleApiTest.java b/src/test/java/io/spring/api/ListArticleApiTest.java index f767a2c..f66d086 100644 --- a/src/test/java/io/spring/api/ListArticleApiTest.java +++ b/src/test/java/io/spring/api/ListArticleApiTest.java @@ -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; diff --git a/src/test/java/io/spring/api/ProfileApiTest.java b/src/test/java/io/spring/api/ProfileApiTest.java index 29fa96c..7f68388 100644 --- a/src/test/java/io/spring/api/ProfileApiTest.java +++ b/src/test/java/io/spring/api/ProfileApiTest.java @@ -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; diff --git a/src/test/java/io/spring/api/TestWithCurrentUser.java b/src/test/java/io/spring/api/TestWithCurrentUser.java index 2173e2f..1277691 100644 --- a/src/test/java/io/spring/api/TestWithCurrentUser.java +++ b/src/test/java/io/spring/api/TestWithCurrentUser.java @@ -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); } } diff --git a/src/test/java/io/spring/api/UsersApiTest.java b/src/test/java/io/spring/api/UsersApiTest.java index f7c5a0b..37e3736 100644 --- a/src/test/java/io/spring/api/UsersApiTest.java +++ b/src/test/java/io/spring/api/UsersApiTest.java @@ -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 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 param = new HashMap() {{ diff --git a/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java b/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java index 7f72500..fccec10 100644 --- a/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java +++ b/src/test/java/io/spring/application/article/ArticleQueryServiceTest.java @@ -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); } diff --git a/src/test/java/io/spring/application/comment/CommentQueryServiceTest.java b/src/test/java/io/spring/application/comment/CommentQueryServiceTest.java index df5a051..92842ce 100644 --- a/src/test/java/io/spring/application/comment/CommentQueryServiceTest.java +++ b/src/test/java/io/spring/application/comment/CommentQueryServiceTest.java @@ -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; diff --git a/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java b/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java index 2c0be3b..3976a95 100644 --- a/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java +++ b/src/test/java/io/spring/application/profile/ProfileQueryServiceTest.java @@ -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; diff --git a/src/test/java/io/spring/application/tag/TagsQueryServiceTest.java b/src/test/java/io/spring/application/tag/TagsQueryServiceTest.java index 1b47384..505ea64 100644 --- a/src/test/java/io/spring/application/tag/TagsQueryServiceTest.java +++ b/src/test/java/io/spring/application/tag/TagsQueryServiceTest.java @@ -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; diff --git a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java index 41c3d0c..ba6defb 100644 --- a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java @@ -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; diff --git a/src/test/java/io/spring/infrastructure/comment/MyBatisCommentRepositoryTest.java b/src/test/java/io/spring/infrastructure/comment/MyBatisCommentRepositoryTest.java index 690c4ee..0c06541 100644 --- a/src/test/java/io/spring/infrastructure/comment/MyBatisCommentRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/comment/MyBatisCommentRepositoryTest.java @@ -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; diff --git a/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java b/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java index 2eeb3aa..3bd78ae 100644 --- a/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/favorite/MyBatisArticleFavoriteRepositoryTest.java @@ -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 { diff --git a/src/test/java/io/spring/infrastructure/service/DefaultJwtServiceTest.java b/src/test/java/io/spring/infrastructure/service/DefaultJwtServiceTest.java index 9a3981f..7ff287b 100644 --- a/src/test/java/io/spring/infrastructure/service/DefaultJwtServiceTest.java +++ b/src/test/java/io/spring/infrastructure/service/DefaultJwtServiceTest.java @@ -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 optional = jwtService.getSubFromToken(token); assertThat(optional.isPresent(), is(true)); - assertThat(optional.get(), is(username)); + assertThat(optional.get(), is(user.getId())); } @Test diff --git a/src/test/java/io/spring/infrastructure/user/MyBatisUserRepositoryTest.java b/src/test/java/io/spring/infrastructure/user/MyBatisUserRepositoryTest.java index 4a5dc18..ed33478 100644 --- a/src/test/java/io/spring/infrastructure/user/MyBatisUserRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/user/MyBatisUserRepositoryTest.java @@ -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;