Skip to content

Commit

Permalink
Uncomment server logic for MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
JSMonk committed Jan 5, 2024
1 parent ce02939 commit 5cc51be
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/jvmMain/kotlin/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.plugins.cors.routing.*
import org.litote.kmongo.reactivestreams.KMongo

//val connectionString: ConnectionString? = System.getenv("MONGODB_URI")?.let {
// ConnectionString("$it?retryWrites=false")
//}

//val client = if (connectionString != null) KMongo.createClient(connectionString).coroutine else KMongo.createClient().coroutine
//val database = client.getDatabase(connectionString?.database ?: "shoppingList")
//val collection = database.getCollection<ShoppingListItem>()
val connectionString: ConnectionString? = System.getenv("MONGODB_URI")?.let {
ConnectionString("$it?retryWrites=false")
}

val collection = mutableListOf<ShoppingListItem>();
val client = if (connectionString != null) KMongo.createClient(connectionString).coroutine else KMongo.createClient().coroutine
val database = client.getDatabase(connectionString?.database ?: "shoppingList")
val collection = database.getCollection<ShoppingListItem>()

fun main() {
val port = System.getenv("PORT")?.toInt() ?: 9090
Expand All @@ -46,15 +44,15 @@ fun main() {
staticResources("/", "static")
route(ShoppingListItem.path) {
get {
call.respond(collection)
call.respond(collection.find().toList())
}
post {
collection.add(call.receive<ShoppingListItem>())
collection.insertOne(call.receive<ShoppingListItem>())
call.respond(HttpStatusCode.OK)
}
delete("/{id}") {
val id = call.parameters["id"]?.toInt() ?: error("Invalid delete request")
collection.removeIf { it.id == id }
collection.deleteOne(ShoppingListItem::id eq id)
call.respond(HttpStatusCode.OK)
}
}
Expand Down

0 comments on commit 5cc51be

Please sign in to comment.