mirror of https://github.com/aruppi/aruppi-api.git
parent
5e4392f861
commit
d7ca2965d2
@ -0,0 +1,20 @@
|
|||||||
|
package com.jeluchu.features.radiostations.mappers
|
||||||
|
|
||||||
|
import com.jeluchu.core.extensions.getStringSafe
|
||||||
|
import com.jeluchu.features.radiostations.models.RadioStationEntity
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import org.bson.Document
|
||||||
|
|
||||||
|
fun documentToRadioStation(doc: Document) = RadioStationEntity(
|
||||||
|
name = doc.getStringSafe("name"),
|
||||||
|
image = doc.getStringSafe("image"),
|
||||||
|
genre = doc.getStringSafe("genre"),
|
||||||
|
stream = doc.getStringSafe("stream"),
|
||||||
|
website = doc.getStringSafe("website")
|
||||||
|
)
|
||||||
|
|
||||||
|
fun List<Document>.documentStationsMapper(): String {
|
||||||
|
val directory = map { documentToRadioStation(it) }
|
||||||
|
return Json.encodeToString(directory)
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.jeluchu.features.radiostations.models
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class RadioStationEntity(
|
||||||
|
val name: String = "",
|
||||||
|
val image: String = "",
|
||||||
|
val genre: String = "",
|
||||||
|
val stream: String = "",
|
||||||
|
val website: String = ""
|
||||||
|
)
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.jeluchu.features.radiostations.routes
|
||||||
|
|
||||||
|
import com.jeluchu.core.extensions.getToJson
|
||||||
|
import com.jeluchu.core.utils.Routes
|
||||||
|
import com.jeluchu.features.radiostations.services.RadioStationsService
|
||||||
|
import com.mongodb.client.MongoDatabase
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
|
fun Route.radioStationsEndpoints(
|
||||||
|
mongoDatabase: MongoDatabase,
|
||||||
|
service: RadioStationsService = RadioStationsService(mongoDatabase)
|
||||||
|
) = route(Routes.RADIO_STATIONS) {
|
||||||
|
getToJson { service.getStations(call) }
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.jeluchu.features.radiostations.services
|
||||||
|
|
||||||
|
import com.jeluchu.core.utils.Collections
|
||||||
|
import com.jeluchu.features.radiostations.mappers.documentStationsMapper
|
||||||
|
import com.mongodb.client.MongoDatabase
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.server.response.*
|
||||||
|
import io.ktor.server.routing.*
|
||||||
|
|
||||||
|
class RadioStationsService(
|
||||||
|
database: MongoDatabase
|
||||||
|
) {
|
||||||
|
private val radioStations = database.getCollection(Collections.RADIO)
|
||||||
|
|
||||||
|
suspend fun getStations(call: RoutingCall) {
|
||||||
|
val elements = radioStations.find().toList()
|
||||||
|
call.respond(HttpStatusCode.OK, elements.documentStationsMapper())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue