2025-01-05 14:14:00 +01:00
|
|
|
import { dataSource } from "../../../data-source";
|
|
|
|
import { role } from "../../../entity/user/role";
|
2025-01-29 09:42:22 +01:00
|
|
|
import DatabaseActionException from "../../../exceptions/databaseActionException";
|
2025-01-05 14:14:00 +01:00
|
|
|
import InternalException from "../../../exceptions/internalException";
|
2024-08-28 20:41:16 +02:00
|
|
|
import { CreateRoleCommand, DeleteRoleCommand, UpdateRoleCommand } from "./roleCommand";
|
|
|
|
|
|
|
|
export default abstract class RoleCommandHandler {
|
|
|
|
/**
|
|
|
|
* @description create role
|
2025-01-05 14:29:31 +01:00
|
|
|
* @param {CreateRoleCommand} createRole
|
2024-08-28 20:41:16 +02:00
|
|
|
* @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) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("CREATE", "role", err);
|
2024-08-28 20:41:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update role
|
2025-01-05 14:29:31 +01:00
|
|
|
* @param {UpdateRoleCommand} updateRole
|
2024-08-28 20:41:16 +02:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async update(updateRole: UpdateRoleCommand): Promise<void> {
|
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.update(role)
|
|
|
|
.set({
|
|
|
|
role: updateRole.role,
|
|
|
|
})
|
2024-09-05 16:17:22 +02:00
|
|
|
.where("id = :id", { id: updateRole.id })
|
2024-08-28 20:41:16 +02:00
|
|
|
.execute()
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("UPDATE", "role", err);
|
2024-08-28 20:41:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description delete role
|
2025-01-05 14:29:31 +01:00
|
|
|
* @param {DeleteRoleCommand} deleteRole
|
2024-08-28 20:41:16 +02:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async delete(deleteRole: DeleteRoleCommand): Promise<void> {
|
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.delete()
|
|
|
|
.from(role)
|
2024-09-05 16:17:22 +02:00
|
|
|
.where("id = :id", { id: deleteRole.id })
|
2024-08-28 20:41:16 +02:00
|
|
|
.execute()
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("DELETE", "role", err);
|
2024-08-28 20:41:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|