update
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter, HTTPException, Security
|
||||
from app.models import Song
|
||||
from app.dependencies import dbEngine, Base, dbSession
|
||||
from app.sql_models import SqlSong, SqlVote
|
||||
from app.dependencies import session
|
||||
from app.routers.user import get_current_user, User
|
||||
from typing import Annotated
|
||||
|
||||
@@ -10,7 +11,20 @@ router = APIRouter(
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def get_songs() -> list[dict]:
|
||||
return dbSession.query(Base.songs).all()
|
||||
async def get_songs(user_id : str = "") -> list[Song]:
|
||||
sqlsongs = session.query(SqlSong).filter(SqlSong.singable == True).all()
|
||||
votes = session.query(SqlVote).filter(SqlVote.user_id == user_id).all()
|
||||
votes = {v.song_id : v.vote for v in votes}
|
||||
|
||||
return [Song(**s.__dict__, vote=votes.get(s.id, None)) for s in sqlsongs] # type: ignore
|
||||
|
||||
@router.post("/{song_id}/vote")
|
||||
async def vote(song_id : str, user_id : str, vote : int):
|
||||
vote_entry = session.query(SqlVote).filter((SqlVote.user_id == user_id) & (SqlVote.song_id == song_id)).first()
|
||||
if vote_entry:
|
||||
vote_entry.vote = str(vote) # type: ignore
|
||||
else:
|
||||
vote_entry = SqlVote(song_id=song_id, user_id=user_id, vote=vote)
|
||||
session.add(vote_entry)
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user