mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
redo of entire app
This commit is contained in:
1 parent
7dee4879a7
commit
093dd7ea9e
45 files changed
+686
-5659
No files matched your search
@@ -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));
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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' } ]
|
||||
Reference in new issue
Block a user