Merge http4k
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import org.flywaydb.core.Flyway
|
||||
import javax.sql.DataSource
|
||||
|
||||
internal class DbMigrationsImpl(private val dataSource: DataSource) : DbMigrations {
|
||||
override fun migrate() {
|
||||
Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.load()
|
||||
.migrate()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||
import be.simplenotes.domain.usecases.repositories.UserRepository
|
||||
import be.simplenotes.persistance.notes.NoteRepositoryImpl
|
||||
import be.simplenotes.persistance.users.UserRepositoryImpl
|
||||
import be.simplenotes.shared.config.DataSourceConfig
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import me.liuwj.ktorm.database.*
|
||||
import org.koin.dsl.module
|
||||
import javax.sql.DataSource
|
||||
|
||||
interface DbMigrations {
|
||||
fun migrate()
|
||||
}
|
||||
|
||||
private fun hikariDataSource(conf: DataSourceConfig): HikariDataSource {
|
||||
val hikariConfig = HikariConfig().also {
|
||||
it.jdbcUrl = conf.jdbcUrl
|
||||
it.driverClassName = conf.driverClassName
|
||||
it.username = conf.username
|
||||
it.password = conf.password
|
||||
it.maximumPoolSize = conf.maximumPoolSize
|
||||
it.connectionTimeout = conf.connectionTimeout
|
||||
}
|
||||
return HikariDataSource(hikariConfig)
|
||||
}
|
||||
|
||||
val persistanceModule = module {
|
||||
single<UserRepository> { UserRepositoryImpl(get()) }
|
||||
single<NoteRepository> { NoteRepositoryImpl(get()) }
|
||||
single<DbMigrations> { DbMigrationsImpl(get()) }
|
||||
single<DataSource> { hikariDataSource(get()) }
|
||||
single { Database.connect(get<DataSource>()) }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package be.simplenotes.persistance.extensions
|
||||
|
||||
import me.liuwj.ktorm.schema.*
|
||||
import java.nio.ByteBuffer
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.ResultSet
|
||||
import java.sql.Types
|
||||
import java.util.*
|
||||
|
||||
internal class UuidBinarySqlType : SqlType<UUID>(Types.BINARY, typeName = "uuidBinary") {
|
||||
override fun doGetResult(rs: ResultSet, index: Int): UUID? {
|
||||
val value = rs.getBytes(index) ?: return null
|
||||
return ByteBuffer.wrap(value).let { b -> UUID(b.long, b.long) }
|
||||
}
|
||||
|
||||
override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: UUID) {
|
||||
val bytes = ByteBuffer.allocate(16)
|
||||
.putLong(parameter.mostSignificantBits)
|
||||
.putLong(parameter.leastSignificantBits)
|
||||
.array()
|
||||
ps.setBytes(index, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <E : Any> BaseTable<E>.uuidBinary(name: String) = registerColumn(name, UuidBinarySqlType())
|
||||
@@ -0,0 +1,123 @@
|
||||
package be.simplenotes.persistance.notes
|
||||
|
||||
import be.simplenotes.domain.model.Note
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
|
||||
|
||||
@Throws(IllegalArgumentException::class)
|
||||
override fun findAll(userId: Int, limit: Int, offset: Int): List<PersistedNoteMetadata> {
|
||||
require(limit > 0) { "limit should be positive" }
|
||||
require(offset >= 0) { "offset should not be negative" }
|
||||
|
||||
val notes = db.notes
|
||||
.filterColumns { listOf(it.uuid, it.title, it.updatedAt) }
|
||||
.filter { it.userId eq userId }
|
||||
.sortedByDescending { it.updatedAt }
|
||||
.take(limit)
|
||||
.drop(offset)
|
||||
.toList()
|
||||
|
||||
if (notes.isEmpty()) return emptyList()
|
||||
|
||||
val uuids = notes.map { note -> note.uuid }
|
||||
|
||||
val tagsByUuid = db.tags
|
||||
.filterColumns { listOf(it.noteUuid, it.name) }
|
||||
.filter { it.noteUuid inList uuids }
|
||||
.groupByTo(HashMap(), { it.note.uuid }, { it.name })
|
||||
|
||||
return notes.map { note ->
|
||||
val tags = tagsByUuid[note.uuid] ?: emptyList()
|
||||
note.toPersistedMetadata(tags)
|
||||
}
|
||||
}
|
||||
|
||||
override fun exists(userId: Int, uuid: UUID): Boolean {
|
||||
return db.notes.any { (it.userId eq userId) and (it.uuid eq uuid) }
|
||||
}
|
||||
|
||||
override fun create(userId: Int, note: Note): PersistedNote {
|
||||
val uuid = UUID.randomUUID()
|
||||
val entity = note.toEntity(uuid, userId).apply {
|
||||
this.updatedAt = LocalDateTime.now()
|
||||
}
|
||||
db.useTransaction {
|
||||
db.notes.add(entity)
|
||||
db.batchInsert(Tags) {
|
||||
note.meta.tags.forEach { tagName ->
|
||||
item {
|
||||
it.noteUuid to uuid
|
||||
it.name to tagName
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entity.toPersistedNote(note.meta.tags)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun find(userId: Int, uuid: UUID): PersistedNote? {
|
||||
val note = db.notes
|
||||
.filterColumns { it.columns - it.userId }
|
||||
.filter { it.uuid eq uuid }
|
||||
.find { it.userId eq userId }
|
||||
?: return null
|
||||
|
||||
val tags = db.tags
|
||||
.filter { it.noteUuid eq uuid }
|
||||
.mapColumns { it.name } as List<String>
|
||||
|
||||
return note.toPersistedNote(tags)
|
||||
}
|
||||
|
||||
override fun update(userId: Int, uuid: UUID, note: Note): PersistedNote? {
|
||||
db.useTransaction {
|
||||
val currentNote = db.notes
|
||||
.find { it.uuid eq uuid and (it.userId eq userId) }
|
||||
?: return null
|
||||
|
||||
currentNote.title = note.meta.title
|
||||
currentNote.markdown = note.markdown
|
||||
currentNote.html = note.html
|
||||
currentNote.updatedAt = LocalDateTime.now()
|
||||
currentNote.flushChanges()
|
||||
|
||||
// delete all tags
|
||||
db.delete(Tags) {
|
||||
it.noteUuid eq uuid
|
||||
}
|
||||
|
||||
// put new ones
|
||||
note.meta.tags.forEach { tagName ->
|
||||
db.insert(Tags) {
|
||||
it.name to tagName
|
||||
it.noteUuid to uuid
|
||||
}
|
||||
}
|
||||
return currentNote.toPersistedNote(note.meta.tags)
|
||||
}
|
||||
}
|
||||
|
||||
override fun delete(userId: Int, uuid: UUID): Boolean = db.useTransaction {
|
||||
db.delete(Notes) { it.uuid eq uuid and (it.userId eq userId) } == 1
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun getTags(userId: Int): List<String> {
|
||||
return db.sequenceOf(Tags)
|
||||
.filter { it.note.userId eq userId }
|
||||
.mapColumns(isDistinct = true) { it.name } as List<String>
|
||||
}
|
||||
|
||||
override fun count(userId: Int) = db.notes.count { it.userId eq userId }
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package be.simplenotes.persistance.notes
|
||||
|
||||
import be.simplenotes.domain.model.Note
|
||||
import be.simplenotes.domain.model.NoteMetadata
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.persistance.extensions.uuidBinary
|
||||
import be.simplenotes.persistance.users.UserEntity
|
||||
import be.simplenotes.persistance.users.Users
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import me.liuwj.ktorm.schema.*
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
internal open class Notes(alias: String?) : Table<NoteEntity>("Notes", alias) {
|
||||
companion object : Notes(null)
|
||||
|
||||
override fun aliased(alias: String) = Notes(alias)
|
||||
|
||||
val uuid = uuidBinary("uuid").primaryKey().bindTo { it.uuid }
|
||||
val title = varchar("title").bindTo { it.title }
|
||||
val markdown = text("markdown").bindTo { it.markdown }
|
||||
val html = text("html").bindTo { it.html }
|
||||
val userId = int("user_id").references(Users) { it.user }
|
||||
val updatedAt = datetime("updated_at").bindTo { it.updatedAt }
|
||||
val user get() = userId.referenceTable as Users
|
||||
}
|
||||
|
||||
internal val Database.notes get() = this.sequenceOf(Notes, withReferences = false)
|
||||
|
||||
internal interface NoteEntity : Entity<NoteEntity> {
|
||||
companion object : Entity.Factory<NoteEntity>()
|
||||
|
||||
var uuid: UUID
|
||||
var title: String
|
||||
var markdown: String
|
||||
var html: String
|
||||
var updatedAt: LocalDateTime
|
||||
|
||||
var user: UserEntity
|
||||
}
|
||||
|
||||
internal fun NoteEntity.toPersistedMetadata(tags: List<String>) = PersistedNoteMetadata(title, tags, updatedAt, uuid)
|
||||
|
||||
internal fun NoteEntity.toPersistedNote(tags: List<String>) = PersistedNote(NoteMetadata(title, tags), markdown, html, updatedAt, uuid)
|
||||
|
||||
internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
|
||||
val note = this
|
||||
return NoteEntity {
|
||||
this.title = note.meta.title
|
||||
this.markdown = note.markdown
|
||||
this.html = note.html
|
||||
this.uuid = uuid
|
||||
this.user["id"] = userId
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package be.simplenotes.persistance.notes
|
||||
|
||||
import be.simplenotes.persistance.extensions.uuidBinary
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import me.liuwj.ktorm.schema.*
|
||||
|
||||
internal open class Tags(alias: String?) : Table<TagEntity>("Tags", alias) {
|
||||
companion object : Tags(null)
|
||||
|
||||
override fun aliased(alias: String) = Tags(alias)
|
||||
|
||||
val id = int("id").primaryKey().bindTo { it.id }
|
||||
val name = varchar("name").bindTo { it.name }
|
||||
val noteUuid = uuidBinary("note_uuid").references(Notes) { it.note }
|
||||
val note get() = noteUuid.referenceTable as Notes
|
||||
}
|
||||
|
||||
internal val Database.tags get() = this.sequenceOf(Tags, withReferences = false)
|
||||
|
||||
internal interface TagEntity : Entity<TagEntity> {
|
||||
companion object : Entity.Factory<TagEntity>()
|
||||
|
||||
val id: Int
|
||||
var name: String
|
||||
var note: NoteEntity
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package be.simplenotes.persistance.users
|
||||
|
||||
import be.simplenotes.domain.model.PersistedUser
|
||||
import be.simplenotes.domain.model.User
|
||||
import be.simplenotes.domain.usecases.repositories.UserRepository
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
|
||||
internal class UserRepositoryImpl(private val db: Database) : UserRepository {
|
||||
override fun create(user: User): PersistedUser? {
|
||||
return try {
|
||||
db.useTransaction { db.users.add(user.toEntity()) }
|
||||
find(user.username)
|
||||
} catch (e: SQLIntegrityConstraintViolationException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun find(username: String) = db.users.find { it.username eq username }?.toPersistedUser()
|
||||
override fun find(id: Int) = db.users.find { it.id eq id }?.toPersistedUser()
|
||||
override fun exists(username: String) = db.users.any { it.username eq username }
|
||||
override fun exists(id: Int) = db.users.any { it.id eq id }
|
||||
override fun delete(id: Int) = db.useTransaction { db.users.find { it.id eq id }?.delete() == 1 }
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package be.simplenotes.persistance.users
|
||||
|
||||
import be.simplenotes.domain.model.PersistedUser
|
||||
import be.simplenotes.domain.model.User
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import me.liuwj.ktorm.schema.*
|
||||
|
||||
internal open class Users(alias: String?) : Table<UserEntity>("Users", alias) {
|
||||
companion object : Users(null)
|
||||
|
||||
override fun aliased(alias: String) = Users(alias)
|
||||
|
||||
val id = int("id").primaryKey().bindTo { it.id }
|
||||
val username = varchar("username").bindTo { it.username }
|
||||
val password = varchar("password").bindTo { it.password }
|
||||
}
|
||||
|
||||
internal interface UserEntity : Entity<UserEntity> {
|
||||
companion object : Entity.Factory<UserEntity>()
|
||||
|
||||
val id: Int
|
||||
var username: String
|
||||
var password: String
|
||||
}
|
||||
|
||||
internal fun UserEntity.toPersistedUser() = PersistedUser(username, password, id)
|
||||
|
||||
internal fun PersistedUser.toEntity(): UserEntity {
|
||||
val user = this
|
||||
return UserEntity {
|
||||
this.username = user.username
|
||||
this.password = user.password
|
||||
}.apply { this["id"] = user.id }
|
||||
}
|
||||
|
||||
internal fun User.toEntity(): UserEntity {
|
||||
val user = this
|
||||
return UserEntity {
|
||||
this.username = user.username
|
||||
this.password = user.password
|
||||
}
|
||||
}
|
||||
|
||||
internal val Database.users get() = this.sequenceOf(Users, withReferences = false)
|
||||
Reference in New Issue
Block a user