From b40e81ca9008e6851cd5e3d05d81f4bdac9a4e1b Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Thu, 7 Dec 2023 22:01:53 +0300
Subject: [PATCH 1/7] fix: remove self arg from system router

---
 app/system/router.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/system/router.py b/app/system/router.py
index 3f1cbf3..9755d15 100644
--- a/app/system/router.py
+++ b/app/system/router.py
@@ -5,11 +5,11 @@
 
 
 @system_router.get("/", include_in_schema=False)
-async def root(self) -> dict[str, str]:
+async def root() -> dict[str, str]:
     return {"message": "MusicBox API"}
 
 
 @system_router.get("/health", include_in_schema=False)
-async def health(self) -> JSONResponse:
+async def health() -> JSONResponse:
     data = {"music-box-api": status.HTTP_200_OK}
     return JSONResponse(data, status_code=status.HTTP_200_OK)

From e81a4f507b6257d792149f73381de473ec71f9a7 Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Thu, 7 Dec 2023 22:35:13 +0300
Subject: [PATCH 2/7] feat: add current device model

---
 app/spotify/schemas.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/app/spotify/schemas.py b/app/spotify/schemas.py
index 3bb8720..af5736d 100644
--- a/app/spotify/schemas.py
+++ b/app/spotify/schemas.py
@@ -12,6 +12,12 @@ class Device(BaseModel):
     volume_percent: int
 
 
+class CurrentDevice(BaseModel):
+    device: Device
+    index: int
+    total: int
+
+
 class Track(BaseModel):
     id: str
 

From 97b45ca2f8d3a8ce6a135bf4e23d859a6a96af8a Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Thu, 7 Dec 2023 22:35:34 +0300
Subject: [PATCH 3/7] fix: add device caching

---
 app/spotify/service.py | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/app/spotify/service.py b/app/spotify/service.py
index c497bc9..5a7458d 100644
--- a/app/spotify/service.py
+++ b/app/spotify/service.py
@@ -44,8 +44,12 @@ def play(self, track: Track) -> None:
                 raise TrackNotFoundError(e.msg)
 
     def refresh_devices(self) -> list[Device]:
-        self.__devices = self.__get_devices_from_api()
-        self.__current_device_index = 0
+        devices_from_api = self.__get_devices_from_api()
+
+        if devices_from_api != self.__devices:
+            self.__devices = devices_from_api
+            self.__current_device_index = 0
+
         return self.__devices
 
     def next_device(self) -> Device | None:
@@ -68,10 +72,33 @@ def get_current_device(self) -> Device | None:
 
         return self.__devices[self.__current_device_index]
 
+    def get_current_device_index(self) -> int:
+        return self.__current_device_index
+
     def get_devices(self) -> list[Device]:
-        print(self.__devices)
         return self.__devices
 
+    def get_devices_count(self) -> int:
+        return len(self.__devices)
+
     def __get_devices_from_api(self) -> list[Device]:
         devices_json = self.__api.devices()["devices"]
         return [Device(**device) for device in devices_json]
+
+
+
+if __name__ == "__main__":
+    import os
+
+    SPOTIFY_CLIENT_ID = "f194ec87794e4505ace8cb76a65b37df"
+    SPOTIFY_CLIENT_SECRET = "324b3a763f704dbeb0764f9e2bf8d53a"
+
+    service = SpotifyService(
+        client_id=SPOTIFY_CLIENT_ID,
+        client_secret=SPOTIFY_CLIENT_SECRET,
+        redirect_uri="http://localhost:8080"
+    )
+
+    print(service.get_devices())
+
+    service.play(Track(id="2bqS0QtnXGjOYs3z6VtSyW"))

From 7f59064286bca464472dd461e89dfe23decf974f Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Thu, 7 Dec 2023 22:36:00 +0300
Subject: [PATCH 4/7] feat: add current, next and previous device endpoints

---
 app/spotify/router.py | 60 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 6 deletions(-)

diff --git a/app/spotify/router.py b/app/spotify/router.py
index 50e03d9..e87eafe 100644
--- a/app/spotify/router.py
+++ b/app/spotify/router.py
@@ -2,15 +2,63 @@
 from fastapi import APIRouter, Depends
 
 from app.container import Container
-from app.spotify.schemas import Device
+from app.spotify.schemas import Device, CurrentDevice
 from app.spotify.service import SpotifyService
 
 spotify_router = APIRouter()
 
 
-@spotify_router.get("/devices")
+@spotify_router.get("/device")
 @inject
