refactor:替换自增id为cuid
This commit is contained in:
@@ -19,12 +19,12 @@ export class AuthController {
|
||||
// 授权用户角色
|
||||
@Post('userRole/:id')
|
||||
grantUserRole(@Param('id') id: string, @Body() dto: UserRoleDto) {
|
||||
return this.authService.grantUserRole(+id, dto);
|
||||
return this.authService.grantUserRole(id, dto);
|
||||
}
|
||||
|
||||
// 授权角色权限
|
||||
@Post('rolePermission/:id')
|
||||
grantRolePermission(@Param('id') id: string, @Body() dto: RolePermissionDto) {
|
||||
return this.authService.grantRolePermission(+id, dto);
|
||||
return this.authService.grantRolePermission(id, dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ export class AuthService {
|
||||
return { accessToken };
|
||||
}
|
||||
|
||||
async grantUserRole(id: number, { roleIds }: UserRoleDto) {
|
||||
async grantUserRole(id: string, { roleIds }: UserRoleDto) {
|
||||
const user = await this.prisma.sysUser.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
@@ -96,7 +96,7 @@ export class AuthService {
|
||||
});
|
||||
}
|
||||
|
||||
async grantRolePermission(id: number, { permissionIds }: RolePermissionDto) {
|
||||
async grantRolePermission(id: string, { permissionIds }: RolePermissionDto) {
|
||||
const role = await this.prisma.sysRole.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ArrayUnique, IsInt, IsNotEmpty } from 'class-validator';
|
||||
import { ArrayUnique, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class RolePermissionDto {
|
||||
@IsNotEmpty()
|
||||
@IsInt({ each: true })
|
||||
@IsString({ each: true })
|
||||
@ArrayUnique()
|
||||
permissionIds: number[];
|
||||
permissionIds: string[];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ArrayUnique, IsInt, IsNotEmpty } from 'class-validator';
|
||||
import { ArrayUnique, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UserRoleDto {
|
||||
@IsNotEmpty()
|
||||
@IsInt({ each: true })
|
||||
@IsString({ each: true })
|
||||
@ArrayUnique()
|
||||
roleIds: number[];
|
||||
roleIds: string[];
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export class FilesService {
|
||||
|
||||
const filePath = await this.minioService.uploadFile(file, fileName);
|
||||
|
||||
const res = await this.prisma.files.create({
|
||||
const res = await this.prisma.file.create({
|
||||
data: { name: fileName, path: filePath },
|
||||
});
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ export class CreateMenuDto {
|
||||
url: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
parentId?: number;
|
||||
@IsString()
|
||||
parentId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
|
||||
@@ -32,11 +32,11 @@ export class MenuController {
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateMenuDto: UpdateMenuDto) {
|
||||
return this.menuService.update(+id, updateMenuDto);
|
||||
return this.menuService.update(id, updateMenuDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: number) {
|
||||
remove(@Param('id') id: string) {
|
||||
return this.menuService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { PermissionType } from '@/common/enum';
|
||||
export class MenuService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
findOne(params: { id?: number; excludeId?: number }) {
|
||||
findOne(params: { id?: string; excludeId?: string }) {
|
||||
const { id, excludeId } = params;
|
||||
return this.prisma.sysMenu.findFirst({
|
||||
where: {
|
||||
@@ -19,7 +19,7 @@ export class MenuService {
|
||||
});
|
||||
}
|
||||
|
||||
findOneById(id: number) {
|
||||
findOneById(id: string) {
|
||||
return this.prisma.sysMenu.findUnique({ where: { id } });
|
||||
}
|
||||
|
||||
@@ -35,15 +35,20 @@ export class MenuService {
|
||||
});
|
||||
}
|
||||
|
||||
generatorMenu(data, parentId = 0) {
|
||||
generatorMenu(data, parentId?: string) {
|
||||
const menu = [];
|
||||
for (const item of data) {
|
||||
if (item.parentId === parentId) {
|
||||
const children = this.generatorMenu(data, item.id);
|
||||
if (children.length) {
|
||||
item.children = children;
|
||||
}
|
||||
// 不存在父id 代表是顶级菜单
|
||||
if (!parentId) {
|
||||
menu.push(item);
|
||||
} else {
|
||||
if (item.parentId === parentId) {
|
||||
const children = this.generatorMenu(data, item.id);
|
||||
if (children.length) {
|
||||
item.children = children;
|
||||
}
|
||||
menu.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
return menu;
|
||||
@@ -69,7 +74,7 @@ export class MenuService {
|
||||
return this.prisma.sysMenu.findMany(query);
|
||||
}
|
||||
|
||||
async update(id: number, data: UpdateMenuDto) {
|
||||
async update(id: string, data: UpdateMenuDto) {
|
||||
const doesExists = await this.findOne({ id });
|
||||
if (!doesExists) {
|
||||
throw new HttpException('菜单不存在!', HttpStatus.NOT_FOUND);
|
||||
@@ -85,7 +90,7 @@ export class MenuService {
|
||||
await this.prisma.sysMenu.update({ data, where: { id } });
|
||||
}
|
||||
|
||||
async remove(menuId: number) {
|
||||
async remove(menuId: string) {
|
||||
const doesExists = await this.findOneById(menuId);
|
||||
if (!doesExists) {
|
||||
throw new HttpException('菜单不存在!', HttpStatus.NOT_FOUND);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsNumber } from 'class-validator';
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class GrantMenuDto {
|
||||
@IsNumber({}, { each: true })
|
||||
menuIds: number[];
|
||||
@IsString({ each: true })
|
||||
menuIds: string[];
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ export class PermissionController {
|
||||
constructor(private readonly permissionService: PermissionService) {}
|
||||
|
||||
@Get(':id/menu')
|
||||
getMenu(@Param('id') id: number) {
|
||||
getMenu(@Param('id') id: string) {
|
||||
return this.permissionService.getMenu(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
grantMenu(@Param('id') id: number, @Body() dto: GrantMenuDto) {
|
||||
grantMenu(@Param('id') id: string, @Body() dto: GrantMenuDto) {
|
||||
return this.permissionService.grantMenu(id, dto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { PrismaService } from '@/prisma/prisma.service';
|
||||
export class PermissionService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getMenu(id: number) {
|
||||
async getMenu(id: string) {
|
||||
const menuIds = await this.prisma.sysMenuPermission.findMany({
|
||||
select: {
|
||||
menuId: true,
|
||||
@@ -23,7 +23,7 @@ export class PermissionService {
|
||||
return menuIds.map((item) => item.menuId);
|
||||
}
|
||||
|
||||
async grantMenu(id: number, { menuIds }: GrantMenuDto) {
|
||||
async grantMenu(id: string, { menuIds }: GrantMenuDto) {
|
||||
await this.prisma.$transaction(async (prisma) => {
|
||||
// 删除角色对应的权限
|
||||
await prisma.sysRolePermission.deleteMany({
|
||||
|
||||
@@ -34,11 +34,11 @@ export class RoleController {
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateRoleDto) {
|
||||
return this.roleService.update(+id, dto);
|
||||
return this.roleService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.roleService.remove(+id);
|
||||
return this.roleService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ export class RoleService {
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: number, data: UpdateRoleDto) {
|
||||
async update(id: string, data: UpdateRoleDto) {
|
||||
const role = await this.findOne({ id });
|
||||
if (!role)
|
||||
throw new HttpException('角色信息不存在!', HttpStatus.NOT_FOUND);
|
||||
@@ -71,7 +71,7 @@ export class RoleService {
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
async remove(id: string) {
|
||||
const role = await this.findOne({ id });
|
||||
if (!role)
|
||||
throw new HttpException('角色信息不存在!', HttpStatus.NOT_FOUND);
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
@@ -17,8 +18,8 @@ export class CreateUserDto {
|
||||
account: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
avatarId: number;
|
||||
@IsString()
|
||||
avatarId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
|
||||
@@ -37,22 +37,22 @@ export class UserController {
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOneById(@Param('id') id: number) {
|
||||
findOneById(@Param('id') id: string) {
|
||||
return this.userService.findOneById(id);
|
||||
}
|
||||
|
||||
@Patch(':id/password')
|
||||
updatePassword(@Param('id') id: string, @Body() dto: UpdatePasswordDto) {
|
||||
return this.userService.updatePassword(+id, dto);
|
||||
return this.userService.updatePassword(id, dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateUserDto) {
|
||||
return this.userService.update(+id, dto);
|
||||
return this.userService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.userService.remove(+id);
|
||||
return this.userService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ export class UserService {
|
||||
};
|
||||
}
|
||||
|
||||
async findOneById(id: number) {
|
||||
async findOneById(id: string) {
|
||||
return this.prisma.sysUser.findUnique({
|
||||
select: {
|
||||
id: true,
|
||||
@@ -112,7 +112,7 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
async getUserInfo(id: number, isAdmin: boolean) {
|
||||
async getUserInfo(id: string, isAdmin: boolean) {
|
||||
const user = await this.prisma.sysUser.findUnique({
|
||||
select: {
|
||||
name: true,
|
||||
@@ -154,7 +154,7 @@ export class UserService {
|
||||
};
|
||||
}
|
||||
|
||||
async update(id: number, data: UpdateUserDto) {
|
||||
async update(id: string, data: UpdateUserDto) {
|
||||
const user = await this.findOne({ id });
|
||||
if (!user)
|
||||
throw new HttpException('修改失败,用户不存在!', HttpStatus.NOT_FOUND);
|
||||
@@ -178,7 +178,7 @@ export class UserService {
|
||||
}
|
||||
|
||||
async updatePassword(
|
||||
id: number,
|
||||
id: string,
|
||||
{ password, newPassword }: UpdatePasswordDto,
|
||||
) {
|
||||
const user = await this.findOne({ id });
|
||||
@@ -203,7 +203,7 @@ export class UserService {
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
async remove(id: string) {
|
||||
const user = await this.findOne({ id });
|
||||
|
||||
if (!user)
|
||||
|
||||
Reference in New Issue
Block a user