permission update

This commit is contained in:
Julian Krauser 2024-08-28 20:41:16 +02:00
parent 9808100d81
commit 6865507545
6 changed files with 100 additions and 5 deletions

View file

@ -0,0 +1,12 @@
export interface CreateRoleCommand {
role: string;
}
export interface UpdateRoleCommand {
id: number;
role: string;
}
export interface DeleteRoleCommand {
id: number;
}

View 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");
});
}
}

View file

@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler {
* @param DeleteRolePermissionCommand
* @returns {Promise<any>}
*/
static async deleteByToken(deletePermission: DeleteRolePermissionCommand): Promise<any> {
static async delete(deletePermission: DeleteRolePermissionCommand): Promise<any> {
return await dataSource
.createQueryBuilder()
.delete()

View file

@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler {
* @param DeleteUserPermissionCommand
* @returns {Promise<any>}
*/
static async deleteByToken(deletePermission: DeleteUserPermissionCommand): Promise<any> {
static async delete(deletePermission: DeleteUserPermissionCommand): Promise<any> {
return await dataSource
.createQueryBuilder()
.delete()

View file

@ -78,7 +78,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
columnNames: ["roleId"],
referencedColumnNames: ["id"],
referencedTableName: "role",
onDelete: "No Action",
onDelete: "CASCADE",
})
);
@ -88,7 +88,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
columnNames: ["userId"],
referencedColumnNames: ["id"],
referencedTableName: "user",
onDelete: "No Action",
onDelete: "CASCADE",
})
);
@ -98,7 +98,7 @@ export class RolePermission1724771491085 implements MigrationInterface {
columnNames: ["roleId"],
referencedColumnNames: ["id"],
referencedTableName: "role",
onDelete: "No Action",
onDelete: "CASCADE",
})
);
}

View file

@ -3,6 +3,23 @@ import { role } from "../entity/role";
import InternalException from "../exceptions/internalException";
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
* @param id number