Skip to content

Commit

Permalink
feat: 1、新增 文章模块 api,查询未分类的文章数量
Browse files Browse the repository at this point in the history
2、修改查询判断,可以查询未分类的文章
  • Loading branch information
kangodyan committed Nov 24, 2023
1 parent 807719b commit c7489b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/modules/article/controller/article.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ export class ArticleController extends BaseController<ArticleService> {
getMdFileData(@Query('fileName') fileName: string) {
return this.service.getMdFileData(fileName);
}

/**
* 查询未分类的文章数量
*/
@Get('countNotClassesArticle')
countNotClassesArticle() {
return this.service.countNotClassesArticle();
}
}
16 changes: 13 additions & 3 deletions src/modules/article/service/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
import { join } from 'path';

import { Injectable } from '@nestjs/common';
import { isEmpty, omit } from 'lodash';
import { isEmpty, isUndefined, omit } from 'lodash';

import { SelectQueryBuilder } from 'typeorm';

Expand Down Expand Up @@ -45,6 +45,16 @@ export class ArticleService extends BaseService<ArticleEntity, ArticleRepository
return mdFileData;
}

/**
* 查询未分类的文章数量
*/
async countNotClassesArticle() {
const data: [{ count: number }] = await this.repository.manager.query(
'SELECT count(*) AS count FROM article WHERE classes = ""',
);
return data[0].count;
}

/**
* 新建博客
* @param data
Expand Down Expand Up @@ -94,8 +104,8 @@ export class ArticleService extends BaseService<ArticleEntity, ArticleRepository
if (!isEmpty(title)) {
qb.andWhere(`${queryName}.title like '%${title}%'`);
}
if (!isEmpty(classes)) {
qb.andWhere(`${queryName}.classes = '%${classes}%'`);
if (!isUndefined(classes)) {
qb.andWhere(`${queryName}.classes = '${classes}'`);
}
if (!isEmpty(tags)) {
// 把'yyds,awsl'转换为"'yyds','awsl'"
Expand Down
2 changes: 1 addition & 1 deletion src/modules/classes/service/classes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ClassesService extends BaseService<ClassesEntity, ClassesRepository
return this.repository.manager.query(
' SELECT' +
' cl.id AS classesId,' +
' MAX(cl.content) AS classesName,' +
' cl.content AS classesName,' +
' COUNT(ar.id) AS count ' +
'FROM' +
' classes cl ' +
Expand Down

0 comments on commit c7489b0

Please sign in to comment.