Skip to content

Commit

Permalink
Updated backend logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Nguyen committed Dec 19, 2023
1 parent 1ba8100 commit a01d2f5
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
102 changes: 102 additions & 0 deletions database/movie-user.sql
Original file line number Diff line number Diff line change
@@ -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);
125 changes: 125 additions & 0 deletions database/movieverse_nosql_setup.js
Original file line number Diff line number Diff line change
@@ -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 });

0 comments on commit a01d2f5

Please sign in to comment.