Refactor: Improve database queries and fix formatting issues in stream utilities
This commit is contained in:
@@ -85,7 +85,7 @@ def get_streamer_status(streamer_username):
|
||||
user_id = get_user_id(streamer_username)
|
||||
|
||||
if not user_id:
|
||||
abort(404)
|
||||
abort(404)
|
||||
|
||||
is_live = streamer_live_status(user_id)
|
||||
most_recent_stream = streamer_most_recent_stream(user_id)
|
||||
|
||||
@@ -62,3 +62,5 @@ SELECT * FROM stream_tags;
|
||||
-- To see all tables in the database
|
||||
SELECT name FROM sqlite_master WHERE type='table';
|
||||
|
||||
|
||||
SELECT isLive FROM streams WHERE user_id = '5';
|
||||
@@ -9,7 +9,7 @@ def streamer_live_status(user_id: int) -> bool:
|
||||
"""
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
is_live = bool(db.fetchone("SELECT 1 FROM streams WHERE user_id = ? AND isLive = 1 ORDER BY stream_id DESC", (user_id,)))
|
||||
is_live = db.fetchone("""SELECT isLive FROM streams WHERE user_id = ?""", (user_id,))
|
||||
db.close_connection()
|
||||
return is_live
|
||||
|
||||
@@ -66,7 +66,7 @@ def user_stream(user_id: int, stream_id: int) -> dict:
|
||||
"""
|
||||
db = Database()
|
||||
db.create_connection()
|
||||
stream = db.fetchone("SELECT * FROM streams WHERE user_id = ? AND stream_id = ?", (user_id,stream_id))
|
||||
stream = db.fetchone("SELECT * FROM streams WHERE user_id = ? AND stream_id = ?", (user_id, stream_id))
|
||||
db.close_connection()
|
||||
|
||||
return stream
|
||||
@@ -9,7 +9,7 @@ load_dotenv()
|
||||
|
||||
serializer = URLSafeTimedSerializer(getenv("AUTH_SECRET_KEY"))
|
||||
|
||||
def get_user_id(username: str) -> Optional[int]:
|
||||
def get_user_id(username: str) -> int:
|
||||
"""
|
||||
Returns user_id associated with given username
|
||||
"""
|
||||
@@ -21,7 +21,7 @@ def get_user_id(username: str) -> Optional[int]:
|
||||
"SELECT user_id FROM users WHERE username = ?",
|
||||
(username,)
|
||||
)
|
||||
return data[0] if data else None
|
||||
return data['user_id'] if data else None
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user