@@ -15,15 +15,13 @@ import io.ktor.serialization.kotlinx.json.*
1515import io.ktor.server.plugins.cors.routing.*
1616import org.litote.kmongo.reactivestreams.KMongo
1717
18- // val connectionString: ConnectionString? = System.getenv("MONGODB_URI")?.let {
19- // ConnectionString("$it?retryWrites=false")
20- // }
21-
22- // val client = if (connectionString != null) KMongo.createClient(connectionString).coroutine else KMongo.createClient().coroutine
23- // val database = client.getDatabase(connectionString?.database ?: "shoppingList")
24- // val collection = database.getCollection<ShoppingListItem>()
18+ val connectionString: ConnectionString ? = System .getenv(" MONGODB_URI" )?.let {
19+ ConnectionString (" $it ?retryWrites=false" )
20+ }
2521
26- val collection = mutableListOf<ShoppingListItem >();
22+ val client = if (connectionString != null ) KMongo .createClient(connectionString).coroutine else KMongo .createClient().coroutine
23+ val database = client.getDatabase(connectionString?.database ? : " shoppingList" )
24+ val collection = database.getCollection<ShoppingListItem >()
2725
2826fun main () {
2927 val port = System .getenv(" PORT" )?.toInt() ? : 9090
@@ -46,15 +44,15 @@ fun main() {
4644 staticResources(" /" , " static" )
4745 route(ShoppingListItem .path) {
4846 get {
49- call.respond(collection)
47+ call.respond(collection.find().toList() )
5048 }
5149 post {
52- collection.add (call.receive<ShoppingListItem >())
50+ collection.insertOne (call.receive<ShoppingListItem >())
5351 call.respond(HttpStatusCode .OK )
5452 }
5553 delete(" /{id}" ) {
5654 val id = call.parameters[" id" ]?.toInt() ? : error(" Invalid delete request" )
57- collection.removeIf { it. id == id }
55+ collection.deleteOne( ShoppingListItem :: id eq id)
5856 call.respond(HttpStatusCode .OK )
5957 }
6058 }
0 commit comments