ff-admin-server/src/command/calendarCommandHandler.ts

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-27 10:47:13 +00:00
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<number>}
*/
static async create(createCalendar: CreateCalendarCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(calendar)
.values({
date: createCalendar.date,
starttime: createCalendar.starttime,
endtime: createCalendar.endtime,
title: createCalendar.title,
content: createCalendar.content,
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<void>}
*/
static async update(updateCalendar: UpdateCalendarCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(calendar)
.set({
date: updateCalendar.date,
starttime: updateCalendar.starttime,
endtime: updateCalendar.endtime,
title: updateCalendar.title,
content: updateCalendar.content,
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<void>}
*/
static async delete(deleteCalendar: DeleteCalendarCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.delete()
.from(calendar)
.where("id = :id", { id: deleteCalendar.id })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting calendar", err);
});
}
}