Compare commits
4 Commits
62a6070bfd
...
83ae38edd1
Author | SHA1 | Date | |
---|---|---|---|
|
83ae38edd1 | ||
|
38d895a959 | ||
|
6690b055b4 | ||
|
09b9830fd0 |
@ -41,15 +41,20 @@ export const load = (id: string) =>
|
|||||||
return res;
|
return res;
|
||||||
});
|
});
|
||||||
|
|
||||||
export const save = (id: string, v: journey) =>
|
var version_add = 1
|
||||||
fetch("/api/" + id, { method: "post", body: JSON.stringify(v) })
|
export const save = async (id: string, v: journey) => {
|
||||||
|
let body = JSON.parse(JSON.stringify(v))
|
||||||
|
body.version +=version_add;
|
||||||
|
return fetch("/api/" + id, { method: "post", body: JSON.stringify(body) })
|
||||||
.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();
|
||||||
})
|
})
|
||||||
.then((_res) => {
|
.then((_res) => {
|
||||||
|
version_add+=1;
|
||||||
console.log("Saved...");
|
console.log("Saved...");
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export const query_nominatim = (
|
export const query_nominatim = (
|
||||||
q: string,
|
q: string,
|
||||||
|
@ -35,6 +35,7 @@ declare global {
|
|||||||
|
|
||||||
interface journey {
|
interface journey {
|
||||||
fmt_ver: number
|
fmt_ver: number
|
||||||
|
version: number
|
||||||
title: string
|
title: string
|
||||||
main: leg[]
|
main: leg[]
|
||||||
}
|
}
|
||||||
@ -52,6 +53,7 @@ const leg_template: leg = {
|
|||||||
}
|
}
|
||||||
const journey_template: journey = {
|
const journey_template: journey = {
|
||||||
fmt_ver: 1,
|
fmt_ver: 1,
|
||||||
|
version: 0,
|
||||||
title: "New Journey",
|
title: "New Journey",
|
||||||
main: [leg_template],
|
main: [leg_template],
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -75,16 +75,15 @@ export default function (server, opts, done) {
|
|||||||
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||-1).catch(_=>-1).then(db_version=>{
|
||||||
if (err) {
|
if(db_version+1 == req.body.version || db_version == -1)
|
||||||
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(_=>reply.code(500).send({ error: "Error with DB" }))
|
||||||
reply.send({ content: "ok" });
|
|
||||||
}
|
return reply.code(409).send({error:"Too old version, please refresh."});
|
||||||
});
|
})
|
||||||
return reply;
|
})
|
||||||
});
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user