import { HttpException , Inject , Injectable } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { __ as t } from 'i18n' ; import { Repository } from 'typeorm' ; import { Role } from '../entities/role.entity' ; import { EntityCheckService } from './entity-check.service' ; @ Injectable ( ) export class RoleService { constructor ( @ Inject ( EntityCheckService ) private readonly entityCheckService , @ InjectRepository ( Role ) private readonly roleRepo ) { } param name async createRole ( name ) { await this . entityCheckService . checkNameExist ( Role , name ) ; await this . roleRepo . save ( this . roleRepo . create ( { name } ) ) ; } param id param name async updateRole ( id , name ) { const role = await this . roleRepo . findOne ( id ) ; if ( ! role ) { throw new HttpException ( t ( 'The role id of %s does not exist' , id . toString ( ) ) , 404 ) ; } if ( name !== role . name ) { await this . entityCheckService . checkNameExist ( Role , name ) ; } role . name = name ; await this . roleRepo . save ( role ) ; } param id async deleteRole ( id ) { const role = await this . roleRepo . findOne ( id , { relations : [ 'permissions' ] } ) ; if ( ! role ) { throw new HttpException ( t ( 'The role id of %s does not exist' , id . toString ( ) ) , 404 ) ; } try { await this . roleRepo . remove ( role ) ; } catch ( err ) { throw new HttpException ( t ( 'Database error %s' , err . toString ( ) ) , 500 ) ; } } /**
     * Query all roles
     */ async findRoles ( ) { return this . roleRepo . find ( ) ; } }