Clean ktorm sequences
This commit is contained in:
parent
9212d23933
commit
70bb22c3dd
@ -1,5 +1,10 @@
|
|||||||
package be.vandewalleh.extensions
|
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 me.liuwj.ktorm.schema.*
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.sql.PreparedStatement
|
import java.sql.PreparedStatement
|
||||||
@ -25,3 +30,7 @@ class UuidBinarySqlType : SqlType<JavaUUID>(Types.BINARY, typeName = "uuidBinary
|
|||||||
fun <E : Any> BaseTable<E>.uuidBinary(name: String): Column<JavaUUID> {
|
fun <E : Any> BaseTable<E>.uuidBinary(name: String): Column<JavaUUID> {
|
||||||
return registerColumn(name, UuidBinarySqlType())
|
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)
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package be.vandewalleh.services
|
|||||||
|
|
||||||
import be.vandewalleh.entities.Note
|
import be.vandewalleh.entities.Note
|
||||||
import be.vandewalleh.extensions.launchIo
|
import be.vandewalleh.extensions.launchIo
|
||||||
|
import be.vandewalleh.extensions.notes
|
||||||
|
import be.vandewalleh.extensions.tags
|
||||||
import be.vandewalleh.tables.Notes
|
import be.vandewalleh.tables.Notes
|
||||||
import be.vandewalleh.tables.Tags
|
import be.vandewalleh.tables.Tags
|
||||||
import me.liuwj.ktorm.database.*
|
import me.liuwj.ktorm.database.*
|
||||||
@ -9,6 +11,7 @@ import me.liuwj.ktorm.dsl.*
|
|||||||
import me.liuwj.ktorm.entity.*
|
import me.liuwj.ktorm.entity.*
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.collections.HashMap
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* service to handle database queries at the Notes level.
|
* service to handle database queries at the Notes level.
|
||||||
@ -23,13 +26,13 @@ class NoteService(private val db: Database) {
|
|||||||
var previous: LocalDateTime? = null
|
var previous: LocalDateTime? = null
|
||||||
|
|
||||||
if (after != null) {
|
if (after != null) {
|
||||||
previous = db.sequenceOf(Notes, withReferences = false)
|
previous = db.notes
|
||||||
.filter { it.userId eq userId and (it.uuid eq after) }
|
.filter { it.userId eq userId and (it.uuid eq after) }
|
||||||
.mapColumns { it.updatedAt }
|
.mapColumns { it.updatedAt }
|
||||||
.firstOrNull() ?: return@launchIo emptyList()
|
.firstOrNull() ?: return@launchIo emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
val notes = db.sequenceOf(Notes, withReferences = false)
|
val notes = db.notes
|
||||||
.filterColumns { it.columns - it.userId }
|
.filterColumns { it.columns - it.userId }
|
||||||
.filter {
|
.filter {
|
||||||
if (previous == null) it.userId eq userId
|
if (previous == null) it.userId eq userId
|
||||||
@ -41,13 +44,11 @@ class NoteService(private val db: Database) {
|
|||||||
|
|
||||||
if (notes.isEmpty()) return@launchIo emptyList()
|
if (notes.isEmpty()) return@launchIo emptyList()
|
||||||
|
|
||||||
val allTags =
|
val tagsByUuid =
|
||||||
db.sequenceOf(Tags, withReferences = false)
|
db.tags
|
||||||
.filterColumns { listOf(it.noteUuid, it.name) }
|
.filterColumns { listOf(it.noteUuid, it.name) }
|
||||||
.filter { it.noteUuid inList notes.map { note -> note.uuid } }
|
.filter { it.noteUuid inList notes.map { note -> note.uuid } }
|
||||||
.toList()
|
.groupByTo(HashMap(), { it.note.uuid }, { it.name })
|
||||||
|
|
||||||
val tagsByUuid = allTags.groupByTo(HashMap(), { it.note.uuid }, { it.name })
|
|
||||||
|
|
||||||
notes.forEach {
|
notes.forEach {
|
||||||
val tags = tagsByUuid[it.uuid]
|
val tags = tagsByUuid[it.uuid]
|
||||||
@ -58,7 +59,7 @@ class NoteService(private val db: Database) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun exists(userId: Int, uuid: UUID) = launchIo {
|
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 {
|
suspend fun create(userId: Int, note: Note): Note = launchIo {
|
||||||
@ -69,7 +70,7 @@ class NoteService(private val db: Database) {
|
|||||||
this.updatedAt = LocalDateTime.now()
|
this.updatedAt = LocalDateTime.now()
|
||||||
}
|
}
|
||||||
db.useTransaction {
|
db.useTransaction {
|
||||||
db.sequenceOf(Notes).add(newNote)
|
db.notes.add(newNote)
|
||||||
db.batchInsert(Tags) {
|
db.batchInsert(Tags) {
|
||||||
note.tags.forEach { tagName ->
|
note.tags.forEach { tagName ->
|
||||||
item {
|
item {
|
||||||
@ -85,7 +86,7 @@ class NoteService(private val db: Database) {
|
|||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
suspend fun find(userId: Int, noteUuid: UUID): Note? = launchIo {
|
suspend fun find(userId: Int, noteUuid: UUID): Note? = launchIo {
|
||||||
val note =
|
val note =
|
||||||
db.sequenceOf(Notes, withReferences = false)
|
db.notes
|
||||||
.filterColumns { it.columns - it.userId }
|
.filterColumns { it.columns - it.userId }
|
||||||
.filter { it.uuid eq noteUuid }
|
.filter { it.uuid eq noteUuid }
|
||||||
.find { it.userId eq userId }
|
.find { it.userId eq userId }
|
||||||
@ -103,7 +104,7 @@ class NoteService(private val db: Database) {
|
|||||||
if (note["uuid"] == null) error("UUID is required")
|
if (note["uuid"] == null) error("UUID is required")
|
||||||
|
|
||||||
db.useTransaction {
|
db.useTransaction {
|
||||||
val currentNote = db.sequenceOf(Notes, withReferences = false)
|
val currentNote = db.notes
|
||||||
.find { it.uuid eq note.uuid and (it.userId eq userId) }
|
.find { it.uuid eq note.uuid and (it.userId eq userId) }
|
||||||
?: return@launchIo false
|
?: return@launchIo false
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package be.vandewalleh.services
|
|||||||
|
|
||||||
import be.vandewalleh.entities.User
|
import be.vandewalleh.entities.User
|
||||||
import be.vandewalleh.extensions.launchIo
|
import be.vandewalleh.extensions.launchIo
|
||||||
|
import be.vandewalleh.extensions.users
|
||||||
import be.vandewalleh.features.PasswordHash
|
import be.vandewalleh.features.PasswordHash
|
||||||
import be.vandewalleh.tables.Users
|
import be.vandewalleh.tables.Users
|
||||||
import me.liuwj.ktorm.database.*
|
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
|
* returns a user from it's username if found or null
|
||||||
*/
|
*/
|
||||||
suspend fun find(username: String): User? = launchIo {
|
suspend fun find(username: String): User? = launchIo {
|
||||||
db.sequenceOf(Users, withReferences = false)
|
db.users.find { it.username eq username }
|
||||||
.find { it.username eq username }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun find(id: Int): User? = launchIo {
|
suspend fun find(id: Int): User? = launchIo {
|
||||||
db.sequenceOf(Users, withReferences = false)
|
db.users.find { it.id eq id }
|
||||||
.find { it.id eq id }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun exists(username: String) = launchIo {
|
suspend fun exists(username: String) = launchIo {
|
||||||
db.sequenceOf(Users, withReferences = false)
|
db.users.any { it.username eq username }
|
||||||
.any { it.username eq username }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun exists(userId: Int) = launchIo {
|
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? {
|
suspend fun create(username: String, password: String): User? {
|
||||||
@ -45,7 +43,7 @@ class UserService(private val db: Database, private val passwordHash: PasswordHa
|
|||||||
return try {
|
return try {
|
||||||
launchIo {
|
launchIo {
|
||||||
db.useTransaction {
|
db.useTransaction {
|
||||||
db.sequenceOf(Users).add(newUser)
|
db.users.add(newUser)
|
||||||
newUser
|
newUser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user