should favorite article

This commit is contained in:
aisensiy 2017-08-16 11:51:20 +08:00
parent 001d4e1eea
commit 827572893a
10 changed files with 282 additions and 7 deletions

View File

@ -0,0 +1,56 @@
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.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.core.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@RequestMapping(path = "articles/{slug}/favorite")
public class ArticleFavoriteApi {
private ArticleFavoriteRepository articleFavoriteRepository;
private ArticleRepository articleRepository;
private ArticleQueryService articleQueryService;
@Autowired
public ArticleFavoriteApi(ArticleFavoriteRepository articleFavoriteRepository,
ArticleRepository articleRepository,
ArticleQueryService articleQueryService) {
this.articleFavoriteRepository = articleFavoriteRepository;
this.articleRepository = articleRepository;
this.articleQueryService = articleQueryService;
}
@PostMapping
public ResponseEntity favoriteArticle(@PathVariable("slug") String slug,
@AuthenticationPrincipal User user) {
Article article = getArticle(slug);
ArticleFavorite articleFavorite = new ArticleFavorite(article.getId(), user.getId());
articleFavoriteRepository.save(articleFavorite);
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
}
private ResponseEntity<HashMap<String, Object>> responseArticleData(final ArticleData articleData) {
return ResponseEntity.status(201).body(new HashMap<String, Object>() {{
put("article", articleData);
}});
}
private Article getArticle(String slug) {
return articleRepository.findBySlug(slug).map(article -> article)
.orElseThrow(ResourceNotFoundException::new);
}
}

View File

@ -0,0 +1,18 @@
package io.spring.core.favorite;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@Getter
@EqualsAndHashCode
public class ArticleFavorite {
private String articleId;
private String userId;
public ArticleFavorite(String articleId, String userId) {
this.articleId = articleId;
this.userId = userId;
}
}

View File

@ -0,0 +1,5 @@
package io.spring.core.favorite;
public interface ArticleFavoriteRepository {
void save(ArticleFavorite articleFavorite);
}

View File

@ -0,0 +1,14 @@
package io.spring.infrastructure.favorite;
import io.spring.core.favorite.ArticleFavorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
@Mapper
@Component
public interface ArticleFavoriteMapper {
boolean find(@Param("articleFavorite") ArticleFavorite articleFavorite);
void insert(@Param("articleFavorite") ArticleFavorite articleFavorite);
}

View File

@ -0,0 +1,23 @@
package io.spring.infrastructure.favorite;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class MyBatisArticleFavoriteRepository implements ArticleFavoriteRepository {
private ArticleFavoriteMapper mapper;
@Autowired
public MyBatisArticleFavoriteRepository(ArticleFavoriteMapper mapper) {
this.mapper = mapper;
}
@Override
public void save(ArticleFavorite articleFavorite) {
if (!mapper.find(articleFavorite)) {
mapper.insert(articleFavorite);
}
}
}

View File

