tables scripts
This commit is contained in:
43
scripts/create_artwork_vote_counts_view.js
Normal file
43
scripts/create_artwork_vote_counts_view.js
Normal file
@@ -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();
|
||||||
37
scripts/create_artworks_table.js
Normal file
37
scripts/create_artworks_table.js
Normal file
@@ -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();
|
||||||
@@ -4,18 +4,13 @@ require('dotenv').config();
|
|||||||
const createTableQuery = `
|
const createTableQuery = `
|
||||||
CREATE TABLE IF NOT EXISTS participants (
|
CREATE TABLE IF NOT EXISTS participants (
|
||||||
id SERIAL PRIMARY KEY,
|
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,
|
participant_name VARCHAR(255) NOT NULL,
|
||||||
email VARCHAR(255) NOT NULL,
|
year INT CHECK (year BETWEEN 1 AND 4),
|
||||||
year INTEGER NOT NULL,
|
department VARCHAR(255),
|
||||||
phone_number VARCHAR(15),
|
phone_number VARCHAR(15),
|
||||||
is_digital_art BOOLEAN DEFAULT false,
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
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
|
|
||||||
);
|
);
|
||||||
`;
|
`;
|
||||||
|
|
||||||
@@ -6,7 +6,8 @@ CREATE TABLE IF NOT EXISTS users (
|
|||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
email VARCHAR(255) UNIQUE NOT NULL,
|
email VARCHAR(255) UNIQUE NOT NULL,
|
||||||
name VARCHAR(255),
|
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()
|
||||||
);
|
);
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
35
scripts/create_votes_table.js
Normal file
35
scripts/create_votes_table.js
Normal file
@@ -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();
|
||||||
Reference in New Issue
Block a user