Skip to content

Created db folder, db-scripts folder, database.py, and subfiles withi… #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ GradeSync is a backend microservice that integrates with assessment platforms to
- Click "New Request" to test the API endpoints.
- Also, create test cases in `api/test_app.py`.
7. When you are finished, run `docker-compose down` or press CTRL+C to stop the server.


### Configuring the database
1. Install postgres through `brew update` then `brew install postgresql`
2. Start and enable the PostgreSQL Service through running `brew services start postgresql`
3. Set up the PostgreSQL User and Database with `sudo -i -u postgres` and then starting the PostgreSQL prompt with `psql`
23 changes: 23 additions & 0 deletions api/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# src/database.py

import os
import psycopg2
from dotenv import load_dotenv

# Load environment variables from config.env
load_dotenv(dotenv_path='../db/config.env')

def connect_db():
try:
connection = psycopg2.connect(
dbname=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD"),
host=os.getenv("DB_HOST"),
port=os.getenv("DB_PORT")
)
print("Database connection successful")
return connection
except Exception as e:
print(f"Error connecting to database: {e}")
return None
2 changes: 2 additions & 0 deletions api/db-scripts/db_cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This needs to be fixed and updated
0 0 * * * /usr/bin/python3 /path/to/your/project/src/load_gradescope_data.py
63 changes: 63 additions & 0 deletions api/db-scripts/gradescope_to_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# src/load_gradescope_data.py

# Heavily update this because it uses the wrong database schema for now
# This is placeholder code for the future

import json
import psycopg2
from src.database import connect_db

def load_gradescope_data(json_file_path):
# Open the JSON file from Gradescope
with open(json_file_path, 'r') as file:
data = json.load(file)

conn = connect_db()
if conn is None:
print("Failed to connect to the database.")
return

try:
with conn.cursor() as cursor:
# Insert or update students
for student in data["students"]:
cursor.execute("""
INSERT INTO students (name, email)
VALUES (%s, %s)
ON CONFLICT (email) DO NOTHING
""", (student["name"], student["email"]))

# Insert assignments and grades
for assignment in data["assignments"]:
cursor.execute("""
INSERT INTO assignments (assignment_name, due_date)
VALUES (%s, %s)
ON CONFLICT (assignment_name) DO NOTHING
RETURNING assignment_id
""", (assignment["name"], assignment["due_date"]))
assignment_id = cursor.fetchone()[0]

for grade_entry in assignment["grades"]:
cursor.execute("""
INSERT INTO grades (student_id, assignment_id, grade, submission_date)
VALUES (
(SELECT student_id FROM students WHERE email = %s),
%s, %s, %s
)
ON CONFLICT (student_id, assignment_id) DO UPDATE SET
grade = EXCLUDED.grade,
submission_date = EXCLUDED.submission_date
""", (grade_entry["student_email"], assignment_id, grade_entry["grade"], grade_entry["submission_date"]))

conn.commit()
print("Gradescope data loaded successfully.")
except Exception as e:
print(f"Error loading Gradescope data: {e}")
conn.rollback()
finally:
conn.close()

if __name__ == "__main__":
# Path to your Gradescope JSON file
json_file_path = "../data/gradescope_data.json"
load_gradescope_data(json_file_path)
25 changes: 25 additions & 0 deletions api/db-scripts/init_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# scripts/init_database.py

import psycopg2
from src.database import connect_db

def initialize_db():
conn = connect_db()
if conn is None:
print("Failed to connect to the database.")
return

try:
with conn.cursor() as cursor:
with open("db/init_db.sql", "r") as sql_file:
cursor.execute(sql_file.read())
conn.commit()
print("Database initialized successfully.")
except Exception as e:
print(f"Error initializing database: {e}")
conn.rollback()
finally:
conn.close()

if __name__ == "__main__":
initialize_db()
1 change: 1 addition & 0 deletions api/db-scripts/pl_to_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Placeholder for next file to implement
5 changes: 5 additions & 0 deletions api/db/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_NAME=my_database
DB_USER=my_user
DB_PASSWORD=my_password
DB_HOST=localhost
DB_PORT=5432
24 changes: 24 additions & 0 deletions api/db/init_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Sample code to create a table in the database
-- We will be copying and pasting in the database schema from mermaid here

-- This is wrong below this line but sample database schema

CREATE TABLE IF NOT EXISTS students (
student_id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);

CREATE TABLE IF NOT EXISTS assignments (
assignment_id SERIAL PRIMARY KEY,
assignment_name VARCHAR(100),
due_date DATE
);

CREATE TABLE IF NOT EXISTS grades (
grade_id SERIAL PRIMARY KEY,
student_id INT REFERENCES students(student_id),
assignment_id INT REFERENCES assignments(assignment_id),
grade FLOAT,
submission_date TIMESTAMP
);
1 change: 1 addition & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ httpx==0.27.0
gspread
google-auth
pydantic==2.9.2
psycopg2