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,10 +1,10 @@
import { Schedule } from "~/shared/types/schedule";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "~/server/streams";
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 index = schedule.events.findIndex(event => event.id === id);
if (index === -1) {
@ -12,5 +12,5 @@ export default defineEventHandler(async (event) => {
}
schedule.events.splice(index, 1);
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
await writeSchedule(schedule);
});