79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
from app.db import get_db_connection
|
|
from app.schemas import LikeResponse
|
|
|
|
class LikeService:
|
|
"""Handle likes and matches"""
|
|
|
|
@staticmethod
|
|
def like_user(liker_id: int, liked_id: int) -> LikeResponse:
|
|
"""User A likes User B"""
|
|
if liker_id == liked_id:
|
|
raise ValueError("Cannot like yourself")
|
|
|
|
is_match = False
|
|
with get_db_connection() as conn:
|
|
cur = conn.cursor()
|
|
|
|
# Check if already liked
|
|
cur.execute("SELECT id FROM likes WHERE liker_id = %s AND liked_id = %s", (liker_id, liked_id))
|
|
if cur.fetchone():
|
|
raise ValueError("Already liked this user")
|
|
|
|
# Insert like
|
|
cur.execute(
|
|
"INSERT INTO likes (liker_id, liked_id) VALUES (%s, %s) RETURNING id",
|
|
(liker_id, liked_id)
|
|
)
|
|
like_id = cur.fetchone()[0]
|
|
conn.commit()
|
|
|
|
# Check for mutual like (match)
|
|
cur.execute(
|
|
"SELECT id FROM likes WHERE liker_id = %s AND liked_id = %s",
|
|
(liked_id, liker_id)
|
|
)
|
|
if cur.fetchone():
|
|
is_match = True
|
|
# Create conversation if not exists
|
|
cur.execute(
|
|
"""INSERT INTO conversations (user_id_1, user_id_2)
|
|
VALUES (%s, %s) ON CONFLICT DO NOTHING""",
|
|
(min(liker_id, liked_id), max(liker_id, liked_id))
|
|
)
|
|
conn.commit()
|
|
|
|
return LikeResponse(id=like_id, liker_id=liker_id, liked_id=liked_id, is_match=is_match)
|
|
|
|
@staticmethod
|
|
def get_matches(user_id: int) -> list:
|
|
"""Get all users that match with this user"""
|
|
with get_db_connection() as conn:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"""SELECT DISTINCT CASE
|
|
WHEN l1.liker_id = %s THEN l1.liked_id
|
|
ELSE l1.liker_id
|
|
END as match_user_id
|
|
FROM likes l1
|
|
JOIN likes l2 ON (
|
|
(l1.liker_id = l2.liked_id AND l1.liked_id = l2.liker_id)
|
|
)
|
|
WHERE l1.liker_id = %s OR l1.liked_id = %s""",
|
|
(user_id, user_id, user_id)
|
|
)
|
|
|
|
match_ids = [row[0] for row in cur.fetchall()]
|
|
|
|
# Fetch profile info for each match
|
|
matches = []
|
|
for match_id in match_ids:
|
|
cur.execute(
|
|
"SELECT id, display_name FROM profiles WHERE user_id = %s",
|
|
(match_id,)
|
|
)
|
|
row = cur.fetchone()
|
|
if row:
|
|
matches.append({"user_id": match_id, "display_name": row[1]})
|
|
|
|
return matches
|