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({
|
|
|
|
starttime: createCalendar.starttime,
|
|
|
|
endtime: createCalendar.endtime,
|
|
|
|
title: createCalendar.title,
|
|
|
|
content: createCalendar.content,
|
2024-10-28 15:02:56 +00:00
|
|
|
location: createCalendar.location,
|
|
|
|
allDay: createCalendar.allDay,
|
2024-10-27 10:47:13 +00:00
|
|
|
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({
|
|
|
|
starttime: updateCalendar.starttime,
|
|
|
|
endtime: updateCalendar.endtime,
|
|
|
|
title: updateCalendar.title,
|
|
|
|
content: updateCalendar.content,
|
2024-10-28 15:02:56 +00:00
|
|
|
location: updateCalendar.location,
|
|
|
|
allDay: updateCalendar.allDay,
|
2024-10-27 10:47:13 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|