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:
parent
754d175ce2
commit
6ea3567c94
8 changed files with 64 additions and 58 deletions
|
@ -1,11 +1,11 @@
|
|||
import { Schedule } from "~/shared/types/schedule";
|
||||
import { readFile, writeFile } from "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;
|
||||
|
@ -31,7 +31,7 @@ export default defineEventHandler(async (event) => {
|
|||
]
|
||||
};
|
||||
broadcastUpdate(schedule);
|
||||
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
|
||||
await writeSchedule(schedule);
|
||||
if (timeChanged)
|
||||
await sendPush(`New time for ${name}`, `${name} will now start at ${start}`);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue