import { dataSource } from "../data-source"; import { template } from "../entity/template"; import InternalException from "../exceptions/internalException"; import { CreateTemplateCommand, DeleteTemplateCommand, UpdateTemplateCommand } from "./templateCommand"; export default abstract class TemplateCommandHandler { /** * @description create template * @param CreateTemplateCommand * @returns {Promise} */ static async create(createTemplate: CreateTemplateCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(template) .values({ template: createTemplate.template, description: createTemplate.description, }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating template", err); }); } /** * @description update template * @param UpdateTemplateCommand * @returns {Promise} */ static async update(updateTemplate: UpdateTemplateCommand): Promise { return await dataSource .createQueryBuilder() .update(template) .set({ template: updateTemplate.template, description: updateTemplate.description, design: updateTemplate.design, headerHTML: updateTemplate.headerHTML, bodyHTML: updateTemplate.bodyHTML, footerHTML: updateTemplate.footerHTML, }) .where("id = :id", { id: updateTemplate.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating template", err); }); } /** * @description delete template * @param DeleteTemplateCommand * @returns {Promise} */ static async delete(deletTemplate: DeleteTemplateCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(template) .where("id = :id", { id: deletTemplate.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting template", err); }); } }