import io.ktor.sessions.*
import io.ktor.util.*
import kotlinx.coroutines.coroutineScope
-import kotlinx.coroutines.flow.singleOrNull
import kotlinx.coroutines.launch
import kotlinx.html.*
import org.litote.kmongo.eq
val originAddress = request.origin.remoteHost
val userAgent = request.userAgent()
if (userAgent != null && credentials.name.isValidUsername() && credentials.password == TEST_PASSWORD) {
- val user = User.select(User::username eq credentials.name).singleOrNull()
+ val user = User.locate(User::username eq credentials.name)
?: User(username = credentials.name).also { User.put(it) }
UserSession(
}
}
-sealed interface FullDocDelta<T : DataDocument<T>> {
- val doc: T
-}
-
-sealed class DocumentDelta<T : DataDocument<T>> {
- data class Created<T : DataDocument<T>>(override val doc: T) : DocumentDelta<T>(), FullDocDelta<T>
- data class Updated<T : DataDocument<T>>(override val doc: T) : DocumentDelta<T>(), FullDocDelta<T>
- data class Deleted<T : DataDocument<T>>(val id: Id<T>) : DocumentDelta<T>()
-}
-
interface DocumentTable<T : DataDocument<T>> {
suspend fun index(vararg properties: KProperty1<T, *>)
suspend fun unique(vararg properties: KProperty1<T, *>)
suspend fun del(id: Id<T>)
suspend fun select(bson: Bson): Flow<T>
+ suspend fun locate(bson: Bson): T?
suspend fun update(where: Bson, set: Bson)
suspend fun remove(where: Bson)
return collection().find(bson).toFlow()
}
+ override suspend fun locate(bson: Bson): T? {
+ return collection().findOne(bson)
+ }
+
override suspend fun update(where: Bson, set: Bson) {
collection().updateMany(where, set)
}
import io.ktor.application.*
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
-import kotlinx.coroutines.flow.singleOrNull
import kotlinx.coroutines.flow.toList
import kotlinx.html.*
import org.litote.kmongo.eq
suspend fun ApplicationCall.userPage(): HTML.() -> Unit {
val username = parameters["name"]!!
- val user = User.select(User::username eq username).singleOrNull()!!
+ val user = User.locate(User::username eq username)!!
val isCurrentUser = user.id == getUserSession()?.user