-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3bf5c7
commit b6ee0a7
Showing
13 changed files
with
247 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nest-server": minor | ||
--- | ||
|
||
feat: NestJS category 模块接口实现 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"node": { | ||
"index": { | ||
"prefix": "https://cdn.npmmirror.com/binaries/node/" | ||
}, | ||
"distro": { | ||
"template": "https://registry.npmmirror.com/-/binary/node/v{{version}}/node-v{{version}}-{{os}}-{{arch}}.zip" | ||
} | ||
}, | ||
"npm": { | ||
"index": { | ||
"prefix": "https://registry.npmmirror.com/npm/" | ||
}, | ||
"distro": { | ||
"template": "https://registry.npmmirror.com/npm/-/npm-{{version}}.tgz" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## 准备环境变量 | ||
|
||
开发环境可以准备一个`.env.development.local`文件,内容如下: | ||
|
||
``` | ||
PORT=8003 | ||
MYSQL_HOST=127.0.0.1 | ||
MYSQL_PORT=3306 | ||
MYSQL_USER=root | ||
MYSQL_PASSWORD=xxx | ||
MYSQL_DATABASE_NAME=blog_db | ||
JWT_SECRET=xxx | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/nest-server/src/modules/category/category.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CategoryController } from './category.controller'; | ||
import { CategoryService } from './category.service'; | ||
|
||
describe('CategoryController', () => { | ||
let controller: CategoryController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [CategoryController], | ||
providers: [CategoryService], | ||
}).compile(); | ||
|
||
controller = module.get<CategoryController>(CategoryController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
app/nest-server/src/modules/category/category.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Body, Controller, Get, Put, Query } from "@nestjs/common"; | ||
import { CategoryService } from "./category.service"; | ||
import { PublicAccess } from "@/decorators/public-access.decorator"; | ||
import { FuzzyQueryCategoriesDto, GetAllCategoriesDto, GetCategoryAdminPageDto, UpdateCategoryDto } from "./dto/category.dto"; | ||
|
||
@Controller("category") | ||
export class CategoryController { | ||
constructor(private readonly categoryService: CategoryService) {} | ||
|
||
@PublicAccess() | ||
@Get("/all") | ||
getAllCategories(@Query() query: GetAllCategoriesDto) { | ||
return query.getCount === "1" ? this.categoryService.getAllCategoriesWithArticleCount() : this.categoryService.getAllCategories(); | ||
} | ||
|
||
@Get("/fuzzy") | ||
fuzzyQueryCategories(@Query() query: FuzzyQueryCategoriesDto) { | ||
return this.categoryService.fuzzyQueryCategories(query.wd); | ||
} | ||
|
||
@Get("/admin/page") | ||
getCategoryAdminPage(@Query() query: GetCategoryAdminPageDto) { | ||
return this.categoryService.getCategoryAdminPageWithArticleCount(query); | ||
} | ||
|
||
@Put("/admin/update") | ||
updateCategory(@Body() body: UpdateCategoryDto) { | ||
return this.categoryService.updateCategory(body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { CategoryService } from "./category.service"; | ||
import { CategoryController } from "./category.controller"; | ||
import { Category } from "@/entities/Category"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Category])], | ||
controllers: [CategoryController], | ||
providers: [CategoryService], | ||
}) | ||
export class CategoryModule {} |
18 changes: 18 additions & 0 deletions
18
app/nest-server/src/modules/category/category.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CategoryService } from './category.service'; | ||
|
||
describe('CategoryService', () => { | ||
let service: CategoryService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CategoryService], | ||
}).compile(); | ||
|
||
service = module.get<CategoryService>(CategoryService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Category } from "@/entities/Category"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { Like, Repository } from "typeorm"; | ||
import { GetCategoryAdminPageDto, UpdateCategoryDto } from "./dto/category.dto"; | ||
|
||
@Injectable() | ||
export class CategoryService { | ||
constructor(@InjectRepository(Category) private readonly categoryRepository: Repository<Category>) {} | ||
|
||
async getAllCategories() { | ||
const data = await this.categoryRepository.find(); | ||
return { | ||
data, | ||
}; | ||
} | ||
|
||
async getAllCategoriesWithArticleCount() { | ||
const categories = await this.categoryRepository | ||
.createQueryBuilder("category") | ||
.leftJoin("category.articles", "article") | ||
.select("category.*") | ||
.addSelect("COUNT(article.id)", "category_count") | ||
.groupBy("category.id") | ||
.getRawMany(); | ||
return { | ||
data: categories.map((category) => ({ | ||
...category, | ||
category_count: parseInt(category.category_count), | ||
})), | ||
}; | ||
} | ||
|
||
async fuzzyQueryCategories(wd: string) { | ||
const data = await this.categoryRepository.find({ where: { category_name: Like(`%${wd}%`) } }); | ||
return { | ||
data, | ||
}; | ||
} | ||
|
||
async getCategoryAdminPageWithArticleCount(query: GetCategoryAdminPageDto) { | ||
const { pageNo, pageSize } = query; | ||
const builder = this.categoryRepository | ||
.createQueryBuilder("category") | ||
.leftJoin("category.articles", "article") | ||
.select("category.*") | ||
.addSelect("COUNT(article.id)", "article_count") | ||
.groupBy("category.id") | ||
.offset((pageNo - 1) * pageSize) | ||
.limit(pageSize); | ||
|
||
const [data, total] = await Promise.all([builder.getRawMany(), builder.getCount()]); | ||
|
||
return { | ||
data: data.map((category) => ({ | ||
...category, | ||
article_count: parseInt(category.article_count), | ||
})), | ||
total, | ||
}; | ||
} | ||
|
||
async updateCategory(body: UpdateCategoryDto) { | ||
const { id, category_name, poster } = body; | ||
await this.categoryRepository.update(id, { category_name, poster }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { QueryPositiveInt } from "@/decorators/query-number.decorator"; | ||
import { IsIn, IsInt, IsNotEmpty, IsOptional, IsPositive, isPositive, IsString, ValidateIf } from "class-validator"; | ||
|
||
export class GetAllCategoriesDto { | ||
@IsOptional() | ||
@IsString() | ||
@IsIn(["0", "1"], { message: "必须是0或1" }) | ||
getCount?: "0" | "1"; | ||
} | ||
|
||
export class FuzzyQueryCategoriesDto { | ||
@IsString() | ||
@IsNotEmpty({ message: "不能为空" }) | ||
wd: string; | ||
} | ||
|
||
export class GetCategoryAdminPageDto { | ||
@IsOptional() | ||
@QueryPositiveInt(1) | ||
pageNo: number = 1; | ||
|
||
@IsOptional() | ||
@QueryPositiveInt(10) | ||
pageSize: number = 10; | ||
} | ||
|
||
export class UpdateCategoryDto { | ||
@IsInt() | ||
@IsPositive() | ||
id?: number; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
category_name: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
poster?: string; | ||
} |
1 change: 1 addition & 0 deletions
1
app/nest-server/src/modules/category/entities/category.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Category {} |