diff --git a/src/component/event/event.types.ts b/src/component/event/event.types.ts index 0e64dff..636f029 100644 --- a/src/component/event/event.types.ts +++ b/src/component/event/event.types.ts @@ -15,5 +15,6 @@ export type TEvent = { location: string, event_type: keyof typeof TEventType, timezone: string, - link: string + link: string, + deleteDate?: number | null }; \ No newline at end of file diff --git a/src/component/event/events.ts b/src/component/event/events.ts index c39958d..c9d9ce1 100644 --- a/src/component/event/events.ts +++ b/src/component/event/events.ts @@ -10,11 +10,16 @@ export type TGetEventsOptions = { year: number, month: number, day: number - } + }, + month?: { + year: number, + month: number, + }, + deleted?: boolean } export type TEventEntity = TEvent & { event_uid: number - notification: "new" | "changed" | "deleted" | "done" + notification: "new" | "changed" | "removed" | "done" } export type TEventEntityNew = Omit @@ -22,26 +27,34 @@ export type TEventEntityNew = Omit export class Event implements TEventEntity { static table_name: "events" static createTable (db: Database): void { - const query = db.query(`CREATE TABLE IF NOT EXISTS events ( - event_uid INTEGER PRIMARY KEY, - uid TEXT NOT NULL UNIQUE, - title TEXT NOT NULL, - date_at DATETIME NOT NULL, - time_start TEXT NOT NULL, - time_end TEXT NOT NULL, - posted_by TEXT NOT NULL, - location TEXT NOT NULL, - event_type TEXT NOT NULL, - link TEXT NOT NULL, - description TEXT NOT NULL, - timezone TEXT NOT NULL, - notification TEXT NOT NULL DEFAULT "new" - );`); + const query = db.query(`CREATE TABLE IF NOT EXISTS "events" ( + "event_uid" INTEGER NOT NULL, + "uid" TEXT NOT NULL, + "title" TEXT NOT NULL, + "date_at" DATETIME NOT NULL, + "time_start" TEXT NOT NULL, + "time_end" TEXT NOT NULL, + "posted_by" TEXT NOT NULL, + "location" TEXT NOT NULL, + "event_type" TEXT NOT NULL, + "link" TEXT NOT NULL, + "description" TEXT NOT NULL, + "timezone" TEXT NOT NULL, + "notification" TEXT NOT NULL, + "deleteDate" INTEGER NULL, + PRIMARY KEY ("event_uid") + ); + CREATE UNIQUE INDEX "sqlite_autoindex_events_1" ON "events" ("uid");`); query.run(); } static insert ( events: TEventEntityNew[], db: Database ) { - const insert = db.prepare("INSERT OR REPLACE INTO events (uid, title, date_at, time_start, time_end, posted_by, location, event_type, link, description, timezone, notification) VALUES ($uid, $title, $date_at, $time_start, $time_end, $posted_by, $location, $event_type, $link, $description, $timezone, $notification)"); + const insert = db.prepare( [ + "INSERT OR REPLACE INTO events", + "(uid, title, date_at, time_start, time_end, posted_by, location, event_type, link, description, timezone, notification)", + "VALUES", + "($uid, $title, $date_at, $time_start, $time_end, $posted_by, $location, $event_type, $link, $description, $timezone, $notification)" + ].join(" ")); const insertEvents = db.transaction(events => { for (const event of events) insert.run(event); return events.length; @@ -71,12 +84,21 @@ export class Event implements TEventEntity { if (options.date) { whereConditions.push(`date_at = "${options.date.year}-${options.date.month}-${options.date.day}"`); } + if ( options.month ) { + whereConditions.push( `strftime('%Y-%m', date_at) = '${options.month.year}-${options.month.month}'`) + } + const where = ( () => { let str = "WHERE "; - if ( whereConditions.length >= 1 ) { - str += whereConditions.join(" OR "); + if ( options.deleted === true ) { + str += "deleteDate IS NOT NULL AND "; + } else if ( options.deleted === false ) { + str += "deleteDate IS NULL AND "; } - return str; + if ( whereConditions.length >= 1 ) { + return str += `( ${ whereConditions.join(" OR ") } )`; + } + return null; })() const query = db.query(`SELECT * FROM events${ where ? ( " " + where ) : ""};`).as(Event); return query.all(); @@ -94,9 +116,10 @@ export class Event implements TEventEntity { event_type: TEventEntity["event_type"]; timezone: string; link: string; - notification: TEventEntity["notification"] + notification: TEventEntity["notification"]; + deleteDate: TEventEntity["deleteDate"]; - constructor(event_uid: number, uid: string, title: string, description: string, date_at: string, time_start: string, time_end: string, posted_by: string, location: string, event_type: TEventEntity["event_type"], timezone: string, link: string, notification: TEventEntity["notification"]) { + constructor(event_uid: number, uid: string, title: string, description: string, date_at: string, time_start: string, time_end: string, posted_by: string, location: string, event_type: TEventEntity["event_type"], timezone: string, link: string, notification: TEventEntity["notification"], deleteDate: TEventEntity["deleteDate"]) { this.event_uid = event_uid; this.uid = uid; this.title = title; @@ -110,9 +133,10 @@ export class Event implements TEventEntity { this.timezone = timezone; this.link = link; this.notification = notification; + this.deleteDate = deleteDate; } syncWithDb ( db: Database ) { - const query = db.prepare( `SELECT * FROM ${Event.table_name} WHERE event_uid = $event_uid;`).as(Event); + const query = db.prepare( `SELECT * FROM events WHERE event_uid = $event_uid;`).as(Event); const entity = query.get({$event_uid: this.event_uid }); if ( ! entity ) { throw new Error(`Could not find Event with event_uid ${this.event_uid} in DB!`); } this.uid = entity.uid; @@ -127,6 +151,7 @@ export class Event implements TEventEntity { this.timezone = entity.timezone; this.link = entity.link; this.notification = entity.notification; + this.deleteDate = entity.deleteDate; return this; } @@ -137,5 +162,18 @@ export class Event implements TEventEntity { WHERE event_uid = $event_uid;` ); query.get({$notification: newValue, $event_uid: this.event_uid }); + return this.syncWithDb( db ); + } + set_deleted ( db: Database ) { + const query = db.prepare( + `UPDATE events + SET deleteDate = $deleteDate + WHERE event_uid = $event_uid;` + ); + query.get({ + $deleteDate: Math.floor((new Date()).getTime() / 1000), + $event_uid: this.event_uid + }); + return this.syncWithDb( db ); } } \ No newline at end of file