diff --git a/scripts/create_artwork_vote_counts_view.js b/scripts/create_artwork_vote_counts_view.js new file mode 100644 index 0000000..be0d4b6 --- /dev/null +++ b/scripts/create_artwork_vote_counts_view.js @@ -0,0 +1,43 @@ +const { Pool } = require('pg'); +require('dotenv').config(); + +const createViewQuery = ` +CREATE OR REPLACE VIEW artwork_vote_counts AS +SELECT + a.id AS artwork_id, + a.title, + a.category, + a.image_url, + p.participant_name, + p.department, + p.year, + COUNT(v.id) AS vote_count +FROM artworks a +JOIN participants p ON a.participant_id = p.id +LEFT JOIN votes v ON a.id = v.artwork_id +GROUP BY a.id, a.title, a.category, a.image_url, p.participant_name, p.department, p.year +ORDER BY vote_count DESC; +`; + +async function createView() { + const pool = new Pool({ + connectionString: process.env.DATABASE_URL, + }); + + try { + console.log('Connecting to the database...'); + const client = await pool.connect(); + + console.log('Creating or replacing artwork_vote_counts view...'); + await client.query(createViewQuery); + + console.log('artwork_vote_counts view created or replaced successfully.'); + client.release(); + } catch (error) { + console.error('Error creating artwork_vote_counts view:', error); + } finally { + await pool.end(); + } +} + +createView(); \ No newline at end of file diff --git a/scripts/create_artworks_table.js b/scripts/create_artworks_table.js new file mode 100644 index 0000000..2ef57d0 --- /dev/null +++ b/scripts/create_artworks_table.js @@ -0,0 +1,37 @@ +const { Pool } = require('pg'); +require('dotenv').config(); + +const createTableQuery = ` +CREATE TABLE IF NOT EXISTS artworks ( + id SERIAL PRIMARY KEY, + participant_id INT NOT NULL REFERENCES participants(id) ON DELETE CASCADE, + category VARCHAR(50) NOT NULL CHECK (category IN ('digital', 'handdrawn', 'poster')), + image_url TEXT NOT NULL, + title VARCHAR(255) NOT NULL, + created_at TIMESTAMP DEFAULT NOW(), + CONSTRAINT unique_participant_category UNIQUE (participant_id, category) +); +`; + +async function createTable() { + const pool = new Pool({ + connectionString: process.env.DATABASE_URL, + }); + + try { + console.log('Connecting to the database...'); + const client = await pool.connect(); + + console.log('Creating artworks table if it does not exist...'); + await client.query(createTableQuery); + + console.log('Artworks table created or already exists.'); + client.release(); + } catch (error) { + console.error('Error creating artworks table:', error); + } finally { + await pool.end(); + } +} + +createTable(); \ No newline at end of file diff --git a/scripts/create_participant_table.js b/scripts/create_participants_table.js similarity index 65% rename from scripts/create_participant_table.js rename to scripts/create_participants_table.js index 2702f59..d140dfb 100644 --- a/scripts/create_participant_table.js +++ b/scripts/create_participants_table.js @@ -4,18 +4,13 @@ require('dotenv').config(); const createTableQuery = ` CREATE TABLE IF NOT EXISTS participants ( id SERIAL PRIMARY KEY, - user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + user_id INT UNIQUE REFERENCES users(id) ON DELETE SET NULL, + email VARCHAR(255) UNIQUE NOT NULL, participant_name VARCHAR(255) NOT NULL, - email VARCHAR(255) NOT NULL, - year INTEGER NOT NULL, + year INT CHECK (year BETWEEN 1 AND 4), + department VARCHAR(255), phone_number VARCHAR(15), - is_digital_art BOOLEAN DEFAULT false, - digital_art_link TEXT DEFAULT NULL, - is_handdrawn BOOLEAN DEFAULT false, - handdrawn_link TEXT DEFAULT NULL, - is_poster BOOLEAN DEFAULT false, - poster_link TEXT DEFAULT NULL, - CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + created_at TIMESTAMP DEFAULT NOW() ); `; diff --git a/scripts/create_users_table.js b/scripts/create_users_table.js index 77fb143..163ba3c 100644 --- a/scripts/create_users_table.js +++ b/scripts/create_users_table.js @@ -6,7 +6,8 @@ CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, name VARCHAR(255), - user_type VARCHAR(50) DEFAULT 'audience' NOT NULL + user_type VARCHAR(50) NOT NULL DEFAULT 'audience' CHECK (user_type IN ('audience', 'participant', 'admin', 'moderator')), + created_at TIMESTAMP DEFAULT NOW() ); `; diff --git a/scripts/create_votes_table.js b/scripts/create_votes_table.js new file mode 100644 index 0000000..fe61d4e --- /dev/null +++ b/scripts/create_votes_table.js @@ -0,0 +1,35 @@ +const { Pool } = require('pg'); +require('dotenv').config(); + +const createTableQuery = ` +CREATE TABLE IF NOT EXISTS votes ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + artwork_id INT NOT NULL REFERENCES artworks(id) ON DELETE CASCADE, + created_at TIMESTAMP DEFAULT NOW(), + CONSTRAINT unique_user_artwork_vote UNIQUE (user_id, artwork_id) +); +`; + +async function createTable() { + const pool = new Pool({ + connectionString: process.env.DATABASE_URL, + }); + + try { + console.log('Connecting to the database...'); + const client = await pool.connect(); + + console.log('Creating votes table if it does not exist...'); + await client.query(createTableQuery); + + console.log('Votes table created or already exists.'); + client.release(); + } catch (error) { + console.error('Error creating votes table:', error); + } finally { + await pool.end(); + } +} + +createTable(); \ No newline at end of file