@ -20,7 +20,8 @@ create table articles (
create table article_favorites ( create table article_favorites (
article_id varchar(255) not null, article_id varchar(255) not null,
user_id varchar(255) not null user_id varchar(255) not null,
primary key(article_id, user_id)
); );
create table follows ( create table follows (

View File

@ -0,0 +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.infrastructure.favorite.ArticleFavoriteMapper">
<insert id="insert">
insert into article_favorites (article_id, user_id) values (#{articleFavorite.articleId}, #{articleFavorite.userId})
</insert>
<select id="find" resultType="java.lang.Boolean">
select count(1) from article_favorites where article_id = #{articleFavorite.articleId} and user_id = #{articleFavorite.userId}
</select>
</mapper>

View File

@ -0,0 +1,92 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.article.ArticleData;
import io.spring.application.article.ArticleQueryService;
import io.spring.application.article.ArticleReadService;
import io.spring.application.profile.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
import io.spring.core.article.Tag;
import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.Optional;
import java.util.stream.Collectors;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
@MockBean
private ArticleFavoriteRepository articleFavoriteRepository;
@MockBean
private ArticleRepository articleRepository;
@MockBean
private ArticleQueryService articleQueryService;
protected String email;
protected String username;
protected String defaultAvatar;
@LocalServerPort
private int port;
private Article article;
private User anotherUser;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
email = "john@jacob.com";
username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture(email, username, defaultAvatar);
anotherUser = new User("other@test.com", "other", "123", "", "");
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
ArticleData articleData = new ArticleData(
article.getId(),
article.getSlug(),
article.getTitle(),
article.getDescription(),
article.getBody(),
true,
1,
article.getCreatedAt(),
article.getUpdatedAt(),
article.getTags().stream().map(Tag::getName).collect(Collectors.toList()),
new ProfileData(
anotherUser.getId(),
anotherUser.getUsername(),
anotherUser.getBio(),
anotherUser.getImage(),
false
));
when(articleQueryService.findBySlug(eq(articleData.getSlug()), eq(user))).thenReturn(Optional.of(articleData));
}
@Test
public void should_favorite_an_article_success() throws Exception {
given()
.header("Authorization", "Token " + token)
.when()
.post("/articles/{slug}/favorite", article.getSlug())
.prettyPeek()
.then()
.statusCode(201)
.body("article.id", equalTo(article.getId()));
}
}

View File

@ -2,10 +2,14 @@ package io.spring.application.article;
import io.spring.core.article.Article; import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository; import io.spring.core.article.ArticleRepository;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.core.user.UserRepository; import io.spring.core.user.UserRepository;
import io.spring.infrastructure.article.MyBatisArticleRepository; import io.spring.infrastructure.article.MyBatisArticleRepository;
import io.spring.infrastructure.favorite.MyBatisArticleFavoriteRepository;
import io.spring.infrastructure.user.MyBatisUserRepository; import io.spring.infrastructure.user.MyBatisUserRepository;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest; import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
@ -15,13 +19,14 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class}) @Import({ArticleQueryService.class, MyBatisUserRepository.class, MyBatisArticleRepository.class, MyBatisArticleFavoriteRepository.class})
public class ArticleQueryServiceTest { public class ArticleQueryServiceTest {
@Autowired @Autowired
private ArticleQueryService queryService; private ArticleQueryService queryService;
@ -32,13 +37,22 @@ public class ArticleQueryServiceTest {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired
private ArticleFavoriteRepository articleFavoriteRepository;
private User user;
private Article article;
@Before
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());
articleRepository.save(article);
}
@Test @Test
public void should_fetch_article_success() throws Exception { public void should_fetch_article_success() throws Exception {
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
userRepository.save(user);
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
articleRepository.save(article);
Optional<ArticleData> optional = queryService.findById(article.getId(), user); Optional<ArticleData> optional = queryService.findById(article.getId(), user);
assertThat(optional.isPresent(), is(true)); assertThat(optional.isPresent(), is(true));
@ -48,4 +62,15 @@ public class ArticleQueryServiceTest {
assertThat(fetched.getCreatedAt(), notNullValue()); assertThat(fetched.getCreatedAt(), notNullValue());
assertThat(fetched.getUpdatedAt(), notNullValue()); assertThat(fetched.getUpdatedAt(), notNullValue());
} }
@Test
public void should_get_article_with_right_favorite_and_favorite_count() throws Exception {
User anotherUser = new User("other@test.com", "other", "123", "", "");
userRepository.save(anotherUser);
articleFavoriteRepository.save(new ArticleFavorite(article.getId(), anotherUser.getId()));
ArticleData articleData = queryService.findById(article.getId(), anotherUser).get();
assertThat(articleData.getFavoritesCount(), is(1));
assertThat(articleData.isFavorited(), is(true));
}
} }

View File

@ -0,0 +1,31 @@
package io.spring.infrastructure.favorite;
import io.spring.core.favorite.ArticleFavorite;
import io.spring.core.favorite.ArticleFavoriteRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@MybatisTest
@Import({MyBatisArticleFavoriteRepository.class})
public class MyBatisArticleFavoriteRepositoryTest {
@Autowired
private ArticleFavoriteRepository articleFavoriteRepository;
@Autowired
private ArticleFavoriteMapper articleFavoriteMapper;
@Test
public void should_save_and_fetch_articleFavorite_success() throws Exception {
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
articleFavoriteRepository.save(articleFavorite);
assertThat(articleFavoriteMapper.find(articleFavorite), is(true));
}
}