dev v0.1.1 #3

Merged
chiko merged 4 commits from dev into main 2025-10-24 23:45:47 +00:00
2 changed files with 64 additions and 25 deletions
Showing only changes of commit eb6525a66f - Show all commits

View File

@@ -15,5 +15,6 @@ export type TEvent = {
location: string, location: string,
event_type: keyof typeof TEventType, event_type: keyof typeof TEventType,
timezone: string, timezone: string,
link: string link: string,
deleteDate?: number | null
}; };

View File

@@ -10,11 +10,16 @@ export type TGetEventsOptions = {
year: number, year: number,
month: number, month: number,
day: number day: number
} },
month?: {
year: number,
month: number,
},
deleted?: boolean
} }
export type TEventEntity = TEvent & { export type TEventEntity = TEvent & {
event_uid: number event_uid: number
notification: "new" | "changed" | "deleted" | "done" notification: "new" | "changed" | "removed" | "done"
} }
export type TEventEntityNew = Omit<TEventEntity, "event_uid"> export type TEventEntityNew = Omit<TEventEntity, "event_uid">
@@ -22,26 +27,34 @@ export type TEventEntityNew = Omit<TEventEntity, "event_uid">
export class Event implements TEventEntity { export class Event implements TEventEntity {
static table_name: "events" static table_name: "events"
static createTable (db: Database): void { static createTable (db: Database): void {
const query = db.query(`CREATE TABLE IF NOT EXISTS events ( const query = db.query(`CREATE TABLE IF NOT EXISTS "events" (
event_uid INTEGER PRIMARY KEY, "event_uid" INTEGER NOT NULL,
uid TEXT NOT NULL UNIQUE, "uid" TEXT NOT NULL,
title TEXT NOT NULL, "title" TEXT NOT NULL,
date_at DATETIME NOT NULL, "date_at" DATETIME NOT NULL,
time_start TEXT NOT NULL, "time_start" TEXT NOT NULL,
time_end TEXT NOT NULL, "time_end" TEXT NOT NULL,
posted_by TEXT NOT NULL, "posted_by" TEXT NOT NULL,
location TEXT NOT NULL, "location" TEXT NOT NULL,
event_type TEXT NOT NULL, "event_type" TEXT NOT NULL,
link TEXT NOT NULL, "link" TEXT NOT NULL,
description TEXT NOT NULL, "description" TEXT NOT NULL,
timezone TEXT NOT NULL, "timezone" TEXT NOT NULL,
notification TEXT NOT NULL DEFAULT "new" "notification" TEXT NOT NULL,
);`); "deleteDate" INTEGER NULL,
PRIMARY KEY ("event_uid")
);
CREATE UNIQUE INDEX "sqlite_autoindex_events_1" ON "events" ("uid");`);
query.run(); query.run();
} }
static insert ( events: TEventEntityNew[], db: Database ) { 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 => { const insertEvents = db.transaction(events => {
for (const event of events) insert.run(event); for (const event of events) insert.run(event);
return events.length; return events.length;
@@ -71,12 +84,21 @@ export class Event implements TEventEntity {
if (options.date) { if (options.date) {
whereConditions.push(`date_at = "${options.date.year}-${options.date.month}-${options.date.day}"`); 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 = ( () => { const where = ( () => {
let str = "WHERE "; let str = "WHERE ";
if ( whereConditions.length >= 1 ) { if ( options.deleted === true ) {
str += whereConditions.join(" OR "); 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); const query = db.query(`SELECT * FROM events${ where ? ( " " + where ) : ""};`).as(Event);
return query.all(); return query.all();
@@ -94,9 +116,10 @@ export class Event implements TEventEntity {
event_type: TEventEntity["event_type"]; event_type: TEventEntity["event_type"];
timezone: string; timezone: string;
link: 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.event_uid = event_uid;
this.uid = uid; this.uid = uid;
this.title = title; this.title = title;
@@ -110,9 +133,10 @@ export class Event implements TEventEntity {
this.timezone = timezone; this.timezone = timezone;
this.link = link; this.link = link;
this.notification = notification; this.notification = notification;
this.deleteDate = deleteDate;
} }
syncWithDb ( db: Database ) { 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 }); 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!`); } if ( ! entity ) { throw new Error(`Could not find Event with event_uid ${this.event_uid} in DB!`); }
this.uid = entity.uid; this.uid = entity.uid;
@@ -127,6 +151,7 @@ export class Event implements TEventEntity {
this.timezone = entity.timezone; this.timezone = entity.timezone;
this.link = entity.link; this.link = entity.link;
this.notification = entity.notification; this.notification = entity.notification;
this.deleteDate = entity.deleteDate;
return this; return this;
} }
@@ -137,5 +162,18 @@ export class Event implements TEventEntity {
WHERE event_uid = $event_uid;` WHERE event_uid = $event_uid;`
); );
query.get({$notification: newValue, $event_uid: this.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 );
} }
} }