Skip to content

Commit 1b45dd2

Browse files
committed
implement /current endpoint
1 parent 374de1d commit 1b45dd2

6 files changed

Lines changed: 62 additions & 13 deletions

File tree

src/main/kotlin/gent/zeus/guitar/PlayerState.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ object PlayerState {
2121

2222
val mutex = Mutex()
2323
}
24+
25+
class EmptyPlayerStateError : ServerError("no tracks have played yet since last restart", null)

src/main/kotlin/gent/zeus/guitar/api/MusicControllers.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,18 @@ class ArtistController {
3737
private fun artistDetails(@PathVariable id: String) = getApiResponse(Preset.Artist.details, id)
3838
}
3939

40-
/*
41-
42-
// coroutines make spring boot crash
4340

4441
@RestController
4542
@RequestMapping("/current")
4643
class CurrentController {
4744

48-
@GetMapping("/")
49-
private suspend fun currentTrack() = coroutineScope {
50-
runBlocking {
51-
val id = PlayerState.mutex.withLock { PlayerState.currentTrackId }
45+
@GetMapping("")
46+
private fun currentTrack() = runBlocking {
47+
val id = PlayerState.mutex.withLock { PlayerState.currentTrackId }
5248

53-
if (id != null)
54-
getApiResponse(Preset.Track.details, id)
55-
else
56-
ResponseEntity.status(200).body(null)
57-
}
49+
if (id != null)
50+
getApiResponse(Preset.Track.detailsWithState, id)
51+
else
52+
ResponseEntity.status(200).body(null)
5853
}
5954
}
60-
*/

src/main/kotlin/gent/zeus/guitar/data/MusicModels.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ data class Track(
1212
val album: Album? = null,
1313
val artists: List<Artist>? = null,
1414
val durationInMs: Int? = null,
15+
val startedAtMs: Long? = null,
16+
val endsAtMs: Long? = null,
1517
val imageUrl: String? = null,
1618
val spotifyUrl: String? = null,
1719
val votesFor: Int? = null,

src/main/kotlin/gent/zeus/guitar/data/Preset.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package gent.zeus.guitar.data
22

3+
import gent.zeus.guitar.data.Preset.Track
4+
import gent.zeus.guitar.ext.playerstate.PlayerStateFetcher
35
import gent.zeus.guitar.ext.spotify.AlbumFetcher
46
import gent.zeus.guitar.ext.spotify.ArtistFetcher
57
import gent.zeus.guitar.ext.spotify.TrackFetcher
@@ -9,6 +11,7 @@ object Preset {
911
object Track {
1012
val details get() = MusicModelMaker(::Track, TrackFetcher(), VoteFetcher())
1113
val voteless get() = MusicModelMaker(::Track, TrackFetcher())
14+
val detailsWithState get() = MusicModelMaker(::Track, TrackFetcher(), VoteFetcher(), PlayerStateFetcher())
1215
}
1316

1417
object Album {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package gent.zeus.guitar.ext
2+
3+
import gent.zeus.guitar.ServerError
4+
5+
class OrderingError(missingValueName: String) : ServerError(
6+
"model fillers are in wrong order (value `$missingValueName` is still missing)",
7+
null,
8+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gent.zeus.guitar.ext.playerstate
2+
3+
import gent.zeus.guitar.DataResult
4+
import gent.zeus.guitar.EmptyPlayerStateError
5+
import gent.zeus.guitar.PlayerState
6+
import gent.zeus.guitar.ServerError
7+
import gent.zeus.guitar.UserError
8+
import gent.zeus.guitar.data.Track
9+
import gent.zeus.guitar.ext.ModelFiller
10+
import gent.zeus.guitar.ext.OrderingError
11+
import kotlinx.coroutines.runBlocking
12+
import kotlinx.coroutines.sync.withLock
13+
import org.apache.catalina.Server
14+
15+
class PlayerStateFetcher : ModelFiller<Track> {
16+
override fun fetchInto(musicModel: Track): DataResult<Track> = runBlocking {
17+
musicModel.durationInMs
18+
?: return@runBlocking DataResult.Error(OrderingError("durationInMs"))
19+
20+
val id = PlayerState.mutex.withLock { PlayerState.currentTrackId }
21+
?: return@runBlocking DataResult.Error(EmptyPlayerStateError())
22+
23+
if (id != musicModel.spotifyId) DataResult.Error(NotCurrentTrackError())
24+
25+
val startedAt = PlayerState.mutex.withLock { PlayerState.currentStartTime }
26+
?: return@runBlocking DataResult.Error(EmptyPlayerStateError())
27+
28+
DataResult.Ok(
29+
musicModel.copy(
30+
startedAtMs = startedAt,
31+
endsAtMs = startedAt + musicModel.durationInMs
32+
)
33+
)
34+
}
35+
}
36+
37+
class NotCurrentTrackError : ServerError(
38+
"this is not the current track",
39+
null,
40+
)

0 commit comments

Comments
 (0)