70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
|
import { dataSource } from "../../../data-source";
|
||
|
import { salutation } from "../../../entity/settings/salutation";
|
||
|
import InternalException from "../../../exceptions/internalException";
|
||
|
import { CreateSalutationCommand, DeleteSalutationCommand, UpdateSalutationCommand } from "./salutationCommand";
|
||
|
|
||
|
export default abstract class SalutationCommandHandler {
|
||
|
/**
|
||
|
* @description create salutation
|
||
|
* @param {CreateSalutationCommand} createSalutation
|
||
|
* @returns {Promise<number>}
|
||
|
*/
|
||
|
static async create(createSalutation: CreateSalutationCommand): Promise<number> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.insert()
|
||
|
.into(salutation)
|
||
|
.values({
|
||
|
salutation: createSalutation.salutation,
|
||
|
})
|
||
|
.execute()
|
||
|
.then((result) => {
|
||
|
return result.identifiers[0].id;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed creating salutation", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description update salutation
|
||
|
* @param {UpdateSalutationCommand} updateSalutation
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async update(updateSalutation: UpdateSalutationCommand): Promise<void> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.update(salutation)
|
||
|
.set({
|
||
|
salutation: updateSalutation.salutation,
|
||
|
})
|
||
|
.where("id = :id", { id: updateSalutation.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed updating salutation", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description delete salutation
|
||
|
* @param {DeleteSalutationCommand} deleteSalutation
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async delete(deleteSalutation: DeleteSalutationCommand): Promise<void> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.delete()
|
||
|
.from(salutation)
|
||
|
.where("id = :id", { id: deleteSalutation.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException(
|
||
|
`Failed deleting salutation ${err.code.includes("ER_ROW_IS_REFERENCED") ? " due to referenced data" : ""}`,
|
||
|
err
|
||
|
);
|
||
|
});
|
||
|
}
|
||
|
}
|