summaryrefslogtreecommitdiff
path: root/nodejs/database
diff options
context:
space:
mode:
authorJulien Dessaux2023-09-18 22:48:08 +0200
committerJulien Dessaux2023-09-18 22:48:08 +0200
commit1b9a55432a0e621edf2c21c815b58b8cfa23a6d8 (patch)
treefa95e39adbea787934cba43132063b1252bede65 /nodejs/database
parent[javascript] fixed wrong variable usage (diff)
downloadspacetraders-1b9a55432a0e621edf2c21c815b58b8cfa23a6d8.tar.gz
spacetraders-1b9a55432a0e621edf2c21c815b58b8cfa23a6d8.tar.bz2
spacetraders-1b9a55432a0e621edf2c21c815b58b8cfa23a6d8.zip
[javascript] Completely reworked contracting
Diffstat (limited to 'nodejs/database')
-rw-r--r--nodejs/database/systems.js13
1 files changed, 13 insertions, 0 deletions
diff --git a/nodejs/database/systems.js b/nodejs/database/systems.js
index dd70d33..f989218 100644
--- a/nodejs/database/systems.js
+++ b/nodejs/database/systems.js
@@ -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(),