refactor paging query no offset
This commit is contained in:
@@ -32,9 +32,8 @@ public class ArticleFacade {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ArticlePreviewResponse> findAll(int offset, int limit) {
|
||||
var idx = articleReader.findAll();
|
||||
return articleReader.findAllById(offset, limit, idx)
|
||||
public List<ArticlePreviewResponse> findAll(Long articleId, int pageSize) {
|
||||
return articleReader.findAll(articleId, pageSize)
|
||||
.stream()
|
||||
.map(dto -> new ArticlePreviewResponse(dto.getId(), dto.getAuthorId(), dto.getTitle(),
|
||||
dto.getNickname(), dto.getImage(), dto.getCreatedAt(), dto.getModifiedAt(),
|
||||
|
||||
@@ -12,8 +12,6 @@ public interface ArticleReader {
|
||||
|
||||
boolean existsById(Long articleId);
|
||||
|
||||
List<Long> findAll();
|
||||
|
||||
List<ArticleDto> findAllById(@Param("offset") int offset,
|
||||
@Param("limit") int limit, @Param("idx") List<Long> idx);
|
||||
List<ArticleDto> findAll(@Param("articleId") Long articleId,
|
||||
@Param("pageSize") int pageSize);
|
||||
}
|
||||
|
||||
@@ -43,13 +43,8 @@ public final class MybatisArticleRepository implements ArticleReader, ArticleRep
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> findAll() {
|
||||
return template.getMapper(ArticleReader.class).findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArticleDto> findAllById(int offset, int limit, List<Long> idx) {
|
||||
return template.getMapper(ArticleReader.class).findAllById(offset, limit, idx);
|
||||
public List<ArticleDto> findAll(Long articleId, int pageSize) {
|
||||
return template.getMapper(ArticleReader.class).findAll(articleId, pageSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ public final class ArticleQueryApi {
|
||||
|
||||
@GetMapping("/api/articles/all")
|
||||
public ResponseEntity<?> findAll(
|
||||
@RequestParam(value = "offset", defaultValue = "0") int offset,
|
||||
@RequestParam(value = "limit", defaultValue = "20") int limit) {
|
||||
@RequestParam(value = "articleId", defaultValue = "0") Long articleId,
|
||||
@RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
|
||||
return ResponseEntity.ok(
|
||||
ApiResult.success(articleFacade.findAll(offset, limit)));
|
||||
ApiResult.success(articleFacade.findAll(articleId, pageSize)));
|
||||
}
|
||||
|
||||
@GetMapping("/api/articles/{articleId}")
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<mapper namespace="com.yam.app.account.domain.AccountReader">
|
||||
|
||||
<select id="existsByEmail" parameterType="String" resultType="boolean">
|
||||
SELECT COUNT(email)
|
||||
SELECT COUNT(*)
|
||||
FROM account
|
||||
WHERE email = #{email}
|
||||
</select>
|
||||
|
||||
@@ -3,19 +3,7 @@
|
||||
|
||||
<mapper namespace="com.yam.app.article.domain.ArticleReader">
|
||||
|
||||
<select id="findAll" resultMap="articleId">
|
||||
SELECT
|
||||
DISTINCT a.id AS article_id, a.created_at
|
||||
FROM article a
|
||||
LEFT OUTER JOIN article_tag atg ON atg.article_id = a.id
|
||||
LEFT OUTER JOIN tag t ON t.id = atg.tag_id
|
||||
</select>
|
||||
|
||||
<resultMap id="articleId" type="Long">
|
||||
<id javaType="Long" column="article_id"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="findAllById" resultMap="articleDto" >
|
||||
<select id="findAll" resultMap="articleDto">
|
||||
SELECT a.id AS article_id,
|
||||
a.title AS article_title,
|
||||
a.status AS article_status,
|
||||
@@ -25,12 +13,15 @@
|
||||
m.nickname AS member_nickname,
|
||||
m.image AS member_image
|
||||
FROM article a
|
||||
INNER JOIN member m ON m.id = a.member_id
|
||||
WHERE a.id IN
|
||||
<foreach index="index" collection="idx" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
LIMIT #{offset}, #{limit}
|
||||
INNER JOIN member m on a.member_id = m.id
|
||||
LEFT OUTER JOIN article_tag atg ON atg.article_id = a.id
|
||||
LEFT OUTER JOIN tag t ON t.id = atg.tag_id
|
||||
WHERE a.status = 'ALIVE'
|
||||
<if test="articleId != 0">
|
||||
AND a.id lt #{articleId}
|
||||
</if>
|
||||
ORDER BY a.id desc
|
||||
LIMIT #{pageSize}
|
||||
</select>
|
||||
|
||||
<resultMap id="articleDto" type="com.yam.app.article.domain.ArticleDto">
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
</select>
|
||||
|
||||
<select id="existsByNickname" parameterType="String" resultType="boolean">
|
||||
SELECT COUNT(id) FROM member WHERE nickname = #{nickname}
|
||||
SELECT COUNT(*) FROM member WHERE nickname = #{nickname}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -38,12 +38,7 @@ public final class FakeArticleRepository implements ArticleRepository, ArticleRe
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> findAll() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArticleDto> findAllById(int offset, int limit, List<Long> idx) {
|
||||
public List<ArticleDto> findAll(Long articleId, int pageSize) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,6 @@ public final class ArticleReaderTest {
|
||||
|
||||
@Test
|
||||
void articlePaginationQueryTests() {
|
||||
List<Long> idx = articleReader.findAll();
|
||||
List<ArticleDto> dtos = articleReader.findAllById(1, 10, idx);
|
||||
|
||||
assertThat(dtos.size()).isEqualTo(10);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user