From 989cb6363dc07a51163bc0ddf4dcc518de78cb6e Mon Sep 17 00:00:00 2001 From: Nihal Menon <=> Date: Sat, 7 Oct 2023 22:08:25 -0400 Subject: [PATCH] deleted excess functions/files --- server/db/env-vars.py | 8 ----- server/db/talk-to-db.py | 48 ------------------------------ server/db/tutorial/db.py | 45 ---------------------------- server/db/tutorial/main.py | 23 --------------- server/sql-functions.py | 60 -------------------------------------- 5 files changed, 184 deletions(-) delete mode 100644 server/db/env-vars.py delete mode 100644 server/db/talk-to-db.py delete mode 100644 server/db/tutorial/db.py delete mode 100644 server/db/tutorial/main.py delete mode 100644 server/sql-functions.py diff --git a/server/db/env-vars.py b/server/db/env-vars.py deleted file mode 100644 index 570dfdd..0000000 --- a/server/db/env-vars.py +++ /dev/null @@ -1,8 +0,0 @@ -import os -from dotenv import find_dotenv, load_dotenv - -dotenv_path = find_dotenv() -load_dotenv(dotenv_path) - -CLOUD_SQL_USERNAME = os.getenv("CLOUD_SQL_USERNAME", default=None) -print(CLOUD_SQL_USERNAME) \ No newline at end of file diff --git a/server/db/talk-to-db.py b/server/db/talk-to-db.py deleted file mode 100644 index 02beaf1..0000000 --- a/server/db/talk-to-db.py +++ /dev/null @@ -1,48 +0,0 @@ -import os -import pymysql -from flask import jsonify -from dotenv import find_dotenv, load_dotenv - -dotenv_path = find_dotenv() -load_dotenv(dotenv_path) - -CLOUD_SQL_USERNAME = os.getenv('CLOUD_SQL_USERNAME', default=None) -CLOUD_SQL_PASSWORD = os.getenv('CLOUD_SQL_PASSWORD', default=None) -CLOUD_SQL_DATABASE_NAME = os.getenv('CLOUD_SQL_DATABASE_NAME', default=None) -CLOUD_SQL_CONNECTION_NAME = os.getenv('CLOUD_SQL_CONNECTION_NAME', default=None) -CLOUD_SQL_PORT = os.getenv('CLOUD_SQL_PORT', default=None) - - -def open_connection(): - unix_socket = '/cloudsql/{}'.format(CLOUD_SQL_CONNECTION_NAME) - try: - connection = pymysql.connect(user=CLOUD_SQL_USERNAME, - password=CLOUD_SQL_PASSWORD, - unix_socket=unix_socket, - db=CLOUD_SQL_DATABASE_NAME, - cursorclass=pymysql.cursors.DictCursor - ) - except pymysql.MySQLError as e: - return e - return connection - - -def get(): - connection = open_connection() - with connection.cursor() as cursor: - result = cursor.execute('SELECT * FROM songs;') - songs = cursor.fetchall() - if result > 0: - got_songs = jsonify(songs) - else: - got_songs = 'No Songs in DB' - return got_songs - - -def create(song): - connection = open_connection() - with connection.cursor() as cursor: - cursor.execute('INSERT INTO songs (title, artist, genre) VALUES(%s, %s, %s)', - (song["title"], song["artist"], song["genre"])) - connection.commit() - connection.close() diff --git a/server/db/tutorial/db.py b/server/db/tutorial/db.py deleted file mode 100644 index b00971c..0000000 --- a/server/db/tutorial/db.py +++ /dev/null @@ -1,45 +0,0 @@ -# db.py -import os -import pymysql -from flask import jsonify - -db_user = os.getenv(key, default=None) -db_password = os.environ.get('CLOUD_SQL_PASSWORD') -db_name = os.environ.get('CLOUD_SQL_DATABASE_NAME') -db_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME') - - -def open_connection(): - unix_socket = '/cloudsql/{}'.format(db_connection_name) - try: - if os.environ.get('GAE_ENV') == 'standard': - conn = pymysql.connect(user=db_user, - password=db_password, - unix_socket=unix_socket, - db=db_name, - cursorclass=pymysql.cursors.DictCursor - ) - except pymysql.MySQLError as e: - return e - return conn - - -def get(): - conn = open_connection() - with conn.cursor() as cursor: - result = cursor.execute('SELECT * FROM songs;') - songs = cursor.fetchall() - if result > 0: - got_songs = jsonify(songs) - else: - got_songs = 'No Songs in DB' - return got_songs - - -def create(song): - conn = open_connection() - with conn.cursor() as cursor: - cursor.execute('INSERT INTO songs (title, artist, genre) VALUES(%s, %s, %s)', - (song["title"], song["artist"], song["genre"])) - conn.commit() - conn.close() diff --git a/server/db/tutorial/main.py b/server/db/tutorial/main.py deleted file mode 100644 index b5fb3d7..0000000 --- a/server/db/tutorial/main.py +++ /dev/null @@ -1,23 +0,0 @@ -# main.py -from flask import Flask, jsonify, request -from db import get, create - -app = Flask(__name__) - - -@app.route('/', methods=['GET']) -def get_songs(): - return get() - - -@app.route('/add', methods=['POST']) -def add_song(): - if not request.is_json: - return jsonify({"msg": "Missing JSON in request"}), 400 - - create(request.get_json()) - return 'Song Added' - - -if __name__ == '__main__': - app.run() diff --git a/server/sql-functions.py b/server/sql-functions.py deleted file mode 100644 index 9674c40..0000000 --- a/server/sql-functions.py +++ /dev/null @@ -1,60 +0,0 @@ -# kiera figuring out sql -import os -from flask import Flask, request, jsonify -import pymysql.cursors -from dotenv import find_dotenv, load_dotenv - -app = Flask(__name__) - - -dotenv_path = find_dotenv() -load_dotenv(dotenv_path) - -CLOUD_SQL_USERNAME = os.getenv('CLOUD_SQL_USERNAME', default=None) -CLOUD_SQL_PASSWORD = os.getenv('CLOUD_SQL_PASSWORD', default=None) -CLOUD_SQL_DATABASE_NAME = os.getenv('CLOUD_SQL_DATABASE_NAME', default=None) -CLOUD_SQL_CONNECTION_NAME = os.getenv('CLOUD_SQL_CONNECTION_NAME', default=None) -CLOUD_SQL_PORT = int(os.getenv('CLOUD_SQL_PORT', default=None)) - - -conn = pymysql.connect( - port=CLOUD_SQL_PORT, - host=CLOUD_SQL_CONNECTION_NAME, # endpoint - user=CLOUD_SQL_USERNAME, - password=CLOUD_SQL_PASSWORD, - db=CLOUD_SQL_DATABASE_NAME, - cursorclass=pymysql.cursors.DictCursor -) - -cursor = conn.cursor() - -def getDecksFromUser(user_id: int): - cursor.execute(""" - SELECT Decks.name, Decks.deck_id - FROM User - INNER JOIN Decks - ON User._id = Decks.user - WHERE User._id = %s; - """, user_id) - return cursor.fetchall() - -def getCardsFromDeck(deck_id: int): - cursor.execute(""" - SELECT Cards.prompt, Cards.answer, Cards.card_id - FROM Decks - INNER JOIN Cards - ON Decks.deck_id = Cards.deck - WHERE Decks.deck_id = %s; - """, deck_id) - return cursor.fetchall() - -@app.route("/") -def hello(): - return getCardsFromDeck(1) - # return len(getDecksFromUser('lebron')) - - # cursor.execute(""" - # INSERT INTO Cards (prompt, answer, deck) VALUES ('leprompt', 'lanswer', 1); - # """) - # conn.commit() - # return cursor.fetchall() \ No newline at end of file