From 64aa2501552490eeb3eff052c2b7ab9421269b81 Mon Sep 17 00:00:00 2001 From: minorcell Date: Sat, 17 May 2025 18:56:24 +0800 Subject: [PATCH] feat: add pagination and sorting to article query endpoint --- src/article/article.service.ts | 19 ++++++++++++------- src/article/dto/query-article.dto.ts | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/article/article.service.ts b/src/article/article.service.ts index ed1f280..ec9a07b 100644 --- a/src/article/article.service.ts +++ b/src/article/article.service.ts @@ -65,7 +65,7 @@ export class ArticleService { articleAuthor, startTime, endTime, - showAll = false, // 默认只显示激活的文章 + isActive, } = query; const skip = (page - 1) * limit; @@ -97,12 +97,17 @@ export class ArticleService { ); } - // 如果不是显示所有,只显示激活的文章 - if (!showAll) { - queryBuilder.andWhere('article.isActive = :isActive', { isActive: true }) - .andWhere('(article.startTime IS NULL OR article.startTime <= :now)') - .andWhere('(article.endTime IS NULL OR article.endTime >= :now)') - .setParameter('now', new Date()); + // 根据激活状态过滤 + if (isActive !== undefined) { + queryBuilder.andWhere('article.isActive = :isActive', { isActive }); + + // 如果是查询激活的文章,还需要检查时间范围 + if (isActive === true) { + queryBuilder + .andWhere('(article.startTime IS NULL OR article.startTime <= :now)') + .andWhere('(article.endTime IS NULL OR article.endTime >= :now)') + .setParameter('now', new Date()); + } } // 执行查询并获取结果 diff --git a/src/article/dto/query-article.dto.ts b/src/article/dto/query-article.dto.ts index 768696c..75d103b 100644 --- a/src/article/dto/query-article.dto.ts +++ b/src/article/dto/query-article.dto.ts @@ -44,7 +44,7 @@ export class QueryArticleDto { * 只有管理员可以设置为true */ @IsOptional() - @IsBoolean({ message: '显示所有文章必须是布尔值' }) + @IsBoolean({ message: '文章状态必须是布尔值' }) @Type(() => Boolean) - showAll?: boolean = false; + isActive?: boolean; }