diff --git a/web_server/blueprints/streams.py b/web_server/blueprints/streams.py index 6d6af97..93fce7a 100644 --- a/web_server/blueprints/streams.py +++ b/web_server/blueprints/streams.py @@ -1,7 +1,8 @@ -from flask import Blueprint, session, jsonify +from flask import Blueprint, session, jsonify, g from utils.stream_utils import streamer_live_status, streamer_most_recent_stream, user_stream, followed_live_streams from utils.user_utils import get_user_id -from database.database import Database +from utils import login_required +from database.database import Database, fetch_data_as_list stream_bp = Blueprint("stream", __name__) @@ -21,9 +22,13 @@ def get_sample_streams() -> list[dict]: # fetch top 25 most viewed live streams if not logged in # TODO Add a check to see if user is logged in, if they are, find streams that match categories they follow - streams = cursor.execute("""SELECT * FROM streams - ORDER BY num_viewers DESC - LIMIT 25; """).fetchall() + query = """SELECT * FROM streams + ORDER BY num_viewers DESC + LIMIT 25; """ + + streams = fetch_data_as_list(cursor, query) + + return jsonify({ "streams": streams }) @@ -67,25 +72,27 @@ def get_categories() -> list[dict]: cursor = db.create_connection() # fetch top categories by number of viewers - categories = cursor.execute("""SELECT category_name, SUM(num_viewers) FROM categories, streams + query = """SELECT categories.category_id, category_name, SUM(num_viewers) as num_viewers FROM categories, streams WHERE categories.category_id = streams.category_id GROUP BY category_name ORDER BY SUM(num_viewers) DESC - LIMIT 25; """).fetchall() + LIMIT 25; """ + + categories = fetch_data_as_list(cursor, query) return jsonify({'categories': categories}) - +@login_required @stream_bp.route('/get_followed_categories') def get_followed_categories() -> list | list[dict]: """ Queries DB to get a list of followed categories - Hmm.. - """ - categories = [] - if categories: - return categories - return get_categories() + """ + db = Database() + cursor = db.create_connection() + + # fetch categories that the user follows + @stream_bp.route('/get_streamer_data/') def get_streamer_data(streamer_username): diff --git a/web_server/database/app.db b/web_server/database/app.db index c452c93..2a24de6 100644 Binary files a/web_server/database/app.db and b/web_server/database/app.db differ diff --git a/web_server/database/database.py b/web_server/database/database.py index 034a6ff..51f0d7a 100644 --- a/web_server/database/database.py +++ b/web_server/database/database.py @@ -19,4 +19,17 @@ class Database: print(e) def close_connection(self) -> None: - self._conn.close() \ No newline at end of file + self._conn.close() + +def fetch_data_as_list(cursor, query, params=None): + # Execute the query with parameters (if any) + cursor.execute(query, params or []) + + # Get the column names from the cursor + columns = [description[0] for description in cursor.description] + + # Convert rows to dictionaries + rows = cursor.fetchall() + result = [dict(zip(columns, row)) for row in rows] + + return result \ No newline at end of file diff --git a/web_server/database/testing_data.sql b/web_server/database/testing_data.sql index 42cffe7..529f0e6 100644 --- a/web_server/database/testing_data.sql +++ b/web_server/database/testing_data.sql @@ -8,11 +8,11 @@ INSERT INTO categories (category_name) VALUES -- Sample data for users INSERT INTO users (username, password, email, num_followers, stream_key, is_partnered, bio) VALUES -('GamerDude', 'password123', 'gamerdude@example.com', 500, '1', 0, 'Streaming my gaming adventures!'), -('MusicLover', 'music4life', 'musiclover@example.com', 1200, '1', 0, 'I share my favorite tunes.'), -('ArtFan', 'artistic123', 'artfan@example.com', 300, '1', 0, 'Exploring the world of art.'), -('EduGuru', 'learn123', 'eduguru@example.com', 800, '1', 0, 'Teaching everything I know.'), -('SportsStar', 'sports123', 'sportsstar@example.com', 2000, '1', 0, 'Join me for live sports updates!'); +('GamerDude', 'password123', 'gamerdude@example.com', 500, '1234', 0, 'Streaming my gaming adventures!'), +('MusicLover', 'music4life', 'musiclover@example.com', 1200, '2345', 0, 'I share my favorite tunes.'), +('ArtFan', 'artistic123', 'artfan@example.com', 300, '3456', 0, 'Exploring the world of art.'), +('EduGuru', 'learn123', 'eduguru@example.com', 800, '4567', 0, 'Teaching everything I know.'), +('SportsStar', 'sports123', 'sportsstar@example.com', 2000, '5678', 0, 'Join me for live sports updates!'); -- Sample data for streams INSERT INTO streams (user_id, title, start_time, num_viewers, isLive, vod_id, category_id) VALUES @@ -52,3 +52,8 @@ SELECT * FROM follows; SELECT * FROM user_preferences; SELECT * FROM subscribes; SELECT * FROM categories; + +INSERT INTO streams (user_id, title, start_time, num_viewers, isLive, vod_id, category_id) VALUES +(6, 'Epic Gaming Session 2', '2025-01-26 18:00:00', 800, 1, NULL, 1); +INSERT INTO users (username, password, email, num_followers, stream_key, is_partnered, bio) VALUES +('GamerDude2', 'password123', 'gamerdude2@gmail.com', 3200, '6789', 0, 'Streaming my gaming adventures!');