-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Aiden-FE <[email protected]>
- Loading branch information
Showing
25 changed files
with
407 additions
and
23 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
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,3 @@ | ||
deploy/mysql | ||
deploy/postgres | ||
deploy/redis |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
import { IsArray, IsString, MaxLength, MinLength } from 'class-validator'; | ||
import { IsArray, IsString, MaxLength, MinLength, IsOptional } from 'class-validator'; | ||
import { DESC_MAX_LIMIT, TITLE_MAX_LIMIT, TITLE_MIN_LIMIT } from '@shared'; | ||
import { Optional } from '@nestjs/common'; | ||
|
||
export class RoleCreateDto { | ||
@IsString() | ||
@MaxLength(TITLE_MAX_LIMIT) | ||
@MinLength(TITLE_MIN_LIMIT) | ||
name: string; | ||
|
||
@Optional() | ||
@IsOptional() | ||
@MaxLength(DESC_MAX_LIMIT) | ||
description?: string; | ||
|
||
@Optional() | ||
@IsOptional() | ||
@IsArray() | ||
permissions?: string[]; | ||
} |
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,47 @@ | ||
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'; | ||
import { ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
import { User } from '@shared'; | ||
import { TodoService } from './todo.service'; | ||
import { CreateTodoDto, DeleteTodoDto, ModifyTodoDto, TodoListQueryDto } from './todo.dto'; | ||
import { UserContextDto } from '../user/user.dto'; | ||
|
||
@ApiTags('待办事项') | ||
@Controller('todo') | ||
export class TodoController { | ||
constructor(private todoService: TodoService) {} | ||
|
||
@ApiOperation({ | ||
summary: '获取待办事项列表', | ||
}) | ||
@Get() | ||
getTodos(@Query() query: TodoListQueryDto, @User() user: UserContextDto) { | ||
return this.todoService.getTodoList(query, user.id); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: '创建待办事项', | ||
}) | ||
@Post() | ||
createTodo(@Body() body: CreateTodoDto, @User() user: UserContextDto) { | ||
return this.todoService.createTodo({ | ||
...body, | ||
userId: user.id, | ||
}); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: '修改待办事项', | ||
}) | ||
@Put() | ||
modifyTodo(@Body() body: ModifyTodoDto, @User() user: UserContextDto) { | ||
return this.todoService.modifyTodo(body, user.id); | ||
} | ||
|
||
@ApiOperation({ | ||
summary: '删除待办事项', | ||
}) | ||
@Delete() | ||
deleteTodo(@Body() body: DeleteTodoDto, @User() user: UserContextDto) { | ||
return this.todoService.deleteTodo(body.id, user.id); | ||
} | ||
} |
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,41 @@ | ||
import { | ||
DESC_MAX_LIMIT, | ||
KEYWORD_MAX_LIMIT, | ||
PaginationRequestFromURLDto, | ||
TITLE_MAX_LIMIT, | ||
TITLE_MIN_LIMIT, | ||
} from '@shared'; | ||
import { IsBoolean, IsString, MaxLength, MinLength, IsOptional, IsNumber } from 'class-validator'; | ||
|
||
export class TodoListQueryDto extends PaginationRequestFromURLDto { | ||
@IsOptional() | ||
@IsString() | ||
@MaxLength(KEYWORD_MAX_LIMIT) | ||
keyword?: string; | ||
} | ||
|
||
export class CreateTodoDto { | ||
@IsString() | ||
@MaxLength(TITLE_MAX_LIMIT) | ||
@MinLength(TITLE_MIN_LIMIT) | ||
title: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
@MaxLength(DESC_MAX_LIMIT) | ||
description?: string; | ||
|
||
@IsOptional() | ||
@IsBoolean() | ||
isFinished?: boolean; | ||
} | ||
|
||
export class ModifyTodoDto extends CreateTodoDto { | ||
@IsNumber() | ||
id: number; | ||
} | ||
|
||
export class DeleteTodoDto { | ||
@IsNumber() | ||
id: number; | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { DBService } from '@app/db'; | ||
import { TodoController } from './todo.controller'; | ||
import { TodoService } from './todo.service'; | ||
|
||
@Module({ | ||
controllers: [TodoController], | ||
providers: [TodoService, DBService], | ||
}) | ||
export class TodoModule {} |
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,90 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DBService } from '@app/db'; | ||
import { HttpResponse, PaginationParams, PaginationResponse, ResponseCode } from '@shared'; | ||
import { CreateTodoDto, ModifyTodoDto, TodoListQueryDto } from './todo.dto'; | ||
|
||
@Injectable() | ||
export class TodoService { | ||
constructor(private dbService: DBService) {} | ||
|
||
async deleteTodo(id: number, userId: string) { | ||
const todo = await this.dbService.todo.findFirst({ | ||
where: { | ||
id, | ||
userId, | ||
}, | ||
}); | ||
if (!todo) { | ||
return new HttpResponse(null, { | ||
statusCode: ResponseCode.NOT_FOUND, | ||
message: '未找到可被删除的资源', | ||
}); | ||
} | ||
return this.dbService.todo.delete({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
} | ||
|
||
async modifyTodo(body: ModifyTodoDto, userId: string) { | ||
const todo = await this.dbService.todo.findFirst({ | ||
where: { | ||
id: body.id, | ||
userId, | ||
}, | ||
}); | ||
if (!todo) { | ||
return new HttpResponse(null, { | ||
statusCode: ResponseCode.NOT_FOUND, | ||
message: '未找到要更新的资源', | ||
}); | ||
} | ||
return this.dbService.todo.update({ | ||
where: { | ||
id: body.id, | ||
}, | ||
data: body, | ||
}); | ||
} | ||
|
||
async getTodoList(query: TodoListQueryDto, userId: string) { | ||
const pagination = PaginationParams(query); | ||
const where = { | ||
userId, | ||
title: { | ||
contains: query.keyword, | ||
}, | ||
}; | ||
const [total, list] = await this.dbService.$transaction([ | ||
this.dbService.todo.count({ where }), | ||
this.dbService.todo.findMany({ | ||
skip: pagination.skip, | ||
take: pagination.take, | ||
where, | ||
orderBy: [{ updatedAt: 'desc' }], | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
isFinished: true, | ||
updatedAt: true, | ||
}, | ||
}), | ||
]); | ||
|
||
return new PaginationResponse({ | ||
total, | ||
list, | ||
...pagination, | ||
}); | ||
} | ||
|
||
createTodo(todo: CreateTodoDto & { userId: string }) { | ||
return this.dbService.todo.create({ | ||
data: { | ||
...todo, | ||
}, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.