feat: moved database preparation to database file & now possible to add transactions with post

This commit is contained in:
2025-05-07 17:22:32 +02:00
parent 314955efcf
commit dd8e3e9449
3 changed files with 70 additions and 25 deletions

View File

@@ -1,5 +1,33 @@
import Database from "better-sqlite3";
export default function getDB() {
function getDB() {
return new Database('store.db', { verbose: console.log })
}
}
function prepareDB(db: Database.Database) {
// setup tiers table
db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INTEGER, enchantment INTEGER, PRIMARY KEY(tier, enchantment));");
db.exec(`INSERT OR IGNORE INTO tiers (tier, enchantment) VALUES (1,0), (2,0), (3,0),
(4,0), (4,1), (4,2), (4,3), (4,4),
(5,0), (5,1), (5,2), (5,3), (5,4),
(6,0), (6,1), (6,2), (6,3), (6,4),
(7,0), (7,1), (7,2), (7,3), (7,4),
(8,0), (8,1), (8,2), (8,3), (8,4)`
);
// setup item types table
db.exec("CREATE TABLE IF NOT EXISTS types (type TEXT PRIMARY KEY);");
db.exec("INSERT OR IGNORE INTO types (type) VALUES ('cloth'), ('leather'), ('bar'), ('block'), ('plank'), ('fee_crafting'), ('fee_market');");
// create transactions table
db.exec("CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER DEFAULT (unixepoch()), tier INTEGER, enchantment INTEGER, type TEXT, silver INT, CONSTRAINT fk_tier FOREIGN KEY (tier, enchantment) REFERENCES tiers(tier, enchantment), CONSTRAINT fk_type FOREIGN KEY (type) REFERENCES types(type));");
}
function insertTransaction(db: Database.Database, tier: number, enchantment: number, type: string, silver: number): boolean {
console.log("insertTransaction", tier, enchantment, type, silver);
return db.prepare("INSERT INTO transactions (tier, enchantment, type, silver) VALUES (?, ?, ?, ?);").run(tier, enchantment, type, silver).changes > 0;
}
export { getDB, prepareDB, insertTransaction };

View File

@@ -1,10 +1,10 @@
import express from 'express';
import prepareDB from './prepare';
import getDB from './database';
import { getDB, insertTransaction, prepareDB } from './database';
import { tiers, type, transaction } from './types';
const app = express();
app.get("/tiers", (_, res) => {
const db = getDB();
res.header("Access-Control-Allow-Origin", process.env.CORS_HEADER);
@@ -24,7 +24,44 @@ app.get("/transactions", (_, res) => {
res.header("Access-Control-Allow-Origin", process.env.CORS_HEADER);
res.send((db.prepare("SELECT * FROM transactions").all() as transaction[]));
db.close();
})
});
app.options("/transactions", (_, res) => {
res.header("Access-Control-Allow-Origin", process.env.CORS_HEADER);
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.send();
});
app.post("/transactions", express.json(), (req, res) => {
res.header("Access-Control-Allow-Origin", process.env.CORS_HEADER);
console.log(req.body);
const { tier, enchantment, type, silver } = req.body;
if (typeof tier !== "number" || tier < 1 || tier > 8) {
res.status(400).send("tier invalid");
return;
}
if (typeof enchantment !== "number" || enchantment < 0 || enchantment > 4) {
res.status(400).send("enchantment invalid");
return;
}
if (typeof type !== "string") {
res.status(400).send("type invalid");
return;
}
if (typeof silver !== "number") {
res.status(400).send("cost invalid");
return;
}
const db = getDB();
if (insertTransaction(db, tier, enchantment, type, silver)) {
res.status(200).send("OK");
return;
} else {
res.status(500).send("Error");
return;
}
});
app.listen(2500, () => {
const db = getDB();

View File

@@ -1,20 +0,0 @@
import Database from "better-sqlite3";
export default function prepareDB(db: Database.Database) {
// setup tiers table
db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INTEGER, enchantment INTEGER, PRIMARY KEY(tier, enchantment));");
db.exec(`INSERT OR IGNORE INTO tiers (tier, enchantment) VALUES (1,0), (2,0), (3,0),
(4,0), (4,1), (4,2), (4,3), (4,4),
(5,0), (5,1), (5,2), (5,3), (5,4),
(6,0), (6,1), (6,2), (6,3), (6,4),
(7,0), (7,1), (7,2), (7,3), (7,4),
(8,0), (8,1), (8,2), (8,3), (8,4)`
);
// setup item types table
db.exec("CREATE TABLE IF NOT EXISTS types (type TEXT PRIMARY KEY);");
db.exec("INSERT OR IGNORE INTO types (type) VALUES ('cloth'), ('leather'), ('bar'), ('block'), ('plank'), ('fee_crafting'), ('fee_market');");
// create transactions table
db.exec("CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER DEFAULT (unixepoch()), tier INT, type TEXT, cost INT, CONSTRAINT fk_tier FOREIGN KEY (tier) REFERENCES tiers(tier), CONSTRAINT fk_type FOREIGN KEY (type) REFERENCES types(type));");
}