Skip to content

Commit

Permalink
🎈 perf: 备忘录
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanjunjie committed Jul 26, 2024
1 parent 57f6ba0 commit c669c8c
Show file tree
Hide file tree
Showing 20 changed files with 630 additions and 96 deletions.
2 changes: 1 addition & 1 deletion nest-admin/servers/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import { MemorandumModule } from './website/memorandum-management/memorandum/mem
OssModule,
// 业务功能模块
MemorandumCategoryModule,
// MemorandumModule
MemorandumModule
],
// app module 守卫,两个守卫分别依赖 UserService、PermService, 而 UserService、PermService 没有设置全局模块,
// 所以这俩 守卫 不能再 main.ts 设置全局守卫
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class MemorandumCategoryEntity extends CommonEntity{
@Column({ type: 'varchar', length: 200, default: '', comment: '描述' })
public description: string

// @ManyToMany(() => MemorandumEntity, i=> i.categories)
// @JoinTable()
// memorandums: MemorandumEntity[];
@ManyToMany(() => MemorandumEntity, i => i.categories)
memorandums: MemorandumEntity[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { TypeOrmModule } from "@nestjs/typeorm"
import { MemorandumCategoryController } from './memorandum-category.controller'
import { MemorandumCategoryEntity } from './memorandum-category.entity'
import { MemorandumCategoryService } from "./memorandum-category.service"
import { MemorandumEntity } from "../memorandum/memorandum.entity"
import { MemorandumService } from "../memorandum/memorandum.service"

@Module({
imports: [TypeOrmModule.forFeature([MemorandumCategoryEntity])],
providers: [MemorandumCategoryService],
imports: [TypeOrmModule.forFeature([MemorandumCategoryEntity, MemorandumEntity])],
providers: [MemorandumCategoryService, MemorandumService],
controllers: [MemorandumCategoryController]
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ResultData } from 'src/common/utils/result'
import { AppHttpCode } from 'src/common/enums/code.enum'
import { plainToInstance } from 'class-transformer'
import { MemorandumService } from '../memorandum/memorandum.service'

@Injectable()
export class MemorandumCategoryService {
Expand All @@ -20,6 +21,7 @@ export class MemorandumCategoryService {
private readonly repository: Repository<MemorandumCategoryEntity>,
@InjectEntityManager()
private readonly manager: EntityManager,
private readonly memorandumService: MemorandumService,
) {}

/**
Expand Down Expand Up @@ -57,10 +59,10 @@ export class MemorandumCategoryService {
const existing = await this.getDetail(dto.id)
if (!existing) return ResultData.fail(AppHttpCode.ROLE_NOT_FOUND, '当前分类不存在或已被删除')

// todo 该分类存在备忘录数据, 则不允许被删除
// let len = 0;
// if (len > 0)
// return ResultData.fail(AppHttpCode.ROLE_NOT_DEL, '当前分类还有绑定的备忘录,需要解除关联后删除')
// 该分类存在备忘录数据, 则不允许被删除
let o = await this.memorandumService.getPage({ size: 1, page: 1, categories: [dto.id] })
if (o.data.total > 0)
return ResultData.fail(AppHttpCode.ROLE_NOT_DEL, '当前分类还有绑定的备忘录,需要解除关联后删除')

const { affected } = await this.manager.transaction(async (transactionalEntityManager) => {
const __data__ = plainToInstance(MemorandumCategoryEntity, {
Expand Down Expand Up @@ -142,10 +144,16 @@ export class MemorandumCategoryService {
where.createdAt = Between(dto.startDate, dto.endDate)
}
const list = await this.repository.find({
select: ['id', 'name'],
order: { createdAt: 'DESC' },
where,
})
return ResultData.ok({ list, total: list.length })
// 转换结果为 label 和 value 格式
const transformedResults = list.map(item => ({
label: item.name,
value: item.id.toString(),
}));
return ResultData.ok(transformedResults)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsString, Length, IsOptional, IsNotEmpty, IsIn } from 'class-validator'
import { StatusValue2 } from 'src/common/enums/common.enum'
import { ReqListQuery } from 'src/common/utils/req-list-query'
import { ReqQuery } from 'src/common/utils/req-query'
import { $enum } from 'ts-enum-util'

export class AddMemorandumDto extends ReqQuery {
@ApiProperty({ description: '标题' })
@IsString({ message: 'name 类型错误, 正确类型 string' })
@IsNotEmpty({ message: 'name 不能为空' })
@Length(0, 100, { message: 'name 字符长度在 0~100' })
readonly name: string

@ApiProperty({ description: '内容' })
@IsString({ message: 'content 类型错误, 正确类型 string' })
@IsNotEmpty({ message: 'content 不能为空' })
readonly content: string

@ApiProperty({ description: '备忘录分类', required: false })
@IsString( { each: true, message: 'id集合中存在类型错误, 正确类型 string[]' })
@IsNotEmpty({ message: 'categories 不能为空' })
readonly categories?: string[]
}

export class SetStickPost extends ReqQuery {
@ApiProperty({ description: 'id' })
@IsString({ message: 'id 类型错误,正确类型 number' })
@IsNotEmpty({ message: 'id 不能为空' })
readonly id: string

@ApiProperty({ description: '是否置顶 0-否 1-是', enum: $enum(StatusValue2).getValues() })
@IsNotEmpty({ message: 'stickyPost 不能为空' })
@IsIn($enum(StatusValue2).getValues(), { message: 'stickyPost 的值只能是 0/1' })
readonly stickyPost: StatusValue2
}

export class UpdateMemorandumDto extends AddMemorandumDto {
@ApiProperty({ description: 'id' })
@IsString({ message: 'id 类型错误,正确类型 number' })
@IsNotEmpty({ message: 'id 不能为空' })
id: string
}

export class DeleteMemorandumDto extends ReqQuery {
@ApiProperty({ description: 'id' })
@IsString({ message: 'id 类型错误,正确类型 number' })
@IsNotEmpty({ message: 'id 不能为空' })
id: string
}

export class FindPageDto extends ReqListQuery {

@ApiProperty({ description: '昵称模糊搜索', required: false })
readonly name?: string

@ApiProperty({ description: '开始时间', required: false })
readonly startDate?: Date

@ApiProperty({ description: '开始时间', required: false })
readonly endDate?: Date

@ApiProperty({ description: '备忘录分类', required: false })
readonly categories?: string[]
}

export class FindListDto {

@ApiProperty({ description: '昵称模糊搜索', required: false })
name?: string

@ApiProperty({ description: '开始时间', required: false })
startDate?: Date

@ApiProperty({ description: '开始时间', required: false })
endDate?: Date

@ApiProperty({ description: '备忘录分类', required: false })
readonly categories?: string[]
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,62 @@
import { Controller, Get, Post, UploadedFile, UseInterceptors, Query, HttpCode, Body, Req } from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { ApiTags, ApiOperation, ApiConsumes, ApiBody, ApiBearerAuth } from '@nestjs/swagger'
import { Controller, Get, Post, UseInterceptors, Query, Body, Delete, Put } from '@nestjs/common'
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'
import { ResultData } from 'src/common/utils/result'
import { UpdateInterceptor } from 'src/common/guards/update.interceptor'
import { ApiResult } from 'src/common/decorators/api-result.decorator'

import { MemorandumService } from './memorandum.service'
import { AddMemorandumDto, DeleteMemorandumDto, FindListDto, FindPageDto, SetStickPost, UpdateMemorandumDto } from './dto/memorandum.dto'
import { MemorandumEntity } from './memorandum.entity'

@ApiTags('备忘录类型')
@ApiTags('备忘录')
@ApiBearerAuth()
@Controller('memorandum')
export class MemorandumController {
constructor(private readonly service: MemorandumService) {}

@Post()
@ApiOperation({ summary: '新增' })
@ApiResult()
@UseInterceptors(UpdateInterceptor)
async create(@Body() dto: AddMemorandumDto): Promise<ResultData> {
return await this.service.add(dto)
}

@Delete()
@ApiOperation({ summary: '删除' })
@ApiResult()
@UseInterceptors(UpdateInterceptor)
async delete(@Body() dto: DeleteMemorandumDto): Promise<ResultData> {
return await this.service.delete(dto)
}

@Put()
@ApiOperation({ summary: '更新' })
@ApiResult()
@UseInterceptors(UpdateInterceptor)
async update(@Body() dto: UpdateMemorandumDto): Promise<ResultData> {
return await this.service.update(dto)
}

@Get('getPage')
@ApiOperation({ summary: '查询分页数据列表' })
@ApiResult(MemorandumEntity, true, true)
async getPage(@Query() dto: FindPageDto): Promise<ResultData> {
return await this.service.getPage(dto)
}

@Put('setStickPost')
@ApiOperation({ summary: '置顶文章' })
@ApiResult()
@UseInterceptors(UpdateInterceptor)
async setStickPost(@Body() dto: SetStickPost): Promise<ResultData> {
return await this.service.setStickPost(dto)
}

@Get('getDetail')
@ApiOperation({ summary: '查询分页数据列表' })
@ApiResult(MemorandumEntity, true, true)
async getDetail(@Query() dto: DeleteMemorandumDto): Promise<ResultData> {
return await this.service.getDetail2(dto.id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { $enum } from 'ts-enum-util'
import { StatusValue2 } from 'src/common/enums/common.enum'
import { MemorandumCategoryEntity } from '../category/memorandum-category.entity'

@Entity('website_memorandum_category')
@Entity('website_memorandum')
export class MemorandumEntity extends CommonEntity {
@ApiProperty({ type: String, description: '标题' })
@Column({ type: 'varchar', default: '', length: 100, comment: '标题' })
Expand All @@ -19,7 +19,7 @@ export class MemorandumEntity extends CommonEntity {
@Column({ type: 'text', comment: '内容' })
public content: string

// @ManyToMany(() => MemorandumCategoryEntity, i=> i.memorandums)
// @JoinTable()
// categories: MemorandumCategoryEntity[];
@ManyToMany(() => MemorandumCategoryEntity, category => category.memorandums, { cascade: true })
@JoinTable()
categories: MemorandumCategoryEntity[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { TypeOrmModule } from "@nestjs/typeorm"
import { MemorandumController } from './memorandum.controller'
import { MemorandumEntity } from './memorandum.entity'
import { MemorandumService } from "./memorandum.service"
import { MemorandumCategoryEntity } from "../category/memorandum-category.entity"

@Module({
imports: [TypeOrmModule.forFeature([MemorandumEntity])],
imports: [TypeOrmModule.forFeature([MemorandumEntity, MemorandumCategoryEntity])],
providers: [MemorandumService],
controllers: [MemorandumController]
})
Expand Down
Loading

0 comments on commit c669c8c

Please sign in to comment.