|
2 | 2 | from fastapi import APIRouter, Depends |
3 | 3 |
|
4 | 4 | from app.container import Container |
5 | | -from app.spotify.schemas import Device |
| 5 | +from app.spotify.exceptions import TrackNotFoundError, DeviceNotFoundError |
| 6 | +from app.spotify.schemas import Device, CurrentDevice, Track |
6 | 7 | from app.spotify.service import SpotifyService |
7 | 8 |
|
8 | 9 | spotify_router = APIRouter() |
9 | 10 |
|
10 | 11 |
|
11 | | -@spotify_router.get("/devices") |
| 12 | +@spotify_router.get("/device") |
12 | 13 | @inject |
13 | | -def get_devices( |
14 | | - spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]), |
15 | | -) -> list[Device]: |
16 | | - return spotify_service.refresh_devices() |
| 14 | +def get_current_device( |
| 15 | + spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]) |
| 16 | +) -> CurrentDevice | dict: |
| 17 | + spotify_service.refresh_devices() |
| 18 | + current_device = spotify_service.get_current_device() |
| 19 | + |
| 20 | + if current_device is None: |
| 21 | + return {} |
| 22 | + |
| 23 | + return CurrentDevice( |
| 24 | + device=current_device, |
| 25 | + index=spotify_service.get_current_device_index(), |
| 26 | + total=spotify_service.get_devices_count() |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@spotify_router.post("/device/next") |
| 31 | +@inject |
| 32 | +def next_device( |
| 33 | + spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]) |
| 34 | +) -> CurrentDevice | dict: |
| 35 | + spotify_service.refresh_devices() |
| 36 | + spotify_service.next_device() |
| 37 | + current_device = spotify_service.get_current_device() |
| 38 | + |
| 39 | + if current_device is None: |
| 40 | + return {} |
| 41 | + |
| 42 | + return CurrentDevice( |
| 43 | + device=current_device, |
| 44 | + index=spotify_service.get_current_device_index(), |
| 45 | + total=spotify_service.get_devices_count() |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@spotify_router.post("/device/previous") |
| 50 | +@inject |
| 51 | +def previous_device( |
| 52 | + spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]) |
| 53 | +) -> CurrentDevice | dict: |
| 54 | + spotify_service.refresh_devices() |
| 55 | + spotify_service.previous_device() |
| 56 | + current_device = spotify_service.get_current_device() |
| 57 | + |
| 58 | + if current_device is None: |
| 59 | + return {} |
| 60 | + |
| 61 | + return CurrentDevice( |
| 62 | + device=current_device, |
| 63 | + index=spotify_service.get_current_device_index(), |
| 64 | + total=spotify_service.get_devices_count() |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@spotify_router.post("/play") |
| 69 | +@inject |
| 70 | +def play( |
| 71 | + track: Track, |
| 72 | + spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]) |
| 73 | +) -> Device | dict: |
| 74 | + try: |
| 75 | + spotify_service.play(track) |
| 76 | + except TrackNotFoundError: |
| 77 | + return {"error": "Track not found"} |
| 78 | + except DeviceNotFoundError: |
| 79 | + return {"error": "Device not found"} |
| 80 | + return {"status": "ok"} |
0 commit comments