permission update
This commit is contained in:
parent
9808100d81
commit
6865507545
6 changed files with 100 additions and 5 deletions
12
src/command/roleCommand.ts
Normal file
12
src/command/roleCommand.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
export interface CreateRoleCommand {
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UpdateRoleCommand {
|
||||||
|
id: number;
|
||||||
|
role: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DeleteRoleCommand {
|
||||||
|
id: number;
|
||||||
|
}
|
66
src/command/roleCommandHandler.ts
Normal file
66
src/command/roleCommandHandler.ts
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import { dataSource } from "../data-source";
|
||||||
|
import { role } from "../entity/role";
|
||||||
|
import InternalException from "../exceptions/internalException";
|
||||||
|
import { CreateRoleCommand, DeleteRoleCommand, UpdateRoleCommand } from "./roleCommand";
|
||||||
|
|
||||||
|
export default abstract class RoleCommandHandler {
|
||||||
|
/**
|
||||||
|
* @description create role
|
||||||
|
* @param CreateRoleCommand
|
||||||
|
* @returns {Promise<number>}
|
||||||
|
*/
|
||||||
|
static async create(createRole: CreateRoleCommand): Promise<number> {
|
||||||
|
return await dataSource
|
||||||
|
.createQueryBuilder()
|
||||||
|
.insert()
|
||||||
|
.into(role)
|
||||||
|
.values({
|
||||||
|
role: createRole.role,
|
||||||
|
})
|
||||||
|
.execute()
|
||||||
|
.then((result) => {
|
||||||
|
return result.identifiers[0].id;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new InternalException("Failed creating role");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description update role
|
||||||
|
* @param UpdateRoleCommand
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
static async update(updateRole: UpdateRoleCommand): Promise<void> {
|
||||||
|
return await dataSource
|
||||||
|
.createQueryBuilder()
|
||||||
|
.update(role)
|
||||||
|
.set({
|
||||||
|
role: updateRole.role,
|
||||||
|
})
|
||||||
|
.where("role.id = :id", { id: updateRole.id })
|
||||||
|
.execute()
|
||||||
|
.then(() => {})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new InternalException("Failed updating role");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description delete role
|
||||||
|
* @param DeleteRoleCommand
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
static async delete(deleteRole: DeleteRoleCommand): Promise<void> {
|
||||||
|
return await dataSource
|
||||||
|
.createQueryBuilder()
|
||||||
|
.delete()
|
||||||
|
.from(role)
|
||||||
|
.where("role.id = :id", { id: deleteRole.id })
|
||||||
|
.execute()
|
||||||
|
.then(() => {})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new InternalException("Failed deleting role");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler {
|
||||||
* @param DeleteRolePermissionCommand
|
* @param DeleteRolePermissionCommand
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
static async deleteByToken(deletePermission: DeleteRolePermissionCommand): Promise<any> {
|
static async delete(deletePermission: DeleteRolePermissionCommand): Promise<any> {
|
||||||
return await dataSource
|
return await dataSource
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
|
|
|
@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler {
|
||||||
* @param DeleteUserPermissionCommand
|
* @param DeleteUserPermissionCommand
|
||||||
* @returns {Promise<any>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
static async deleteByToken(deletePermission: DeleteUserPermissionCommand): Promise<any> {
|
static async delete(deletePermission: DeleteUserPermissionCommand): Promise<any> {
|
||||||
return await dataSource
|
return await dataSource
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.delete()
|
.delete()
|
||||||
|
|
|
@ -78,7 +78,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
|
||||||
columnNames: ["roleId"],
|
columnNames: ["roleId"],
|
||||||
referencedColumnNames: ["id"],
|
referencedColumnNames: ["id"],
|
||||||
referencedTableName: "role",
|
referencedTableName: "role",
|
||||||
onDelete: "No Action",
|
onDelete: "CASCADE",
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
|
||||||
columnNames: ["userId"],
|
columnNames: ["userId"],
|
||||||
referencedColumnNames: ["id"],
|
referencedColumnNames: ["id"],
|
||||||
referencedTableName: "user",
|
referencedTableName: "user",
|
||||||
onDelete: "No Action",
|
onDelete: "CASCADE",
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
|
||||||
columnNames: ["roleId"],
|
columnNames: ["roleId"],
|
||||||
referencedColumnNames: ["id"],
|
referencedColumnNames: ["id"],
|
||||||
referencedTableName: "role",
|
referencedTableName: "role",
|
||||||
onDelete: "No Action",
|
onDelete: "CASCADE",
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,23 @@ import { role } from "../entity/role";
|
||||||
import InternalException from "../exceptions/internalException";
|
import InternalException from "../exceptions/internalException";
|
||||||
|
|
||||||
export default abstract class RoleService {
|
export default abstract class RoleService {
|
||||||
|
/**
|
||||||
|
* @description get roles
|
||||||
|
* @returns {Promise<Array<role>>}
|
||||||
|
*/
|
||||||
|
static async getAll(): Promise<Array<role>> {
|
||||||
|
return await dataSource
|
||||||
|
.getRepository(role)
|
||||||
|
.createQueryBuilder("role")
|
||||||
|
.getMany()
|
||||||
|
.then((res) => {
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw new InternalException("roles not found");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description get role by id
|
* @description get role by id
|
||||||
* @param id number
|
* @param id number
|
||||||
|
|
Loading…
Reference in a new issue