Add Versioning
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
soraefir 2025-05-14 23:21:41 +02:00
parent a2303c215e
commit 3d0c9a5719
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
4 changed files with 15 additions and 11 deletions

View File

@ -41,8 +41,9 @@ export const load = (id: string) =>
return res; return res;
}); });
export const save = (id: string, v: journey) => export const save = (id: string, v: journey) =>{
fetch("/api/" + id, { method: "post", body: JSON.stringify(v) }) v.version = v.version +1
return fetch("/api/" + id, { method: "post", body: JSON.stringify(v) })
.then((res) => { .then((res) => {
if (!res.ok) throw new Error("Error " + res.statusText); if (!res.ok) throw new Error("Error " + res.statusText);
return res.json(); return res.json();
@ -50,6 +51,7 @@ export const save = (id: string, v: journey) =>
.then((_res) => { .then((_res) => {
console.log("Saved..."); console.log("Saved...");
}); });
}
export const query_nominatim = ( export const query_nominatim = (
q: string, q: string,

View File

@ -37,6 +37,7 @@ declare global {
interface journey { interface journey {
fmt_ver: number fmt_ver: number
version: number
title: string title: string
main: leg[] main: leg[]
} }

View File

@ -10,6 +10,7 @@ function migrate_A_to_0(e: journey): journey {
v.travel = v.travel || []; v.travel = v.travel || [];
v.day_title = typeof (v.day_title) == "string" ? [v.day_title] : []; v.day_title = typeof (v.day_title) == "string" ? [v.day_title] : [];
}) })
e.version = e.version | 0;
console.log(e) console.log(e)
return e; return e;
} }

View File

@ -72,18 +72,18 @@ export default function (server, opts, done) {
}); });
server.post("/:id", async (req, reply) => { server.post("/:id", async (req, reply) => {
if (req.params.id == undefined) if (req.params.id == undefined )
return reply.code(400).send({ error: "No ID query parameter" }); return reply.code(400).send({ error: "No ID query parameter" });
server.level.db.put(req.params.id, req.body, (err) => { return server.level.db.get(req.params.id).then(r=>r.version>=0?r.version:-1).catch(_=>-1).then(db_ver=>{
if (err) { if(db_ver+1 == req.body.version){
console.warn(err); return server.level.db.put(req.params.id, req.body)
reply.code(500).send({ error: "Error with DB" }); .then(_=>reply.send({ content: "ok" }))
} else { .catch(_err=>reply.code(500).send({ error: "Error with DB" }));
reply.send({ content: "ok" }); }else{
return reply.code(409).send({ error: "Old version, please refresh" });
} }
}); })
return reply;
}); });
done(); done();