@@ -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- // }
18+ val connectionString: ConnectionString ? = System .getenv(" MONGODB_URI" )?.let {
19+ ConnectionString (" $it ?retryWrites=false" )
20+ }
2121
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>()
25-
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
@@ -52,15 +50,15 @@ fun main() {
5250 staticResources(" /" , " static" )
5351 route(ShoppingListItem .path) {
5452 get {
55- call.respond(collection)
53+ call.respond(collection.find().toList() )
5654 }
5755 post {
58- collection.add (call.receive<ShoppingListItem >())
56+ collection.insertOne (call.receive<ShoppingListItem >())
5957 call.respond(HttpStatusCode .OK )
6058 }
6159 delete(" /{id}" ) {
6260 val id = call.parameters[" id" ]?.toInt() ? : error(" Invalid delete request" )
63- collection.removeIf { it. id == id }
61+ collection.deleteOne( ShoppingListItem :: id eq id)
6462 call.respond(HttpStatusCode .OK )
6563 }
6664 }
0 commit comments