import { dataSource } from "../data-source"; import { calendar } from "../entity/calendar"; import { calendarType } from "../entity/calendarType"; import InternalException from "../exceptions/internalException"; import { CreateCalendarCommand, DeleteCalendarCommand, UpdateCalendarCommand } from "./calendarCommand"; export default abstract class CalendarCommandHandler { /** * @description create calendar * @param CreateCalendarCommand * @returns {Promise} */ static async create(createCalendar: CreateCalendarCommand): Promise { return await dataSource .createQueryBuilder() .insert() .into(calendar) .values({ starttime: createCalendar.starttime, endtime: createCalendar.endtime, title: createCalendar.title, content: createCalendar.content, location: createCalendar.location, allDay: createCalendar.allDay, type: await dataSource .getRepository(calendarType) .createQueryBuilder("type") .where("id = :id", { id: createCalendar.typeId }) .getOneOrFail(), }) .execute() .then((result) => { return result.identifiers[0].id; }) .catch((err) => { throw new InternalException("Failed creating calendar", err); }); } /** * @description update calendar * @param UpdateCalendarCommand * @returns {Promise} */ static async update(updateCalendar: UpdateCalendarCommand): Promise { return await dataSource .createQueryBuilder() .update(calendar) .set({ starttime: updateCalendar.starttime, endtime: updateCalendar.endtime, title: updateCalendar.title, content: updateCalendar.content, location: updateCalendar.location, allDay: updateCalendar.allDay, type: await dataSource .getRepository(calendarType) .createQueryBuilder("type") .where("id = :id", { id: updateCalendar.typeId }) .getOneOrFail(), }) .where("id = :id", { id: updateCalendar.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed updating award", err); }); } /** * @description delete calendar * @param DeleteCalendarCommand * @returns {Promise} */ static async delete(deleteCalendar: DeleteCalendarCommand): Promise { return await dataSource .createQueryBuilder() .delete() .from(calendar) .where("id = :id", { id: deleteCalendar.id }) .execute() .then(() => {}) .catch((err) => { throw new InternalException("Failed deleting calendar", err); }); } }