Refactor code saving data

Move the code dealing with saving and loading data to server/database to
gather it all up into one place.
This commit is contained in:
Hornwitser 2025-03-05 18:41:47 +01:00
parent 754d175ce2
commit 6ea3567c94
8 changed files with 64 additions and 58 deletions

View file

@ -1,11 +1,11 @@
import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "node:fs/promises";
import { broadcastUpdate } from "~/server/streams";
import { sendPush } from "~/server/web-push";
import { readSchedule, writeSchedule } from "~/server/database";
export default defineEventHandler(async (event) => {
const formData = await readFormData(event);
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const schedule: Schedule = await readSchedule();
const id = formData.get("id") as string;
const name = formData.get("name") as string;
const description = formData.get("description") as string;
@ -26,6 +26,6 @@ export default defineEventHandler(async (event) => {
]
});
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
await writeSchedule(schedule);
await sendPush("New event", `${name} will start at ${start}`);
});