should favorite article
This commit is contained in:
56
src/main/java/io/spring/api/ArticleFavoriteApi.java
Normal file
56
src/main/java/io/spring/api/ArticleFavoriteApi.java
Normal 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);
|
||||
}
|
||||
}
|
||||
18
src/main/java/io/spring/core/favorite/ArticleFavorite.java
Normal file
18
src/main/java/io/spring/core/favorite/ArticleFavorite.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.spring.core.favorite;
|
||||
|
||||
public interface ArticleFavoriteRepository {
|
||||
void save(ArticleFavorite articleFavorite);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,8 @@ create table articles (
|
||||
|
||||
create table article_favorites (
|
||||
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 (
|
||||
|
||||
10
src/main/resources/mapper/ArticleFavoriteMapper.xml
Normal file
10
src/main/resources/mapper/ArticleFavoriteMapper.xml
Normal 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>
|
||||
Reference in New Issue
Block a user