Skip to content

Commit

Permalink
initial commit for the backend. Database should be ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
kek20009 committed Nov 4, 2024
1 parent 8a5ded1 commit 7b2d944
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SQLite database
/Backend/testData.db
*.db

#Log files for some reason lol
*.log
logs/
30 changes: 30 additions & 0 deletions Backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use Python as the base image
FROM python:3.8-slim-buster

# Install pip and Python dependencies
RUN apt-get update && apt-get install -y python3 python3-pip

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1


# Set the working directory in the container
WORKDIR /app


# Install pip requirements
COPY /requirements.txt .
RUN python3 -m pip install -r requirements.txt

# Copy main application file
COPY generateData.py .

# Set the port for the application
ENV PORT=5000
EXPOSE 5000

# Command to run the application
CMD ["python3", "generateData.py"]
44 changes: 44 additions & 0 deletions Backend/everythingData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from contextlib import closing
import sqlite3

def connect_db():
"""Establish a connection to the SQLite database."""
return sqlite3.connect('/home/sus98/2102/proj/cse2102-fall24-Team51/Backend/testData.db') #this might change

def insert_user(name, age, marital_status, pet_preference):
"""Insert a new user into the users table."""
with closing(connect_db()) as conn, conn:
cursor = conn.cursor()
cursor.execute(
'''INSERT INTO users (name, age, marital_status, pet_preference)
VALUES (?, ?, ?, ?)''',
(name, age, marital_status, pet_preference)
)
conn.commit()
return cursor.lastrowid

def insert_pet(name, age, gender, breed, type, location, picture=None):
"""Insert a new pet into the pets table."""
with closing(connect_db()) as conn, conn:
cursor = conn.cursor()
cursor.execute(
'''INSERT INTO pets (name, age, gender, breed, type, location, picture)
VALUES (?, ?, ?, ?, ?, ?, ?)''',
(name, age, gender, breed, type, location, picture)
)
conn.commit()
return cursor.lastrowid

def fetch_all_pets():
"""Retrieve all pets from the pets table."""
with closing(connect_db()) as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM pets")
return cursor.fetchall()

def fetch_user_by_id(user_id):
"""Retrieve a user by their user_id."""
with closing(connect_db()) as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE user_id = ?", (user_id,))
return cursor.fetchone()
77 changes: 77 additions & 0 deletions Backend/generateData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'''ONLY USE THIS IF THE DATABASE IS EMPTY. IT'S THE INIT CODE'''

import sqlite3
from contextlib import closing
import os



def populate_test_data(db_path):
'''THE ACTUAL FUNCITON'''

conn = sqlite3.connect(db_path)
cursor = conn.cursor()



# Create tables
cursor.executescript('''
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
marital_status TEXT,
pet_preference TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS pets (
pet_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
gender TEXT CHECK(gender IN ('male', 'female')),
breed TEXT,
type TEXT CHECK(type IN ('dog', 'cat')),
location TEXT,
photo_path TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS user_tests (
test_id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
question TEXT,
answer TEXT,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
''') #ur welcome :)


# Insert sample users
cursor.executescript('''
INSERT INTO users (name, age, marital_status, pet_preference) VALUES
('Ben Ten', 28, 'single', 'dog'),
('Spongebob Smith', 35, 'married', 'cat'),
('Dipper Pines', 25, 'married', 'any'),
('Emilia Kirej', 20, 'married', 'dog');
''')

# Insert sample pets
cursor.executescript('''
INSERT INTO pets (name, age, gender, breed, type, location, photo_path) VALUES
('Gunner', 9, 'male', 'Golden Retriever', 'dog', 'Myrtle Beach, SC', 'images/gunner.jpg'),
('Lizzie Izzie', 5, 'female', 'Donskoy', 'cat', 'Milford, CT', 'images/lizzie_izzie.jpg'),
('Chuck', 1, 'male', 'Corgi', 'dog', 'Orlando, FL', 'images/chuck.jpg'),
('Monster', 5, 'female', 'Maine Coon', 'cat', 'Houston, TX', 'images/monster.jpg');
''')

conn.commit()
conn.close()




#populate_test_data('/home/sus98/2102/proj/cse2102-fall24-Team51/Backend/testData.db')


Binary file added Backend/images/Chuck.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Backend/images/gunner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Backend/images/lizzie_izzie.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Backend/images/monster.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flask==2.0.3
requests==2.26.0
pandas==1.3.4
pytest==7.2.1
flask_cors==3.0.10
#Werkzeug==2.1.3
#pip freeze

0 comments on commit 7b2d944

Please sign in to comment.