salutation CRUD and controller
This commit is contained in:
parent
b55d0554e4
commit
1ab4d93d2b
10 changed files with 288 additions and 3 deletions
12
src/command/settings/salutation/salutationCommand.ts
Normal file
12
src/command/settings/salutation/salutationCommand.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
export interface CreateSalutationCommand {
|
||||
salutation: string;
|
||||
}
|
||||
|
||||
export interface UpdateSalutationCommand {
|
||||
id: number;
|
||||
salutation: string;
|
||||
}
|
||||
|
||||
export interface DeleteSalutationCommand {
|
||||
id: number;
|
||||
}
|
69
src/command/settings/salutation/salutationCommandHandler.ts
Normal file
69
src/command/settings/salutation/salutationCommandHandler.ts
Normal file
|
@ -0,0 +1,69 @@
|
|||
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
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue