Refactor database schema; add hunts and team_clues tables, update clues structure, and improve error handling in routes

This commit is contained in:
bobbert committed 2025-03-12 16:16:42 +00:00
1 parent 4f7fdc6ad2
commit 77e83dc21c
13 files changed
+136 -111

No files matched your search

+42 -44
View File
@@ -1,64 +1,62 @@
-- Schema for clue_hunt.db
-- Drop tables if they exist
DROP TABLE IF EXISTS team_clues;
DROP TABLE IF EXISTS clues;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS teams;
DROP TABLE IF EXISTS game_settings;
DROP TABLE IF EXISTS hunts;
-- Users table
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
team_id INTEGER, -- Ensure this column exists
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id)
);
-- Teams table
-- Create teams table
CREATE TABLE teams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
code TEXT UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
code TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Clues table
-- Create users table
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT DEFAULT 'user',
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE SET NULL
);
-- Create clues table
CREATE TABLE clues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
team_id INTEGER NOT NULL,
found BOOLEAN DEFAULT FALSE,
found_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (team_id) REFERENCES teams(id)
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT NOT NULL,
qr_code TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Game settings table
CREATE TABLE game_settings (
id INTEGER PRIMARY KEY,
hunt_active BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
-- Create team_clues table (for tracking which teams have found which clues)
CREATE TABLE team_clues (
team_id INTEGER NOT NULL,
clue_id INTEGER NOT NULL,
found_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (team_id, clue_id),
FOREIGN KEY (team_id) REFERENCES teams(id) ON DELETE CASCADE,
FOREIGN KEY (clue_id) REFERENCES clues(id) ON DELETE CASCADE
);
-- Execute this in your database:
CREATE TABLE IF NOT EXISTS hunts (
id INT AUTO_INCREMENT PRIMARY KEY,
active BOOLEAN DEFAULT FALSE,
-- Create hunts table
CREATE TABLE hunts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
active BOOLEAN DEFAULT 0,
start_time DATETIME DEFAULT NULL,
end_time DATETIME DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert a default hunt row if none exists:
INSERT INTO hunts (active)
SELECT FALSE
WHERE NOT EXISTS (SELECT 1 FROM hunts LIMIT 1);
-- Insert a default hunt row
INSERT INTO hunts (active) SELECT 0 WHERE NOT EXISTS (SELECT 1 FROM hunts LIMIT 1);
-- Insert initial admin user
INSERT INTO users (username, password, role)
@@ -70,8 +68,8 @@ INSERT INTO teams (name, code) VALUES ('Team Alpha', 'ALPHA123');
INSERT INTO teams (name, code) VALUES ('Team Beta', 'BETA456');
INSERT INTO teams (name, code) VALUES ('Team Gamma', 'GAMMA789');
-- Insert sample clues for Team Alpha
INSERT INTO clues (description, team_id) VALUES ('Find the red door', 1);
INSERT INTO clues (description, team_id) VALUES ('Look under the bridge', 1);
INSERT INTO clues (description, team_id) VALUES ('Check the library', 1);
-- Insert sample clues
INSERT INTO clues (title, description, location, qr_code) VALUES ('Clue 1', 'Find the red door', 'Location 1', 'QR1');
INSERT INTO clues (title, description, location, qr_code) VALUES ('Clue 2', 'Look under the bridge', 'Location 2', 'QR2');
INSERT INTO clues (title, description, location, qr_code) VALUES ('Clue 3', 'Check the library', 'Location 3', 'QR3');