diff --git a/src/article/article.service.ts b/src/article/article.service.ts index 3557a36..9879d29 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; }