REFACTOR: Moved user routes into user blueprint, and grouped them, fixed some (not all) routes

This commit is contained in:
2025-02-06 01:00:46 +00:00
parent 4b0bbb12ae
commit 6a8509ca42
7 changed files with 84 additions and 75 deletions

View File

@@ -34,18 +34,6 @@ def get_followed_live_streams(user_id: int) -> Optional[List[dict]]:
""", (user_id,))
return live_streams
def get_followed_streamers(user_id: int) -> Optional[List[dict]]:
"""
Returns a list of streamers who the user follows
"""
with Database() as db:
followed_streamers = db.fetchall("""
SELECT user_id, username
FROM users
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
""", (user_id,))
return followed_streamers
def get_vod(vod_id: int) -> dict:
"""
Returns data of a streamers vod
@@ -71,17 +59,6 @@ def get_user_vods(user_id: int):
return vods
def get_streamer_data(user_id: int) -> Optional[dict]:
"""
Returns information about the streamer
"""
with Database() as db:
data = db.fetchone("""
SELECT username, bio, num_followers, is_partnered FROM users
WHERE user_id = ?;
""", (user_id,))
return data
def generate_thumbnail(user_id: int) -> None:
"""
Generates the thumbnail of a stream

View File

@@ -1,5 +1,5 @@
from database.database import Database
from typing import Optional
from typing import Optional, List
from datetime import datetime
from itsdangerous import URLSafeTimedSerializer
from os import getenv
@@ -45,19 +45,22 @@ def is_user_partner(user_id: int) -> bool:
""", (user_id,))
return bool(data)
def is_subscribed(user_id: int, streamer_id: int) -> bool:
def is_subscribed(user_id: int, subscribed_to_id: int) -> bool:
"""
Returns True if user is subscribed to a streamer, else False
"""
with Database() as db:
result = db.fetchone("""
SELECT 1
SELECT *
FROM subscribes
WHERE user_id = ?
AND streamer_id = ?
AND expires > ?
""", (user_id, streamer_id, datetime.now()))
return bool(result)
AND subscribed_id = ?
AND expires > ?;
""", (user_id, subscribed_to_id, datetime.now()))
print(result)
if result:
return True
return False
def is_following(user_id: int, followed_id: int) -> bool:
"""
@@ -108,7 +111,7 @@ def subscription_expiration(user_id: int, subscribed_id: int) -> int:
with Database() as db:
data = db.fetchone("""
SELECT expires
FROM subscriptions
FROM subscribes
WHERE user_id = ?
AND subscribed_id = ?
AND expires > ?
@@ -149,4 +152,27 @@ def get_email(user_id: int) -> Optional[str]:
WHERE user_id = ?
""", (user_id,))
return email["email"] if email else None
return email["email"] if email else None
def get_followed_streamers(user_id: int) -> Optional[List[dict]]:
"""
Returns a list of streamers who the user follows
"""
with Database() as db:
followed_streamers = db.fetchall("""
SELECT user_id, username
FROM users
WHERE user_id IN (SELECT followed_id FROM follows WHERE user_id = ?);
""", (user_id,))
return followed_streamers
def get_user_data(user_id: int) -> Optional[dict]:
"""
Returns username, bio, number of followers, and if user is partnered from user_id
"""
with Database() as db:
data = db.fetchone("""
SELECT username, bio, num_followers, is_partnered FROM users
WHERE user_id = ?;
""", (user_id,))
return data