1
0
Fork 0

[javascript] Completely reworked contracting

This commit is contained in:
Julien Dessaux 2023-09-18 22:48:08 +02:00
parent 3be4f0e9b4
commit 1b9a55432a
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 112 additions and 176 deletions

View file

@ -4,6 +4,7 @@ const addSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?)
const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`);
const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE data->>'symbol' = ?;`);
const getSystemsCountStatement = db.prepare(`SELECT COUNT(data) as data FROM systems;`);
const setSystemStatement = db.prepare(`UPDATE systems SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`);
const setSystemWaypointsStatement = db.prepare(`UPDATE systems SET data = (SELECT json_set(data, '$.waypoints', json(:waypoints)) FROM systems WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
export function addSystem(data) {
@ -34,6 +35,18 @@ export function getSystemUpdated(symbol) {
return updated.updated;
}
export function setSystem(data) {
if (getSystem(data.symbol) === null) {
addSystem(data);
} else {
return setSystemStatement.run({
data: JSON.stringify(data),
date: new Date().toISOString(),
symbol: data.symbol,
}).changes;
}
}
export function setSystemWaypoints(symbol, waypoints) {
return setSystemWaypointsStatement.run({
date: new Date().toISOString(),