diff --git a/src/command/roleCommand.ts b/src/command/roleCommand.ts new file mode 100644 index 0000000..7638c8b --- /dev/null +++ b/src/command/roleCommand.ts @@ -0,0 +1,12 @@ +export interface CreateRoleCommand { + role: string; +} + +export interface UpdateRoleCommand { + id: number; + role: string; +} + +export interface DeleteRoleCommand { + id: number; +} diff --git a/src/command/roleCommandHandler.ts b/src/command/roleCommandHandler.ts new file mode 100644 index 0000000..3e9902b --- /dev/null +++ b/src/command/roleCommandHandler.ts @@ -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} + */ + static async create(createRole: CreateRoleCommand): Promise { + 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} + */ + static async update(updateRole: UpdateRoleCommand): Promise { + 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} + */ + static async delete(deleteRole: DeleteRoleCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(role) + .where("role.id = :id", { id: deleteRole.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new InternalException("Failed deleting role"); + }); + } +} diff --git a/src/command/rolePermissionCommandHandler.ts b/src/command/rolePermissionCommandHandler.ts index e5726dd..a2fe2ac 100644 --- a/src/command/rolePermissionCommandHandler.ts +++ b/src/command/rolePermissionCommandHandler.ts @@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler { * @param DeleteRolePermissionCommand * @returns {Promise} */ - static async deleteByToken(deletePermission: DeleteRolePermissionCommand): Promise { + static async delete(deletePermission: DeleteRolePermissionCommand): Promise { return await dataSource .createQueryBuilder() .delete() diff --git a/src/command/userPermissionCommandHandler.ts b/src/command/userPermissionCommandHandler.ts index 76d944e..d629677 100644 --- a/src/command/userPermissionCommandHandler.ts +++ b/src/command/userPermissionCommandHandler.ts @@ -33,7 +33,7 @@ export default abstract class UserPermissionCommandHandler { * @param DeleteUserPermissionCommand * @returns {Promise} */ - static async deleteByToken(deletePermission: DeleteUserPermissionCommand): Promise { + static async delete(deletePermission: DeleteUserPermissionCommand): Promise { return await dataSource .createQueryBuilder() .delete() diff --git a/src/migrations/1724771491085-role_permission.ts b/src/migrations/1724771491085-role_permission.ts index e658462..d40b148 100644 --- a/src/migrations/1724771491085-role_permission.ts +++ b/src/migrations/1724771491085-role_permission.ts @@ -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", }) ); } diff --git a/src/service/roleService.ts b/src/service/roleService.ts index c952802..2316d39 100644 --- a/src/service/roleService.ts +++ b/src/service/roleService.ts @@ -3,6 +3,23 @@ import { role } from "../entity/role"; import InternalException from "../exceptions/internalException"; export default abstract class RoleService { + /** + * @description get roles + * @returns {Promise>} + */ + static async getAll(): Promise> { + 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