From 8c597fd68d52f256f7e07aa5ade0a2cf7ddf52ad Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 28 Oct 2024 16:02:56 +0100 Subject: [PATCH] calendar fixes --- src/command/calendarCommand.ts | 10 ++++--- src/command/calendarCommandHandler.ts | 6 ++-- src/controller/admin/calendarController.ts | 16 +++++++---- src/entity/calendar.ts | 33 ++++++++++++++++------ src/factory/admin/calendar.ts | 5 +++- src/migrations/1729947763295-calendar.ts | 18 +++++++++--- src/service/calendarService.ts | 2 ++ src/viewmodel/admin/calendar.models.ts | 7 +++-- 8 files changed, 70 insertions(+), 27 deletions(-) diff --git a/src/command/calendarCommand.ts b/src/command/calendarCommand.ts index d216657..e6365db 100644 --- a/src/command/calendarCommand.ts +++ b/src/command/calendarCommand.ts @@ -1,22 +1,24 @@ export interface CreateCalendarCommand { - date: Date; starttime: Date; endtime: Date; title: string; content: string; + location: string; + allDay: boolean; typeId: number; } export interface UpdateCalendarCommand { - id: number; - date: Date; + id: string; starttime: Date; endtime: Date; title: string; content: string; + location: string; + allDay: boolean; typeId: number; } export interface DeleteCalendarCommand { - id: number; + id: string; } diff --git a/src/command/calendarCommandHandler.ts b/src/command/calendarCommandHandler.ts index 927b542..082695f 100644 --- a/src/command/calendarCommandHandler.ts +++ b/src/command/calendarCommandHandler.ts @@ -16,11 +16,12 @@ export default abstract class CalendarCommandHandler { .insert() .into(calendar) .values({ - date: createCalendar.date, 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") @@ -46,11 +47,12 @@ export default abstract class CalendarCommandHandler { .createQueryBuilder() .update(calendar) .set({ - date: updateCalendar.date, 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") diff --git a/src/controller/admin/calendarController.ts b/src/controller/admin/calendarController.ts index 7fbfb67..7a23803 100644 --- a/src/controller/admin/calendarController.ts +++ b/src/controller/admin/calendarController.ts @@ -69,19 +69,21 @@ export async function getCalendarTypeById(req: Request, res: Response): Promise< * @returns {Promise<*>} */ export async function createCalendarItem(req: Request, res: Response): Promise { - const date = req.body.date; const starttime = req.body.starttime; const endtime = req.body.endtime; const title = req.body.title; const content = req.body.content; + const location = req.body.location; + const allDay = req.body.allDay; const typeId = req.body.typeId; let createItem: CreateCalendarCommand = { - date, starttime, endtime, title, content, + location, + allDay, typeId, }; let id = await CalendarCommandHandler.create(createItem); @@ -117,21 +119,23 @@ export async function createCalendarType(req: Request, res: Response): Promise} */ export async function updateCalendarItem(req: Request, res: Response): Promise { - const id = parseInt(req.params.id); - const date = req.body.date; + const id = req.params.id; const starttime = req.body.starttime; const endtime = req.body.endtime; const title = req.body.title; const content = req.body.content; + const location = req.body.location; + const allDay = req.body.allDay; const typeId = req.body.typeId; let updateItem: UpdateCalendarCommand = { id, - date, starttime, endtime, title, content, + location, + allDay, typeId, }; await CalendarCommandHandler.update(updateItem); @@ -169,7 +173,7 @@ export async function updateCalendarType(req: Request, res: Response): Promise} */ export async function deleteCalendarItem(req: Request, res: Response): Promise { - const id = parseInt(req.params.id); + const id = req.params.id; let deleteItem: DeleteCalendarCommand = { id, diff --git a/src/entity/calendar.ts b/src/entity/calendar.ts index 1d1852a..ffdeceb 100644 --- a/src/entity/calendar.ts +++ b/src/entity/calendar.ts @@ -1,18 +1,23 @@ -import { Column, Entity, ManyToOne, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { + Column, + Entity, + ManyToOne, + PrimaryColumn, + PrimaryGeneratedColumn, + CreateDateColumn, + UpdateDateColumn, +} from "typeorm"; import { calendarType } from "./calendarType"; @Entity() export class calendar { - @PrimaryGeneratedColumn("increment") - id: number; + @PrimaryGeneratedColumn("uuid") + id: string; - @Column({ type: "date", nullable: false }) - date: Date; - - @Column({ type: "timestamp", nullable: true }) + @Column({ type: "datetime", nullable: false }) starttime: Date; - @Column({ type: "datetime", nullable: true }) + @Column({ type: "datetime", nullable: false }) endtime: Date; @Column({ type: "varchar", length: 255, nullable: false }) @@ -21,6 +26,18 @@ export class calendar { @Column({ type: "text", nullable: true }) content: string; + @Column({ type: "text", nullable: true }) + location: string; + + @Column({ type: "boolean", default: false }) + allDay: boolean; + + @CreateDateColumn() + createdAt: Date; + + @UpdateDateColumn() + updatedAt: Date; + @ManyToOne(() => calendarType, (t) => t.calendar, { nullable: false, onDelete: "RESTRICT", diff --git a/src/factory/admin/calendar.ts b/src/factory/admin/calendar.ts index 460552c..aa29be2 100644 --- a/src/factory/admin/calendar.ts +++ b/src/factory/admin/calendar.ts @@ -11,11 +11,14 @@ export default abstract class CalendarFactory { public static mapToSingle(record: calendar): CalendarViewModel { return { id: record.id, - date: record.date, starttime: record.starttime, endtime: record.endtime, title: record.title, content: record.content, + location: record.location, + allDay: record.allDay, + createdAt: record.createdAt, + updatedAt: record.updatedAt, type: CalendarTypeFactory.mapToSingle(record.type), }; } diff --git a/src/migrations/1729947763295-calendar.ts b/src/migrations/1729947763295-calendar.ts index f8b5688..82146fe 100644 --- a/src/migrations/1729947763295-calendar.ts +++ b/src/migrations/1729947763295-calendar.ts @@ -23,12 +23,22 @@ export class Calendar1729947763295 implements MigrationInterface { new Table({ name: "calendar", columns: [ - { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, - { name: "date", type: "date", isNullable: false }, - { name: "starttime", type: "timestamp", isNullable: true }, - { name: "endtime", type: "datetime", isNullable: true }, + { name: "id", type: "varchar", length: "36", isPrimary: true, isGenerated: true, generationStrategy: "uuid" }, + { name: "starttime", type: "datetime", isNullable: false }, + { name: "endtime", type: "datetime", isNullable: false }, { name: "title", type: "varchar", length: "255", isNullable: false }, { name: "content", type: "text", isNullable: true }, + { name: "allDay", type: "tinyint", isNullable: false, default: 0 }, + { name: "location", type: "text", isNullable: true }, + { name: "createdAt", type: "datetime", precision: 6, isNullable: false, default: "CURRENT_TIMESTAMP(6)" }, + { + name: "updatedAt", + type: "datetime", + precision: 6, + isNullable: false, + default: "CURRENT_TIMESTAMP(6)", + onUpdate: "CURRENT_TIMESTAMP(6)", + }, { name: "typeId", type: variableType_int, isNullable: false }, ], }) diff --git a/src/service/calendarService.ts b/src/service/calendarService.ts index 99f0e7d..87aaac4 100644 --- a/src/service/calendarService.ts +++ b/src/service/calendarService.ts @@ -11,6 +11,7 @@ export default abstract class CalendarService { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") + .leftJoinAndSelect("calendar.type", "type") .getMany() .then((res) => { return res; @@ -28,6 +29,7 @@ export default abstract class CalendarService { return await dataSource .getRepository(calendar) .createQueryBuilder("calendar") + .leftJoinAndSelect("calendar.type", "type") .where("calendar.id = :id", { id: id }) .getOneOrFail() .then((res) => { diff --git a/src/viewmodel/admin/calendar.models.ts b/src/viewmodel/admin/calendar.models.ts index 536882a..b65fb88 100644 --- a/src/viewmodel/admin/calendar.models.ts +++ b/src/viewmodel/admin/calendar.models.ts @@ -1,11 +1,14 @@ import { CalendarTypeViewModel } from "./calendarType.models"; export interface CalendarViewModel { - id: number; - date: Date; + id: string; starttime: Date; endtime: Date; title: string; content: string; + location: string; + allDay: boolean; + createdAt: Date; + updatedAt: Date; type: CalendarTypeViewModel; }