67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
|
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");
|
||
|
});
|
||
|
}
|
||
|
}
|