remove favorite
This commit is contained in:
parent
827572893a
commit
7c685942ea
@ -11,6 +11,7 @@ import io.spring.core.user.User;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -43,8 +44,18 @@ public class ArticleFavoriteApi {
|
|||||||
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping
|
||||||
|
public ResponseEntity unfavoriteArticle(@PathVariable("slug") String slug,
|
||||||
|
@AuthenticationPrincipal User user) {
|
||||||
|
Article article = getArticle(slug);
|
||||||
|
articleFavoriteRepository.find(article.getId(), user.getId()).ifPresent(favorite -> {
|
||||||
|
articleFavoriteRepository.remove(favorite);
|
||||||
|
});
|
||||||
|
return responseArticleData(articleQueryService.findBySlug(slug, user).get());
|
||||||
|
}
|
||||||
|
|
||||||
private ResponseEntity<HashMap<String, Object>> responseArticleData(final ArticleData articleData) {
|
private ResponseEntity<HashMap<String, Object>> responseArticleData(final ArticleData articleData) {
|
||||||
return ResponseEntity.status(201).body(new HashMap<String, Object>() {{
|
return ResponseEntity.ok(new HashMap<String, Object>() {{
|
||||||
put("article", articleData);
|
put("article", articleData);
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package io.spring.core.favorite;
|
package io.spring.core.favorite;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface ArticleFavoriteRepository {
|
public interface ArticleFavoriteRepository {
|
||||||
void save(ArticleFavorite articleFavorite);
|
void save(ArticleFavorite articleFavorite);
|
||||||
|
|
||||||
|
Optional<ArticleFavorite> find(String articleId, String userId);
|
||||||
|
|
||||||
|
void remove(ArticleFavorite favorite);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,9 @@ import org.springframework.stereotype.Component;
|
|||||||
@Mapper
|
@Mapper
|
||||||
@Component
|
@Component
|
||||||
public interface ArticleFavoriteMapper {
|
public interface ArticleFavoriteMapper {
|
||||||
boolean find(@Param("articleFavorite") ArticleFavorite articleFavorite);
|
ArticleFavorite find(@Param("articleId") String articleId, @Param("userId") String userId);
|
||||||
|
|
||||||
void insert(@Param("articleFavorite") ArticleFavorite articleFavorite);
|
void insert(@Param("articleFavorite") ArticleFavorite articleFavorite);
|
||||||
|
|
||||||
|
void delete(@Param("favorite") ArticleFavorite favorite);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class MyBatisArticleFavoriteRepository implements ArticleFavoriteRepository {
|
public class MyBatisArticleFavoriteRepository implements ArticleFavoriteRepository {
|
||||||
private ArticleFavoriteMapper mapper;
|
private ArticleFavoriteMapper mapper;
|
||||||
@ -16,8 +18,18 @@ public class MyBatisArticleFavoriteRepository implements ArticleFavoriteReposito
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(ArticleFavorite articleFavorite) {
|
public void save(ArticleFavorite articleFavorite) {
|
||||||
if (!mapper.find(articleFavorite)) {
|
if (mapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()) != null) {
|
||||||
mapper.insert(articleFavorite);
|
mapper.insert(articleFavorite);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ArticleFavorite> find(String articleId, String userId) {
|
||||||
|
return Optional.ofNullable(mapper.find(articleId, userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(ArticleFavorite favorite) {
|
||||||
|
mapper.delete(favorite);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,18 @@
|
|||||||
<insert id="insert">
|
<insert id="insert">
|
||||||
insert into article_favorites (article_id, user_id) values (#{articleFavorite.articleId}, #{articleFavorite.userId})
|
insert into article_favorites (article_id, user_id) values (#{articleFavorite.articleId}, #{articleFavorite.userId})
|
||||||
</insert>
|
</insert>
|
||||||
<select id="find" resultType="java.lang.Boolean">
|
<delete id="delete">
|
||||||
select count(1) from article_favorites where article_id = #{articleFavorite.articleId} and user_id = #{articleFavorite.userId}
|
delete from article_favorites where article_id = #{favorite.articleId} and user_id = #{favorite.userId}
|
||||||
|
</delete>
|
||||||
|
<select id="find" resultMap="articleFavorite">
|
||||||
|
select
|
||||||
|
AF.article_id articleFavoriteArticleId,
|
||||||
|
AF.user_id articleFavoriteUserId
|
||||||
|
from article_favorites AF
|
||||||
|
where AF.article_id = #{articleId} and AF.user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
<resultMap id="articleFavorite" type="io.spring.core.favorite.ArticleFavorite">
|
||||||
|
<result column="articleFavoriteArticleId" property="articleId"/>
|
||||||
|
<result column="articleFavoriteUserId" property="userId"/>
|
||||||
|
</resultMap>
|
||||||
</mapper>
|
</mapper>
|
@ -8,6 +8,7 @@ import io.spring.application.profile.ProfileData;
|
|||||||
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.article.Tag;
|
import io.spring.core.article.Tag;
|
||||||
|
import io.spring.core.favorite.ArticleFavorite;
|
||||||
import io.spring.core.favorite.ArticleFavoriteRepository;
|
import io.spring.core.favorite.ArticleFavoriteRepository;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -23,7 +24,9 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
@ -86,7 +89,23 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
|||||||
.post("/articles/{slug}/favorite", article.getSlug())
|
.post("/articles/{slug}/favorite", article.getSlug())
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then()
|
.then()
|
||||||
.statusCode(201)
|
.statusCode(200)
|
||||||
.body("article.id", equalTo(article.getId()));
|
.body("article.id", equalTo(article.getId()));
|
||||||
|
|
||||||
|
verify(articleFavoriteRepository).save(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_unfavorite_an_article_success() throws Exception {
|
||||||
|
when(articleFavoriteRepository.find(eq(article.getId()), eq(user.getId()))).thenReturn(Optional.of(new ArticleFavorite(article.getId(), user.getId())));
|
||||||
|
given()
|
||||||
|
.header("Authorization", "Token " + token)
|
||||||
|
.when()
|
||||||
|
.delete("/articles/{slug}/favorite", article.getSlug())
|
||||||
|
.prettyPeek()
|
||||||
|
.then()
|
||||||
|
.statusCode(200)
|
||||||
|
.body("article.id", equalTo(article.getId()));
|
||||||
|
verify(articleFavoriteRepository).remove(new ArticleFavorite(article.getId(), user.getId()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,14 @@ public class MyBatisArticleFavoriteRepositoryTest {
|
|||||||
public void should_save_and_fetch_articleFavorite_success() throws Exception {
|
public void should_save_and_fetch_articleFavorite_success() throws Exception {
|
||||||
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
|
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
|
||||||
articleFavoriteRepository.save(articleFavorite);
|
articleFavoriteRepository.save(articleFavorite);
|
||||||
assertThat(articleFavoriteMapper.find(articleFavorite), is(true));
|
assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), is(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_remove_favorite_success() throws Exception {
|
||||||
|
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
|
||||||
|
articleFavoriteRepository.save(articleFavorite);
|
||||||
|
articleFavoriteRepository.remove(articleFavorite);
|
||||||
|
assertThat(articleFavoriteRepository.find("123", "456").isPresent(), is(false));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user