Compare commits
2 Commits
e3c7f3c18f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
dd8e3e9449
|
|||
|
314955efcf
|
@@ -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 };
|
||||
44
src/main.ts
44
src/main.ts
@@ -1,9 +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);
|
||||
@@ -23,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();
|
||||
|
||||
@@ -1,22 +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 INT PRIMARY KEY);");
|
||||
for (let i = 1; i <= 8; i++) {
|
||||
if (!db.prepare(`SELECT tier FROM tiers WHERE tier = ${i};`).pluck().get()) {
|
||||
db.prepare("INSERT INTO tiers VALUES (?)").run(i);
|
||||
}
|
||||
}
|
||||
|
||||
// setup item types table
|
||||
db.exec("CREATE TABLE IF NOT EXISTS types (type VARCHAR PRIMARY KEY);")
|
||||
for (const type of ["cloth", "hide", "fee_crafting", "fee_market"]) {
|
||||
if(!db.prepare(`SELECT type FROM types WHERE type = '${type}'`).pluck().get()) {
|
||||
db.prepare("INSERT INTO types VALUES (?)").run(type);
|
||||
}
|
||||
}
|
||||
|
||||
// create transactions table
|
||||
db.exec("CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER DEFAULT (unixepoch()), tier INT, type VARCHAR, cost INT, CONSTRAINT fk_tier FOREIGN KEY (tier) REFERENCES tiers(tier), CONSTRAINT fk_type FOREIGN KEY (type) REFERENCES types(type));");
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
type type = { type: string };
|
||||
type tiers = { tier: number };
|
||||
type transaction = { id: number; tier: number; type: string; cost: number };
|
||||
|
||||
export type { type, tiers, transaction };
|
||||
Reference in New Issue
Block a user