-def get_devices(
-        spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]),
-) -> list[Device]:
-    return spotify_service.refresh_devices()
+def get_current_device(
+        spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
+) -> CurrentDevice | dict:
+    spotify_service.refresh_devices()
+    current_device = spotify_service.get_current_device()
+
+    if current_device is None:
+        return {}
+
+    return CurrentDevice(
+        device=current_device,
+        index=spotify_service.get_current_device_index(),
+        total=spotify_service.get_devices_count()
+    )
+
+
+@spotify_router.get("/device/next")
+@inject
+def next_device(
+        spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
+) -> CurrentDevice | dict:
+    spotify_service.refresh_devices()
+    spotify_service.next_device()
+    current_device = spotify_service.get_current_device()
+
+    if current_device is None:
+        return {}
+
+    return CurrentDevice(
+        device=current_device,
+        index=spotify_service.get_current_device_index(),
+        total=spotify_service.get_devices_count()
+    )
+
+
+@spotify_router.get("/device/previous")
+@inject
+def previous_device(
+        spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
+) -> CurrentDevice | dict:
+    spotify_service.refresh_devices()
+    spotify_service.previous_device()
+    current_device = spotify_service.get_current_device()
+
+    if current_device is None:
+        return {}
+
+    return CurrentDevice(
+        device=current_device,
+        index=spotify_service.get_current_device_index(),
+        total=spotify_service.get_devices_count()
+    )

From 9e283be55a292382713c97e0d2665e8af8b5b8b9 Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Thu, 7 Dec 2023 22:37:09 +0300
Subject: [PATCH 5/7] fix: change get to post

---
 app/spotify/router.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/spotify/router.py b/app/spotify/router.py
index e87eafe..753a823 100644
--- a/app/spotify/router.py
+++ b/app/spotify/router.py
@@ -26,7 +26,7 @@ def get_current_device(
     )
 
 
-@spotify_router.get("/device/next")
+@spotify_router.post("/device/next")
 @inject
 def next_device(
         spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
@@ -45,7 +45,7 @@ def next_device(
     )
 
 
-@spotify_router.get("/device/previous")
+@spotify_router.post("/device/previous")
 @inject
 def previous_device(
         spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])

From 5c06589b6fb2629ab6fd420e1d51b648f102581b Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Fri, 8 Dec 2023 02:08:04 +0300
Subject: [PATCH 6/7] feat: add play endpoint

---
 app/spotify/router.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/app/spotify/router.py b/app/spotify/router.py
index 753a823..5f305f9 100644
--- a/app/spotify/router.py
+++ b/app/spotify/router.py
@@ -2,7 +2,8 @@
 from fastapi import APIRouter, Depends
 
 from app.container import Container
-from app.spotify.schemas import Device, CurrentDevice
+from app.spotify.exceptions import TrackNotFoundError, DeviceNotFoundError
+from app.spotify.schemas import Device, CurrentDevice, Track
 from app.spotify.service import SpotifyService
 
 spotify_router = APIRouter()
@@ -62,3 +63,18 @@ def previous_device(
         index=spotify_service.get_current_device_index(),
         total=spotify_service.get_devices_count()
     )
+
+
+@spotify_router.post("/play")
+@inject
+def play(
+        track: Track,
+        spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
+) -> Device | dict:
+    try:
+        spotify_service.play(track)
+    except TrackNotFoundError:
+        return {"error": "Track not found"}
+    except DeviceNotFoundError:
+        return {"error": "Device not found"}
+    return {"status": "ok"}

From 54bff671c89750dc8a1673c99536ae9fbc4a78f1 Mon Sep 17 00:00:00 2001
From: Tim <herew26@gmail.com>
Date: Fri, 8 Dec 2023 02:41:10 +0300
Subject: [PATCH 7/7] fix: remove main

---
 app/spotify/service.py | 18 ------------------
 1 file changed, 18 deletions(-)

diff --git a/app/spotify/service.py b/app/spotify/service.py
index 5a7458d..05871c3 100644
--- a/app/spotify/service.py
+++ b/app/spotify/service.py
@@ -84,21 +84,3 @@ def get_devices_count(self) -> int:
     def __get_devices_from_api(self) -> list[Device]:
         devices_json = self.__api.devices()["devices"]
         return [Device(**device) for device in devices_json]
-
-
-
-if __name__ == "__main__":
-    import os
-
-    SPOTIFY_CLIENT_ID = "f194ec87794e4505ace8cb76a65b37df"
-    SPOTIFY_CLIENT_SECRET = "324b3a763f704dbeb0764f9e2bf8d53a"
-
-    service = SpotifyService(
-        client_id=SPOTIFY_CLIENT_ID,
-        client_secret=SPOTIFY_CLIENT_SECRET,
-        redirect_uri="http://localhost:8080"
-    )
-
-    print(service.get_devices())
-
-    service.play(Track(id="2bqS0QtnXGjOYs3z6VtSyW"))
