diff --git a/api/src/extensions/KtormExtensions.kt b/api/src/extensions/KtormExtensions.kt index f17ba49..75b07a5 100644 --- a/api/src/extensions/KtormExtensions.kt +++ b/api/src/extensions/KtormExtensions.kt @@ -1,5 +1,10 @@ package be.vandewalleh.extensions +import be.vandewalleh.tables.Notes +import be.vandewalleh.tables.Tags +import be.vandewalleh.tables.Users +import me.liuwj.ktorm.database.* +import me.liuwj.ktorm.entity.* import me.liuwj.ktorm.schema.* import java.nio.ByteBuffer import java.sql.PreparedStatement @@ -25,3 +30,7 @@ class UuidBinarySqlType : SqlType(Types.BINARY, typeName = "uuidBinary fun BaseTable.uuidBinary(name: String): Column { return registerColumn(name, UuidBinarySqlType()) } + +val Database.users get() = this.sequenceOf(Users, withReferences = false) +val Database.notes get() = this.sequenceOf(Notes, withReferences = false) +val Database.tags get() = this.sequenceOf(Tags, withReferences = false) diff --git a/api/src/services/NoteService.kt b/api/src/services/NoteService.kt index 18bce91..16ba984 100644 --- a/api/src/services/NoteService.kt +++ b/api/src/services/NoteService.kt @@ -2,6 +2,8 @@ package be.vandewalleh.services import be.vandewalleh.entities.Note import be.vandewalleh.extensions.launchIo +import be.vandewalleh.extensions.notes +import be.vandewalleh.extensions.tags import be.vandewalleh.tables.Notes import be.vandewalleh.tables.Tags import me.liuwj.ktorm.database.* @@ -9,6 +11,7 @@ import me.liuwj.ktorm.dsl.* import me.liuwj.ktorm.entity.* import java.time.LocalDateTime import java.util.* +import kotlin.collections.HashMap /** * service to handle database queries at the Notes level. @@ -23,13 +26,13 @@ class NoteService(private val db: Database) { var previous: LocalDateTime? = null if (after != null) { - previous = db.sequenceOf(Notes, withReferences = false) + previous = db.notes .filter { it.userId eq userId and (it.uuid eq after) } .mapColumns { it.updatedAt } .firstOrNull() ?: return@launchIo emptyList() } - val notes = db.sequenceOf(Notes, withReferences = false) + val notes = db.notes .filterColumns { it.columns - it.userId } .filter { if (previous == null) it.userId eq userId @@ -41,13 +44,11 @@ class NoteService(private val db: Database) { if (notes.isEmpty()) return@launchIo emptyList() - val allTags = - db.sequenceOf(Tags, withReferences = false) + val tagsByUuid = + db.tags .filterColumns { listOf(it.noteUuid, it.name) } .filter { it.noteUuid inList notes.map { note -> note.uuid } } - .toList() - - val tagsByUuid = allTags.groupByTo(HashMap(), { it.note.uuid }, { it.name }) + .groupByTo(HashMap(), { it.note.uuid }, { it.name }) notes.forEach { val tags = tagsByUuid[it.uuid] @@ -58,7 +59,7 @@ class NoteService(private val db: Database) { } suspend fun exists(userId: Int, uuid: UUID) = launchIo { - db.sequenceOf(Notes, withReferences = false).any { it.userId eq userId and (it.uuid eq uuid) } + db.notes.any { (it.userId eq userId) and (it.uuid eq uuid) } } suspend fun create(userId: Int, note: Note): Note = launchIo { @@ -69,7 +70,7 @@ class NoteService(private val db: Database) { this.updatedAt = LocalDateTime.now() } db.useTransaction { - db.sequenceOf(Notes).add(newNote) + db.notes.add(newNote) db.batchInsert(Tags) { note.tags.forEach { tagName -> item { @@ -85,7 +86,7 @@ class NoteService(private val db: Database) { @Suppress("UNCHECKED_CAST") suspend fun find(userId: Int, noteUuid: UUID): Note? = launchIo { val note = - db.sequenceOf(Notes, withReferences = false) + db.notes .filterColumns { it.columns - it.userId } .filter { it.uuid eq noteUuid } .find { it.userId eq userId } @@ -103,7 +104,7 @@ class NoteService(private val db: Database) { if (note["uuid"] == null) error("UUID is required") db.useTransaction { - val currentNote = db.sequenceOf(Notes, withReferences = false) + val currentNote = db.notes .find { it.uuid eq note.uuid and (it.userId eq userId) } ?: return@launchIo false diff --git a/api/src/services/UserService.kt b/api/src/services/UserService.kt index 973fc13..0798797 100644 --- a/api/src/services/UserService.kt +++ b/api/src/services/UserService.kt @@ -2,6 +2,7 @@ package be.vandewalleh.services import be.vandewalleh.entities.User import be.vandewalleh.extensions.launchIo +import be.vandewalleh.extensions.users import be.vandewalleh.features.PasswordHash import be.vandewalleh.tables.Users import me.liuwj.ktorm.database.* @@ -18,22 +19,19 @@ class UserService(private val db: Database, private val passwordHash: PasswordHa * returns a user from it's username if found or null */ suspend fun find(username: String): User? = launchIo { - db.sequenceOf(Users, withReferences = false) - .find { it.username eq username } + db.users.find { it.username eq username } } suspend fun find(id: Int): User? = launchIo { - db.sequenceOf(Users, withReferences = false) - .find { it.id eq id } + db.users.find { it.id eq id } } suspend fun exists(username: String) = launchIo { - db.sequenceOf(Users, withReferences = false) - .any { it.username eq username } + db.users.any { it.username eq username } } suspend fun exists(userId: Int) = launchIo { - db.sequenceOf(Users).any { it.id eq userId } + db.users.any { it.id eq userId } } suspend fun create(username: String, password: String): User? { @@ -45,7 +43,7 @@ class UserService(private val db: Database, private val passwordHash: PasswordHa return try { launchIo { db.useTransaction { - db.sequenceOf(Users).add(newUser) + db.users.add(newUser) newUser } }