list recent article
This commit is contained in:
@@ -2,6 +2,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.core.article.Article;
|
||||
import io.spring.core.article.ArticleRepository;
|
||||
@@ -13,9 +14,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
@@ -52,6 +55,15 @@ public class ArticlesApi {
|
||||
put("article", articleQueryService.findById(article.getId(), user).get());
|
||||
}});
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity getArticles(@RequestParam(value = "offset", defaultValue = "0") int offset,
|
||||
@RequestParam(value = "limit", defaultValue = "20") int limit,
|
||||
@RequestParam(value = "tag", required = false) String tag,
|
||||
@RequestParam(value = "favorited", required = false) String favoritedBy,
|
||||
@RequestParam(value = "author", required = false) String author) {
|
||||
return ResponseEntity.ok(articleQueryService.findRecentArticles(tag, author, favoritedBy, new Page(offset, limit)));
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
|
||||
33
src/main/java/io/spring/application/Page.java
Normal file
33
src/main/java/io/spring/application/Page.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package io.spring.application;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class Page {
|
||||
private static final int MAX_LIMIT = 100;
|
||||
private int offset = 0;
|
||||
private int limit = 20;
|
||||
|
||||
public Page(int offset, int limit) {
|
||||
setOffset(offset);
|
||||
setLimit(limit);
|
||||
}
|
||||
|
||||
private void setOffset(int offset) {
|
||||
if (offset > 0) {
|
||||
this.offset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
private void setLimit(int limit) {
|
||||
if (limit > MAX_LIMIT) {
|
||||
this.limit = MAX_LIMIT;
|
||||
} else if (limit > 0) {
|
||||
this.limit = limit;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class ArticleDataList {
|
||||
@JsonProperty("articles")
|
||||
private final List<ArticleData> articleDatas;
|
||||
@JsonProperty("articlesCount")
|
||||
private final int count;
|
||||
|
||||
public ArticleDataList(List<ArticleData> articleDatas, int count) {
|
||||
|
||||
this.articleDatas = articleDatas;
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import io.spring.application.Page;
|
||||
import io.spring.application.profile.UserRelationshipQueryService;
|
||||
import io.spring.core.user.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@@ -54,4 +57,12 @@ public class ArticleQueryService {
|
||||
user.getId(),
|
||||
articleData.getProfileData().getId()));
|
||||
}
|
||||
|
||||
public ArticleDataList findRecentArticles(String tag, String author, String favoritedBy, Page page) {
|
||||
List<String> articleIds = articleReadService.queryArticles(tag, author, favoritedBy, page);
|
||||
int articleCount = articleReadService.countArticle(tag, author, favoritedBy);
|
||||
return new ArticleDataList(
|
||||
articleIds.size() == 0 ? new ArrayList<>() : articleReadService.findArticles(articleIds),
|
||||
articleCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
package io.spring.application.article;
|
||||
|
||||
import io.spring.application.Page;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Mapper
|
||||
public interface ArticleReadService {
|
||||
ArticleData findById(@Param("id") String id);
|
||||
|
||||
ArticleData findBySlug(@Param("slug") String slug);
|
||||
|
||||
List<String> queryArticles(@Param("tag") String tag, @Param("author") String author, @Param("favoritedBy") String favoritedBy, @Param("page") Page page);
|
||||
|
||||
int countArticle(@Param("tag") String tag, @Param("author") String author, @Param("favoritedBy") String favoritedBy);
|
||||
|
||||
List<ArticleData> findArticles(@Param("articleIds") List<String> articleIds);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ public class Article {
|
||||
private DateTime updatedAt;
|
||||
|
||||
public Article(String title, String description, String body, String[] tagList, String userId) {
|
||||
this(title, description, body, tagList, userId, new DateTime());
|
||||
}
|
||||
|
||||
public Article(String title, String description, String body, String[] tagList, String userId, DateTime createdAt) {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.slug = toSlug(title);
|
||||
this.title = title;
|
||||
@@ -34,8 +38,8 @@ public class Article {
|
||||
this.body = body;
|
||||
this.tags = Arrays.stream(tagList).collect(toSet()).stream().map(Tag::new).collect(toList());
|
||||
this.userId = userId;
|
||||
this.createdAt = new DateTime();
|
||||
this.updatedAt = new DateTime();
|
||||
this.createdAt = createdAt;
|
||||
this.updatedAt = createdAt;
|
||||
}
|
||||
|
||||
public void update(String title, String description, String body) {
|
||||
|
||||
@@ -33,6 +33,59 @@
|
||||
<include refid="selectArticleData"/>
|
||||
where A.slug = #{slug}
|
||||
</select>
|
||||
<select id="queryArticles" resultMap="articleId">
|
||||
select
|
||||
DISTINCT(A.id) articleId, A.created_at
|
||||
from
|
||||
articles A
|
||||
left join article_tags AT on A.id = AT.article_id
|
||||
left join tags T on T.id = AT.tag_id
|
||||
left join article_favorites AF on AF.article_id = A.id
|
||||
<where>
|
||||
<if test="tag != null">
|
||||
T.name = #{tag}
|
||||
</if>
|
||||
<if test="author != null">
|
||||
AND A.user_id = #{author}
|
||||
</if>
|
||||
<if test="favoritedBy != null">
|
||||
AND AF.user_id = #{favoritedBy}
|
||||
</if>
|
||||
</where>
|
||||
order by A.created_at desc
|
||||
limit #{page.offset}, #{page.limit}
|
||||
</select>
|
||||
<select id="countArticle" resultType="java.lang.Integer">
|
||||
select
|
||||
count(DISTINCT A.id)
|
||||
from
|
||||
articles A
|
||||
left join article_tags AT on A.id = AT.article_id
|
||||
left join tags T on T.id = AT.tag_id
|
||||
left join article_favorites AF on AF.article_id = A.id
|
||||
<where>
|
||||
<if test="tag != null">
|
||||
T.name = #{tag}
|
||||
</if>
|
||||
<if test="author != null">
|
||||
AND A.user_id = #{author}
|
||||
</if>
|
||||
<if test="favoritedBy != null">
|
||||
AND AF.user_id = #{favoritedBy}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<select id="findArticles" resultMap="articleData">
|
||||
<include refid="selectArticleData"/>
|
||||
where A.id in
|
||||
<foreach index="index" collection="articleIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
<resultMap id="articleId" type="string">
|
||||
<id javaType="string" column="articleId"/>
|
||||
</resultMap>
|
||||
|
||||
<resultMap id="articleData" type="io.spring.application.article.ArticleData">
|
||||
<id column="articleId" property="id"/>
|
||||
|
||||
Reference in New Issue
Block a user