实现权限管理的服务

This commit is contained in:
2025-05-08 22:26:04 +08:00
parent ff4c755fc8
commit 1b73ef6bc9
5 changed files with 59 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { BadRequestException, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Role } from "../entities/role.entity";
import { In, Repository } from "typeorm";
@@ -23,4 +23,21 @@ export class RoleService {
}
})
}
async create(role: Pick<Role, 'name' | 'localName'>): Promise<Role> {
const newRole = this.roleRepository.create(role);
return this.roleRepository.save(newRole);
}
async list(): Promise<Role[]> {
return this.roleRepository.find();
}
async delete(roleId: string): Promise<void> {
const existingRole = await this.roleRepository.findOne({ where: { id: roleId } });
if (!existingRole) {
throw new BadRequestException('Role not found');
}
await this.roleRepository.delete(existingRole.id);
}
}