Refactor data handlers + misc fixes
This refactors the data handlers into a more standard/understood model-like architecture, to prepare for a new, more robust config system. It also fixes a problem with creating new Notes and uploading new Photos via the web interface (related to #58). Finally, it speeds up docker builds by sending in less files, which aren't necessary or will be built anyway. This is all in preparation to allow building #13 more robustly.
This commit is contained in:
45
lib/models/dashboard.ts
Normal file
45
lib/models/dashboard.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
import { Dashboard } from '/lib/types.ts';
|
||||
|
||||
const db = new Database();
|
||||
|
||||
export class DashboardModel {
|
||||
static async getByUserId(userId: string) {
|
||||
const dashboard =
|
||||
(await db.query<Dashboard>(sql`SELECT * FROM "bewcloud_dashboards" WHERE "user_id" = $1 LIMIT 1`, [
|
||||
userId,
|
||||
]))[0];
|
||||
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
static async create(userId: string) {
|
||||
const data: Dashboard['data'] = { links: [], notes: '' };
|
||||
|
||||
const newDashboard = (await db.query<Dashboard>(
|
||||
sql`INSERT INTO "bewcloud_dashboards" (
|
||||
"user_id",
|
||||
"data"
|
||||
) VALUES ($1, $2)
|
||||
RETURNING *`,
|
||||
[
|
||||
userId,
|
||||
JSON.stringify(data),
|
||||
],
|
||||
))[0];
|
||||
|
||||
return newDashboard;
|
||||
}
|
||||
|
||||
static async update(dashboard: Dashboard) {
|
||||
await db.query(
|
||||
sql`UPDATE "bewcloud_dashboards" SET
|
||||
"data" = $2
|
||||
WHERE "id" = $1`,
|
||||
[
|
||||
dashboard.id,
|
||||
JSON.stringify(dashboard.data),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user