initial commit for GitHub

This commit is contained in:
Lucas Jensen
2024-12-01 19:15:25 -08:00
commit 925b334e4c
91 changed files with 8031 additions and 0 deletions

64
server/app/db/DDL.sql Normal file
View File

@@ -0,0 +1,64 @@
-- Auto generated by DBeaver
-- meganjohns.articles definition
CREATE TABLE `articles` (
`article_id` int(11) NOT NULL AUTO_INCREMENT,
`article_title` varchar(255) NOT NULL,
`body` varchar(255) NOT NULL,
`video_url` varchar(255) DEFAULT NULL,
`is_featured` tinyint(1) DEFAULT 0,
PRIMARY KEY (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- meganjohns.artists definition
CREATE TABLE `artists` (
`artist_id` int(11) NOT NULL AUTO_INCREMENT,
`artist_name` varchar(255) NOT NULL,
`artist_url` varchar(255) NOT NULL,
PRIMARY KEY (`artist_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- meganjohns.medium definition
CREATE TABLE `medium` (
`medium_id` int(11) NOT NULL AUTO_INCREMENT,
`medium_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`medium_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- meganjohns.albums definition
CREATE TABLE `albums` (
`album_id` int(11) NOT NULL AUTO_INCREMENT,
`album_name` varchar(255) NOT NULL,
`year` int(11) NOT NULL,
`artist_id` int(11) NOT NULL,
`spotify_url` varchar(255) DEFAULT NULL,
`itunes_url` varchar(255) DEFAULT NULL,
`bandcamp_url` varchar(255) DEFAULT NULL,
`apple_music_url` varchar(255) DEFAULT NULL,
PRIMARY KEY (`album_id`),
KEY `artist_id` (`artist_id`),
CONSTRAINT `albums_ibfk_1` FOREIGN KEY (`artist_id`) REFERENCES `artists` (`artist_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- meganjohns.artwork definition
CREATE TABLE `artwork` (
`artwork_id` int(11) NOT NULL AUTO_INCREMENT,
`medium_id` int(11) NOT NULL,
`artwork_name` varchar(255) NOT NULL,
`source_url` varchar(255) NOT NULL,
`year` int(11) NOT NULL,
`size` varchar(255) DEFAULT NULL,
PRIMARY KEY (`artwork_id`),
UNIQUE KEY `artwork_name` (`artwork_name`),
KEY `medium_id` (`medium_id`),
CONSTRAINT `artwork_ibfk_1` FOREIGN KEY (`medium_id`) REFERENCES `medium` (`medium_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

View File

37
server/app/db/conn.py Normal file
View File

@@ -0,0 +1,37 @@
import os
import mysql.connector
from dotenv import load_dotenv
class DBException(Exception):
pass
def connect_db() -> mysql.connector.MySQLConnection:
"""
Connects to the MySQL database using credentials from the .env file.
Returns a MySQLConnection object which can be used by the database query layer.
Credential values are validated and an exception is raised if any are missing.
"""
load_dotenv()
host = os.getenv("DB_HOST")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
database = os.getenv("DB_DATABASE")
if None in [host, user, password, database]:
raise DBException("Missing database credentials")
try:
return mysql.connector.connect(
host=host,
user=user,
password=password,
database=database,
auth_plugin="mysql_native_password",
) # type: ignore
except mysql.connector.Error as err:
raise DBException("Could not connect to database") from err