-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from musicorum-app/parties
In progress: party routes and schemas
- Loading branch information
Showing
21 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/io/musicorum/api/realms/charts/KoinModules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.musicorum.api.realms.charts | ||
|
||
import io.musicorum.api.realms.charts.repositories.ChartSnapshotRepository | ||
import io.musicorum.api.realms.charts.repositories.ChartTrackRepository | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val chartModules = module(createdAtStart = true) { | ||
singleOf(::ChartTrackRepository) | ||
singleOf(::ChartSnapshotRepository) | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/io/musicorum/api/realms/charts/repositories/ChartSnapshotRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.musicorum.api.realms.charts.repositories | ||
|
||
import io.musicorum.api.realms.charts.schemas.ChartSnapshot | ||
import io.musicorum.api.services.database | ||
import kotlinx.coroutines.Dispatchers | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.javatime.CurrentDateTime | ||
import org.jetbrains.exposed.sql.javatime.datetime | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import java.time.ZoneOffset | ||
|
||
class ChartSnapshotRepository { | ||
object ChartSnapshot : Table("chart_snapshots") { | ||
val id = integer("id").autoIncrement() | ||
val updatedAt = datetime("updated_at") | ||
|
||
override val primaryKey = PrimaryKey(id) | ||
} | ||
|
||
init { | ||
transaction(database) { | ||
SchemaUtils.create(ChartSnapshot) | ||
} | ||
} | ||
|
||
suspend fun createSnapshot(): Int { | ||
return newSuspendedTransaction(Dispatchers.IO) { | ||
return@newSuspendedTransaction ChartSnapshot.insert { | ||
it[updatedAt] = CurrentDateTime | ||
}[ChartSnapshot.id] | ||
} | ||
} | ||
|
||
suspend fun getSnapshot(id: Int): io.musicorum.api.realms.charts.schemas.ChartSnapshot { | ||
return newSuspendedTransaction { | ||
return@newSuspendedTransaction ChartSnapshot.select { ChartSnapshot.id eq id }.map { | ||
ChartSnapshot( | ||
it[ChartSnapshot.id], | ||
it[ChartSnapshot.updatedAt].toEpochSecond(ZoneOffset.UTC) | ||
) | ||
}.first() | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/io/musicorum/api/realms/charts/repositories/ChartTrackRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.musicorum.api.realms.charts.repositories | ||
|
||
import io.musicorum.api.realms.charts.schemas.ChartTrack | ||
import io.musicorum.api.services.database | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
class ChartTrackRepository { | ||
object ChartTracks : Table("chart_tracks") { | ||
val id = integer("id").autoIncrement() | ||
val name = text("name") | ||
val playCount = long("play_count") | ||
val snapshotId = integer("snapshot_id").references(ChartSnapshotRepository.ChartSnapshot.id) | ||
val listeners = long("listeners") | ||
val artist = text("artist") | ||
val position = integer("positions") | ||
|
||
override val primaryKey = PrimaryKey(id) | ||
} | ||
|
||
init { | ||
transaction(database) { | ||
SchemaUtils.createMissingTablesAndColumns(ChartTracks) | ||
} | ||
} | ||
|
||
suspend fun insert(track: ChartTrack): Int { | ||
return newSuspendedTransaction { | ||
return@newSuspendedTransaction ChartTracks.insert { | ||
it[name] = track.name | ||
it[playCount] = track.playCount | ||
it[snapshotId] = track.snapshotId | ||
it[listeners] = track.listeners | ||
it[position] = track.position | ||
it[artist] = track.artist | ||
}[ChartTracks.id] | ||
} | ||
} | ||
|
||
suspend fun getAll(snapshotId: Int): List<ChartTrack> { | ||
return newSuspendedTransaction { | ||
return@newSuspendedTransaction ChartTracks.select { | ||
ChartTracks.snapshotId eq snapshotId | ||
}.map { | ||
ChartTrack( | ||
it[ChartTracks.playCount], | ||
it[ChartTracks.snapshotId], | ||
it[ChartTracks.listeners], | ||
it[ChartTracks.artist], | ||
it[ChartTracks.position], | ||
it[ChartTracks.name] | ||
) | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/io/musicorum/api/realms/charts/repositories/lastfm/LastFmChartRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.musicorum.api.realms.charts.repositories.lastfm | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.engine.cio.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import io.musicorum.api.enums.EnvironmentVariable | ||
import io.musicorum.api.realms.charts.schemas.lastfm.BaseChartArtistResponse | ||
import io.musicorum.api.realms.charts.schemas.lastfm.BaseChartTrackResponse | ||
import io.musicorum.api.realms.charts.schemas.lastfm.ChartArtist | ||
import io.musicorum.api.realms.charts.schemas.lastfm.ChartTrack | ||
import io.musicorum.api.utils.getRequiredEnv | ||
import kotlinx.serialization.json.Json | ||
|
||
class LastFmChartRepository { | ||
companion object { | ||
private val client = HttpClient(CIO) { | ||
install(ContentNegotiation) { | ||
json(Json { ignoreUnknownKeys = true }) | ||
} | ||
|
||
defaultRequest { | ||
url("https", "ws.audioscrobbler.com") { | ||
path("2.0") | ||
parameters.append("api_key", getRequiredEnv(EnvironmentVariable.LastfmApiKey)) | ||
parameters.append("format", "json") | ||
|
||
} | ||
} | ||
} | ||
|
||
suspend fun getTopTracks(): List<ChartTrack> { | ||
val res = client.get { | ||
url { | ||
parameter("method", "chart.gettoptracks") | ||
} | ||
}.body<BaseChartTrackResponse>() | ||
|
||
return res.tracks.tracks | ||
} | ||
|
||
suspend fun getTopArtists(): List<ChartArtist> { | ||
val res = client.get { | ||
url { | ||
parameter("method", "chart.gettopartists") | ||
} | ||
}.body<BaseChartArtistResponse>() | ||
|
||
return res.artists.artists | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/io/musicorum/api/realms/charts/routes/Routes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.musicorum.api.realms.charts.routes | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.musicorum.api.realms.charts.repositories.ChartSnapshotRepository | ||
import io.musicorum.api.realms.charts.repositories.ChartTrackRepository | ||
import io.musicorum.api.realms.charts.repositories.lastfm.LastFmChartRepository | ||
import io.musicorum.api.realms.charts.schemas.ChartTrack | ||
import kotlinx.serialization.Serializable | ||
import org.koin.ktor.ext.inject | ||
|
||
fun Application.createChartRoutes() { | ||
val chartSnapshotRepository = inject<ChartSnapshotRepository>() | ||
val trackRepository = inject<ChartTrackRepository>() | ||
|
||
routing { | ||
route("/charts") { | ||
route("/positions") { | ||
createUpdatePositionsRoute() | ||
|
||
get { | ||
val snapshotId = call.request.queryParameters["snapshotId"]?.toIntOrNull() | ||
if (snapshotId == null) { | ||
call.respond(HttpStatusCode.BadRequest) | ||
} else { | ||
val tracks = trackRepository.value.getAll(snapshotId) | ||
val lastUpdated = chartSnapshotRepository.value.getSnapshot(snapshotId).updatedAt | ||
call.respond(PositionResponse(lastUpdated, tracks)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
private data class PositionResponse( | ||
val updatedAt: Long, | ||
val trackEntries: List<ChartTrack> | ||
) |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/io/musicorum/api/realms/charts/routes/UpdatePositions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.musicorum.api.realms.charts.routes | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.musicorum.api.realms.charts.repositories.ChartSnapshotRepository | ||
import io.musicorum.api.realms.charts.repositories.ChartTrackRepository | ||
import io.musicorum.api.realms.charts.repositories.lastfm.LastFmChartRepository | ||
import org.koin.ktor.ext.inject | ||
|
||
fun Route.createUpdatePositionsRoute() { | ||
val chartTrackRepository = inject<ChartTrackRepository>() | ||
val chartSnapshotRepository = inject<ChartSnapshotRepository>() | ||
|
||
route("/update") { | ||
post { | ||
val topTracks = LastFmChartRepository.getTopTracks() | ||
val topArtists = LastFmChartRepository.getTopArtists() | ||
|
||
val snapshotId = chartSnapshotRepository.value.createSnapshot() | ||
|
||
var trackPos = 1 | ||
for (track in topTracks) { | ||
chartTrackRepository.value.insert( | ||
io.musicorum.api.realms.charts.schemas.ChartTrack( | ||
name = track.name, | ||
playCount = track.playCount.toLong(), | ||
listeners = track.listeners.toLong(), | ||
position = trackPos++, | ||
artist = track.artist.name, | ||
snapshotId = snapshotId | ||
) | ||
) | ||
} | ||
|
||
call.respond(HttpStatusCode.Created) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/io/musicorum/api/realms/charts/schemas/ChartSnapshot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.musicorum.api.realms.charts.schemas | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ChartSnapshot( | ||
val id: Int, | ||
val updatedAt: Long | ||
) |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/io/musicorum/api/realms/charts/schemas/ChartTrack.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.musicorum.api.realms.charts.schemas | ||
|
||
import kotlinx.serialization.Serializable | ||
import java.math.BigInteger | ||
|
||
@Serializable | ||
data class ChartTrack( | ||
val playCount: Long, | ||
val snapshotId: Int, | ||
val listeners: Long, | ||
val artist: String, | ||
val position: Int, | ||
val name: String | ||
) |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/io/musicorum/api/realms/charts/schemas/lastfm/ChartArtist.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.musicorum.api.realms.charts.schemas.lastfm | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class BaseChartArtistResponse( | ||
val artists: ChartArtistResponse | ||
) | ||
|
||
@Serializable | ||
data class ChartArtistResponse( | ||
@SerialName("artist") | ||
val artists: List<ChartArtist> | ||
) | ||
|
||
|
||
@Serializable | ||
data class ChartArtist( | ||
val name: String, | ||
@SerialName("playcount") | ||
val playCount: String, | ||
val listeners: String | ||
) |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/io/musicorum/api/realms/charts/schemas/lastfm/ChartTrack.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.musicorum.api.realms.charts.schemas.lastfm | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class BaseChartTrackResponse( | ||
val tracks: ChartTrackResponse | ||
) | ||
|
||
@Serializable | ||
data class ChartTrackResponse( | ||
@SerialName("track") | ||
val tracks: List<ChartTrack> | ||
) | ||
|
||
@Serializable | ||
data class ChartTrack( | ||
val name: String, | ||
@SerialName("playcount") | ||
val playCount: String, | ||
val listeners: String, | ||
val artist: Artist | ||
) | ||
|
||
@Serializable | ||
data class Artist( | ||
val name: String | ||
) |
Oops, something went wrong.