From 09b9830fd08a1e4796301fe50c9ad455a100f807 Mon Sep 17 00:00:00 2001 From: soraefir Date: Thu, 15 May 2025 00:35:39 +0200 Subject: [PATCH] Update --- src/client/api.ts | 6 ++++-- src/client/types/format.ts | 2 ++ src/client/types/migration.ts | 1 + src/server/api.ts | 19 +++++++++---------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/client/api.ts b/src/client/api.ts index 039cb54..9bb7386 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -41,8 +41,9 @@ export const load = (id: string) => return res; }); -export const save = (id: string, v: journey) => - fetch("/api/" + id, { method: "post", body: JSON.stringify(v) }) +export const save = async (id: string, v: journey) => { + v.version +=1; + return fetch("/api/" + id, { method: "post", body: JSON.stringify(v) }) .then((res) => { if (!res.ok) throw new Error("Error " + res.statusText); return res.json(); @@ -50,6 +51,7 @@ export const save = (id: string, v: journey) => .then((_res) => { console.log("Saved..."); }); + } export const query_nominatim = ( q: string, diff --git a/src/client/types/format.ts b/src/client/types/format.ts index d89a1e6..53871ab 100644 --- a/src/client/types/format.ts +++ b/src/client/types/format.ts @@ -35,6 +35,7 @@ declare global { interface journey { fmt_ver: number + version: number title: string main: leg[] } @@ -52,6 +53,7 @@ const leg_template: leg = { } const journey_template: journey = { fmt_ver: 1, + version: 0, title: "New Journey", main: [leg_template], } diff --git a/src/client/types/migration.ts b/src/client/types/migration.ts index 14f8963..c1a4c31 100644 --- a/src/client/types/migration.ts +++ b/src/client/types/migration.ts @@ -10,6 +10,7 @@ function migrate_A_to_0(e: journey): journey { v.travel = v.travel || []; v.day_title = typeof (v.day_title) == "string" ? [v.day_title] : [] }) + e.version = e.version || 0; console.log(e) return e; } diff --git a/src/server/api.ts b/src/server/api.ts index 9b8b531..70a6fcd 100644 --- a/src/server/api.ts +++ b/src/server/api.ts @@ -75,16 +75,15 @@ export default function (server, opts, done) { if (req.params.id == undefined) return reply.code(400).send({ error: "No ID query parameter" }); - server.level.db.put(req.params.id, req.body, (err) => { - if (err) { - console.warn(err); - reply.code(500).send({ error: "Error with DB" }); - } else { - reply.send({ content: "ok" }); - } - }); - return reply; - }); + return server.level.db.get(req.params.id).then(r=>r.version||-1).catch(_=>-1).then(db_version=>{ + if(db_version+1 == req.body.version) + return server.level.db.put(req.params.id, req.body) + .then(_=>reply.send({ content: "ok" })) + .catch(_=>reply.code(500).send({ error: "Error with DB" })) + + return reply.code(409).send({error:"Too old version, please refresh."}); + }) + }) done(); };