diff --git a/backend/src/app.py b/backend/src/app.py index 9ddb702..ee7c838 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -17,19 +17,7 @@ socketio = SocketIO(app) CORS(app) -ROOMS: dict[int, Room] = { - 1234: Room( - 1234, - (0.0, 1.0), - "Test Room", - None, - {"Rock", "Metal"}, - {}, - [], - [Song(mbid="test", title="", artist="<artist placeholder>", tags=["Metal"], image_id="img-id", youtube_id="yt-id")], - playing_idx=1, - ) -} # { room_id: room, ... } +ROOMS: dict[int, Room] = {} # { room_id: room, ... } def error(msg: str, status: int = 400) -> Response: @@ -82,11 +70,56 @@ def queue_next(): # room.renew_queue() emit("update_songs", {"songs": [1, 2, 3]}, broadcast=True, namespace="/") - return {"success": True, "ended": True, "index": room.playing_idx, "queue": []} + return {"success": True, "ended": True, "index": room.playing_idx, "queue": room.playing} return {"success": True, "ended": False, "index": room.playing_idx} +@app.post("/api/room/new") +def room_new(): + if (room_name := request.args.get("name")) is None: + return error("Missing room name") + + if (room_cords := request.args.get("coords")) is None: + return error("Missing room coords") + + if room_pin := request.args.get("pin"): + room_pin = int(room_pin) + else: + room_pin = None + + lat, lon = room_cords.split(",") + + room = Room( + id=max(ROOMS or [0]) + 1, # + coord=(float(lat), float(lon)), + name=room_name, + pin=room_pin, + tags=set([tag for tag in request.args.get("tags", "").split(",") if tag]), + songs={}, + history=[], + playing=[], + playing_idx=-1, + ) + + ROOMS[room.id] = room + + return {"success": True, "room_id": room.id} + + +@app.get("/api/room") +def room(): + return [ + { + "id": room.id, + "name": room.name, + "private": room.pin is not None, + "coords": room.coord, + } + for room in ROOMS.values() + ] + + init_db() test_algo() exit() diff --git a/backend/src/song.py b/backend/src/song.py index 8be443d..5b3524b 100644 --- a/backend/src/song.py +++ b/backend/src/song.py @@ -30,7 +30,7 @@ class Song: youtube_id: str -def get_song_by_mbid(mbid: str) -> Song: +def get_song_by_mbid(mbid: str) -> Song | None: conn = get_connection() cursor = conn.cursor() cursor.execute("SELECT * FROM songs WHERE mbid = ?", (mbid,)) @@ -38,7 +38,7 @@ def get_song_by_mbid(mbid: str) -> Song: conn.close() if row is None: - raise ValueError(f"Song with MBID {mbid} not found") + return None song = Song(mbid=row["mbid"], title=row["title"], artist=row["artist"], tags=row["tags"].split(","), image_id=row["lastfm_image_id"], youtube_id=row["youtube_id"]) return song