ff-admin-server/src/command/calendarCommandHandler.ts
2024-11-07 10:49:34 +01:00

96 lines
2.9 KiB
TypeScript

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({
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<void>}
*/
static async update(updateCalendar: UpdateCalendarCommand): Promise<void> {
let sequence = await dataSource
.getRepository(calendar)
.createQueryBuilder("calendar")
.where("id = :id", { id: updateCalendar.id })
.getOneOrFail()
.then((res) => {
return res.sequence;
});
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(),
sequence: sequence + 1,
})
.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);
});
}
}