From a01d2f5df195e9dfb53480082df04e9bf5a55103 Mon Sep 17 00:00:00 2001 From: Son Nguyen Date: Tue, 19 Dec 2023 22:21:46 +0700 Subject: [PATCH] Updated backend logic --- database/movie-user.sql | 102 +++++++++++++++++++++++ database/movieverse_nosql_setup.js | 125 +++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 database/movie-user.sql create mode 100644 database/movieverse_nosql_setup.js diff --git a/database/movie-user.sql b/database/movie-user.sql new file mode 100644 index 00000000..2e2cec2e --- /dev/null +++ b/database/movie-user.sql @@ -0,0 +1,102 @@ +-- Create a database (Uncomment if needed) +CREATE DATABASE MovieVerseDB; + +-- Switch to the MovieVerseDB database (Uncomment if needed) +\c MovieVerseDB + +-- Create table for storing movie details +CREATE TABLE movies ( + movie_id SERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL, + release_date DATE, + genre VARCHAR(100), + director VARCHAR(255), + description TEXT, + duration INT, + language VARCHAR(50), + country VARCHAR(100), + poster_url VARCHAR(255) +); + +-- Create table for storing movie cast +CREATE TABLE cast ( + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + actor VARCHAR(255), + role VARCHAR(255), + PRIMARY KEY (movie_id, actor) +); + +-- Create table for storing movie crew +CREATE TABLE crew ( + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + crew_member VARCHAR(255), + role VARCHAR(255), + PRIMARY KEY (movie_id, crew_member) +); + +-- Create table for storing movie trailers +CREATE TABLE trailers ( + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + trailer_url VARCHAR(255), + PRIMARY KEY (movie_id, trailer_url) +); + +-- Create table for storing user information +CREATE TABLE users ( + user_id SERIAL PRIMARY KEY, + username VARCHAR(50) UNIQUE NOT NULL, + password_hash VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + join_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + is_admin BOOLEAN DEFAULT FALSE +); + +-- Create table for storing reviews +CREATE TABLE reviews ( + review_id SERIAL PRIMARY KEY, + user_id INT REFERENCES users(user_id) ON DELETE CASCADE, + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + review_text TEXT, + review_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + rating DECIMAL(2, 1) CHECK (rating >= 0 AND rating <= 10) +); + +-- Create table for storing ratings +CREATE TABLE ratings ( + user_id INT REFERENCES users(user_id) ON DELETE CASCADE, + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + rating DECIMAL(2, 1) NOT NULL CHECK (rating >= 0 AND rating <= 10), + rating_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, movie_id) +); + +-- Create table for storing movie favorites +CREATE TABLE favorites ( + user_id INT REFERENCES users(user_id) ON DELETE CASCADE, + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + favorited_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, movie_id) +); + +-- Create table for storing movie watchlist +CREATE TABLE watchlist ( + user_id INT REFERENCES users(user_id) ON DELETE CASCADE, + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, movie_id) +); + +-- Create table for storing movie watch history +CREATE TABLE watch_history ( + user_id INT REFERENCES users(user_id) ON DELETE CASCADE, + movie_id INT REFERENCES movies(movie_id) ON DELETE CASCADE, + watched_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (user_id, movie_id) +); + +-- Indexes for performance optimization +CREATE INDEX idx_movies_title ON movies(title); +CREATE INDEX idx_reviews_user_id ON reviews(user_id); +CREATE INDEX idx_reviews_movie_id ON reviews(movie_id); +CREATE INDEX idx_ratings_user_id ON ratings(user_id); +CREATE INDEX idx_ratings_movie_id ON ratings(movie_id); diff --git a/database/movieverse_nosql_setup.js b/database/movieverse_nosql_setup.js new file mode 100644 index 00000000..5ba5a113 --- /dev/null +++ b/database/movieverse_nosql_setup.js @@ -0,0 +1,125 @@ +// Connecting to the MongoDB instance +db = connect("localhost:27017/MovieVerseDB"); + +// Dropping existing collections if they exist (for clean setup) +db.userPreferences.drop(); +db.sessionData.drop(); +db.appLogs.drop(); + +// Creating collections with their respective schemas +// Collection for User Preferences +db.createCollection("userPreferences", { + validator: { + $jsonSchema: { + bsonType: "object", + required: ["userId", "preferences"], + properties: { + userId: { + bsonType: "int", + description: "User identifier" + }, + preferences: { + bsonType: "object", + properties: { + genres: { + bsonType: "array", + items: { + bsonType: "string", + description: "Preferred genres" + } + }, + directors: { + bsonType: "array", + items: { + bsonType: "string", + description: "Preferred directors" + } + }, + actors: { + bsonType: "array", + items: { + bsonType: "string", + description: "Favorite actors" + } + } + // Additional preferences can be added here + } + } + } + } + } +}); + +// Collection for Session Data +db.createCollection("sessionData", { + validator: { + $jsonSchema: { + bsonType: "object", + required: ["sessionId", "userId", "startTime"], + properties: { + sessionId: { + bsonType: "string", + description: "Unique session identifier" + }, + userId: { + bsonType: "int", + description: "User identifier for the session" + }, + startTime: { + bsonType: "date", + description: "Session start time" + }, + activities: { + bsonType: "array", + items: { + bsonType: "object", + properties: { + timestamp: { + bsonType: "date", + description: "Activity timestamp" + }, + activity: { + bsonType: "string", + description: "Description of user activity" + } + } + } + } + } + } + } +}); + +// Collection for Application Logs +db.createCollection("appLogs", { + validator: { + $jsonSchema: { + bsonType: "object", + required: ["timestamp", "logLevel", "message"], + properties: { + timestamp: { + bsonType: "date", + description: "Log timestamp" + }, + logLevel: { + bsonType: "string", + enum: ["INFO", "WARN", "ERROR", "DEBUG"], + description: "Log level" + }, + message: { + bsonType: "string", + description: "Log message" + }, + details: { + bsonType: "object", + description: "Additional details about the log, if any" + } + } + } + } +}); + +// Indexes for optimization +db.userPreferences.createIndex({ userId: 1 }); +db.sessionData.createIndex({ sessionId: 1 }); +db.appLogs.createIndex({ timestamp: -1 });