summaryrefslogtreecommitdiff
path: root/nodejs/database/ships.js
blob: 0220be62354cd9b01d84f6b3d229113a028d99b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import db from './db.js';

const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
const setShipStatement = db.prepare(`INSERT INTO ships(data, updated) VALUES (json(?), ?);`);
const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`);

export function getShip(symbol) {
	try {
		const data = getShipStatement.get(symbol);
		if (data === undefined) {
			return null;
		}
		return JSON.parse(data.data);
	} catch (err) {
		console.log(err);
		return null;
	}
}

export function setShip(data) {
	if (getShip(data.symbol) === null) {
		try {
			return setShipStatement.run(JSON.stringify(data), new Date().toISOString()).lastInsertRowid;
		} catch (err) {
			console.log(err);
			return null;
		}
	} else {
		try {
			return updateShipStatement.run({
				data: JSON.stringify(data),
				date: new Date().toISOString(),
				symbol: data.symbol,
			}).changes;
		} catch (err) {
			console.log(err);
			return null;
		}
	}
}

export function setShipCargo(symbol, cargo) {
	try {
		setShipCargoStatement.run({
			cargo: JSON.stringify(cargo),
			date: new Date().toISOString(),
			symbol: symbol,
		}).changes;
	} catch (err) {
		console.log(err);
		return null;
	}
}

export function setShipFuel(symbol, fuel) {
	try {
		setShipFuelStatement.run({
			date: new Date().toISOString(),
			fuel: JSON.stringify(fuel),
			symbol: symbol,
		}).changes;
	} catch (err) {
		console.log(err);
		return null;
	}
}

export function setShipNav(symbol, nav) {
	try {
		setShipNavStatement.run({
			date: new Date().toISOString(),
			nav: JSON.stringify(nav),
			symbol: symbol,
		}).changes;
	} catch (err) {
		console.log(err);
		return null;
	}
}