redo of entire app

This commit is contained in:
bobbert committed 2025-03-11 16:22:47 +00:00
1 parent 7dee4879a7
commit 093dd7ea9e
45 files changed
+686 -5659

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import bcrypt from 'bcryptjs';
async function initDb() {
const db = await open({
filename: './clue_hunt.db',
driver: sqlite3.Database,
});
// Create tables
await db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user'
);
CREATE TABLE IF NOT EXISTS teams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
code TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS clues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
found BOOLEAN NOT NULL DEFAULT FALSE,
skipped BOOLEAN NOT NULL DEFAULT FALSE,
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES teams(id)
);
CREATE TABLE IF NOT EXISTS progress (
id INTEGER PRIMARY KEY AUTOINCREMENT,
team_id INTEGER,
clue_id INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id),
FOREIGN KEY (clue_id) REFERENCES clues(id)
);
`);
// Seed initial data
const hashedPassword = bcrypt.hashSync('admin', 10);
await db.run(
'INSERT OR IGNORE INTO users (username, password, role) VALUES (?, ?, ?)',
['admin', hashedPassword, 'admin']
);
await db.run(
'INSERT OR IGNORE INTO teams (name, code) VALUES (?, ?)',
['Team Alpha', 'ALPHA123']
);
console.log('Database initialized successfully.');
await db.close();
}
initDb().catch((err) => console.error('Error initializing database:', err));
+18
View File
@@ -0,0 +1,18 @@
const { query } = require('../lib/db');
const bcrypt = require('bcryptjs');
async function seedDb() {
try {
const hashedPassword = bcrypt.hashSync('admin', 10);
await query(
'INSERT OR IGNORE INTO users (username, password, role) VALUES (?, ?, ?)',
['admin', hashedPassword, 'admin']
);
console.log('Database seeded successfully.');
} catch (err) {
console.error('Error seeding database:', err);
}
}
seedDb();
+12
View File
@@ -0,0 +1,12 @@
const { query } = require('../lib/db');
async function testDb() {
try {
const rows = await query('SELECT 1 + 1 AS result');
console.log('Database test successful:', rows);
} catch (err) {
console.error('Database test failed:', err);
}
}
testDb();
+16
View File
@@ -0,0 +1,16 @@
const Database = require('better-sqlite3');
// Use an in-memory database for testing
const db = new Database(':memory:');
// Create a table
db.exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
// Insert data
const insert = db.prepare('INSERT INTO test (name) VALUES (?)');
insert.run('Alice');
insert.run('Bob');
// Query data
const rows = db.prepare('SELECT * FROM test').all();
console.log(rows); // Should output: [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ]