Fix typos
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package be.simplenotes.persistence
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import be.simplenotes.persistence.utils.DbType
|
||||
import be.simplenotes.persistence.utils.type
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.database.asIterable
|
||||
import me.liuwj.ktorm.database.use
|
||||
import java.sql.SQLException
|
||||
import javax.inject.Singleton
|
||||
|
||||
interface DbHealthCheck {
|
||||
fun isOk(): Boolean
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class DbHealthCheckImpl(
|
||||
private val db: Database,
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
) : DbHealthCheck {
|
||||
override fun isOk() = if (dataSourceConfig.type() == DbType.H2) true
|
||||
else try {
|
||||
db.useConnection { connection ->
|
||||
connection.prepareStatement("""SHOW DATABASES""").use {
|
||||
it.executeQuery().asIterable().map { it.getString(1) }
|
||||
}
|
||||
}.any { it in dataSourceConfig.jdbcUrl }
|
||||
} catch (e: SQLException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package be.simplenotes.persistence
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import be.simplenotes.persistence.utils.DbType
|
||||
import be.simplenotes.persistence.utils.type
|
||||
import org.flywaydb.core.Flyway
|
||||
import javax.inject.Singleton
|
||||
import javax.sql.DataSource
|
||||
|
||||
interface DbMigrations {
|
||||
fun migrate()
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class DbMigrationsImpl(
|
||||
private val dataSource: DataSource,
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
) : DbMigrations {
|
||||
override fun migrate() {
|
||||
|
||||
val migrationDir = when (dataSourceConfig.type()) {
|
||||
DbType.H2 -> "db/migration/other"
|
||||
DbType.MariaDb -> "db/migration/mariadb"
|
||||
}
|
||||
|
||||
Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.locations(migrationDir)
|
||||
.load()
|
||||
.migrate()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package be.simplenotes.persistence
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import io.micronaut.context.annotation.Bean
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import javax.inject.Singleton
|
||||
import javax.sql.DataSource
|
||||
|
||||
@Factory
|
||||
class PersistenceModule {
|
||||
|
||||
@Singleton
|
||||
internal fun database(migrations: DbMigrations, dataSource: DataSource): Database {
|
||||
migrations.migrate()
|
||||
return Database.connect(dataSource)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Bean(preDestroy = "close")
|
||||
internal fun dataSource(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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package be.simplenotes.persistence.converters
|
||||
|
||||
import be.simplenotes.persistence.notes.NoteEntity
|
||||
import be.simplenotes.types.*
|
||||
import me.liuwj.ktorm.entity.Entity
|
||||
import org.mapstruct.Mapper
|
||||
import org.mapstruct.Mapping
|
||||
import org.mapstruct.Mappings
|
||||
import org.mapstruct.ReportingPolicy
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Mapper(
|
||||
uses = [NoteEntityFactory::class, UserEntityFactory::class],
|
||||
unmappedTargetPolicy = ReportingPolicy.IGNORE,
|
||||
componentModel = "jsr330"
|
||||
)
|
||||
internal abstract class NoteConverter {
|
||||
|
||||
fun toNote(entity: NoteEntity, tags: Tags) =
|
||||
Note(NoteMetadata(title = entity.title, tags = tags), entity.markdown, entity.html)
|
||||
|
||||
@Mappings(
|
||||
Mapping(target = ".", source = "entity"),
|
||||
Mapping(target = "tags", source = "tags"),
|
||||
)
|
||||
abstract fun toNoteMetadata(entity: NoteEntity, tags: Tags): NoteMetadata
|
||||
|
||||
@Mappings(
|
||||
Mapping(target = ".", source = "entity"),
|
||||
Mapping(target = "tags", source = "tags"),
|
||||
)
|
||||
abstract fun toPersistedNoteMetadata(entity: NoteEntity, tags: Tags): PersistedNoteMetadata
|
||||
|
||||
fun toPersistedNote(entity: NoteEntity, tags: Tags) = PersistedNote(
|
||||
NoteMetadata(title = entity.title, tags = tags),
|
||||
entity.markdown,
|
||||
entity.html,
|
||||
entity.updatedAt,
|
||||
entity.uuid,
|
||||
entity.public
|
||||
)
|
||||
|
||||
@Mappings(
|
||||
Mapping(target = ".", source = "entity"),
|
||||
Mapping(target = "trash", source = "entity.deleted"),
|
||||
Mapping(target = "tags", source = "tags"),
|
||||
)
|
||||
abstract fun toExportedNote(entity: NoteEntity, tags: Tags): ExportedNote
|
||||
|
||||
fun toEntity(note: Note, uuid: UUID, userId: Int, updatedAt: LocalDateTime) = NoteEntity {
|
||||
this.title = note.meta.title
|
||||
this.markdown = note.markdown
|
||||
this.html = note.html
|
||||
this.uuid = uuid
|
||||
this.deleted = false
|
||||
this.public = false
|
||||
this.user.id = userId
|
||||
this.updatedAt = updatedAt
|
||||
}
|
||||
|
||||
@Mappings(
|
||||
Mapping(target = ".", source = "note"),
|
||||
Mapping(target = "updatedAt", source = "updatedAt"),
|
||||
Mapping(target = "uuid", source = "uuid"),
|
||||
Mapping(target = "public", constant = "false"),
|
||||
)
|
||||
abstract fun toPersistedNote(note: Note, updatedAt: LocalDateTime, uuid: UUID): PersistedNote
|
||||
|
||||
abstract fun toEntity(persistedNoteMetadata: PersistedNoteMetadata): NoteEntity
|
||||
|
||||
abstract fun toEntity(noteMetadata: NoteMetadata): NoteEntity
|
||||
|
||||
@Mapping(target = "title", source = "meta.title")
|
||||
abstract fun toEntity(persistedNote: PersistedNote): NoteEntity
|
||||
|
||||
@Mapping(target = "deleted", source = "trash")
|
||||
abstract fun toEntity(exportedNote: ExportedNote): NoteEntity
|
||||
}
|
||||
|
||||
typealias Tags = List<String>
|
||||
|
||||
@Singleton
|
||||
internal class NoteEntityFactory : Entity.Factory<NoteEntity>()
|
||||
@@ -0,0 +1,24 @@
|
||||
package be.simplenotes.persistence.converters
|
||||
|
||||
import be.simplenotes.persistence.users.UserEntity
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import be.simplenotes.types.User
|
||||
import me.liuwj.ktorm.entity.Entity
|
||||
import org.mapstruct.Mapper
|
||||
import org.mapstruct.ReportingPolicy
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Mapper(
|
||||
uses = [UserEntityFactory::class],
|
||||
unmappedTargetPolicy = ReportingPolicy.IGNORE,
|
||||
componentModel = "jsr330"
|
||||
)
|
||||
internal interface UserConverter {
|
||||
fun toUser(userEntity: UserEntity): User
|
||||
fun toPersistedUser(userEntity: UserEntity): PersistedUser
|
||||
fun toEntity(user: User): UserEntity
|
||||
fun toEntity(user: PersistedUser): UserEntity
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class UserEntityFactory : Entity.Factory<UserEntity>()
|
||||
@@ -0,0 +1,26 @@
|
||||
package be.simplenotes.persistence.extensions
|
||||
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.SqlType
|
||||
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,219 @@
|
||||
package be.simplenotes.persistence.notes
|
||||
|
||||
import be.simplenotes.persistence.converters.NoteConverter
|
||||
import be.simplenotes.persistence.repositories.NoteRepository
|
||||
import be.simplenotes.types.ExportedNote
|
||||
import be.simplenotes.types.Note
|
||||
import be.simplenotes.types.PersistedNote
|
||||
import be.simplenotes.types.PersistedNoteMetadata
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.*
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import javax.inject.Singleton
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
@Singleton
|
||||
internal class NoteRepositoryImpl(
|
||||
private val db: Database,
|
||||
private val converter: NoteConverter,
|
||||
) : NoteRepository {
|
||||
|
||||
@Throws(IllegalArgumentException::class)
|
||||
override fun findAll(
|
||||
userId: Int,
|
||||
limit: Int,
|
||||
offset: Int,
|
||||
tag: String?,
|
||||
deleted: Boolean,
|
||||
): List<PersistedNoteMetadata> {
|
||||
require(limit > 0) { "limit should be positive" }
|
||||
require(offset >= 0) { "offset should not be negative" }
|
||||
|
||||
val uuids1: List<UUID>? = if (tag != null) {
|
||||
db.from(Tags)
|
||||
.leftJoin(Notes, on = Notes.uuid eq Tags.noteUuid)
|
||||
.select(Notes.uuid)
|
||||
.where { (Notes.userId eq userId) and (Tags.name eq tag) and (Notes.deleted eq deleted) }
|
||||
.map { it[Notes.uuid]!! }
|
||||
} else null
|
||||
|
||||
var query = db.notes
|
||||
.filterColumns { listOf(it.uuid, it.title, it.updatedAt) }
|
||||
.filter { (it.userId eq userId) and (it.deleted eq deleted) }
|
||||
|
||||
if (uuids1 != null) query = query.filter { it.uuid inList uuids1 }
|
||||
|
||||
val notes = query
|
||||
.sortedByDescending { it.updatedAt }
|
||||
.take(limit)
|
||||
.drop(offset)
|
||||
.toList()
|
||||
|
||||
val tagsByUuid = notes.tagsByUuid()
|
||||
|
||||
return notes.map { note ->
|
||||
val tags = tagsByUuid[note.uuid] ?: emptyList()
|
||||
converter.toPersistedNoteMetadata(note, tags)
|
||||
}
|
||||
}
|
||||
|
||||
override fun exists(userId: Int, uuid: UUID): Boolean {
|
||||
return db.notes.any { (it.userId eq userId) and (it.uuid eq uuid) and (it.deleted eq false) }
|
||||
}
|
||||
|
||||
override fun create(userId: Int, note: Note): PersistedNote {
|
||||
val uuid = UUID.randomUUID()
|
||||
val entity = converter.toEntity(note, uuid, userId, LocalDateTime.now())
|
||||
db.notes.add(entity)
|
||||
db.batchInsert(Tags) {
|
||||
note.meta.tags.forEach { tagName ->
|
||||
item {
|
||||
it.noteUuid to uuid
|
||||
it.name to tagName
|
||||
}
|
||||
}
|
||||
}
|
||||
return converter.toPersistedNote(entity, note.meta.tags)
|
||||
}
|
||||
|
||||
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) and (it.deleted eq false) }
|
||||
?: return null
|
||||
|
||||
val tags = db.from(Tags)
|
||||
.select(Tags.name)
|
||||
.where { Tags.noteUuid eq uuid }
|
||||
.map { it[Tags.name]!! }
|
||||
|
||||
return converter.toPersistedNote(note, tags)
|
||||
}
|
||||
|
||||
override fun update(userId: Int, uuid: UUID, note: Note): PersistedNote? {
|
||||
val now = LocalDateTime.now()
|
||||
val count = db.update(Notes) {
|
||||
it.title to note.meta.title
|
||||
it.markdown to note.markdown
|
||||
it.html to note.html
|
||||
it.updatedAt to now
|
||||
where { (it.uuid eq uuid) and (it.userId eq userId) and (it.deleted eq false) }
|
||||
}
|
||||
|
||||
if (count == 0) return null
|
||||
|
||||
// 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 converter.toPersistedNote(note, now, uuid)
|
||||
}
|
||||
|
||||
override fun delete(userId: Int, uuid: UUID, permanent: Boolean): Boolean {
|
||||
return if (!permanent) {
|
||||
db.update(Notes) {
|
||||
it.deleted to true
|
||||
it.updatedAt to LocalDateTime.now()
|
||||
where { it.userId eq userId and (it.uuid eq uuid) }
|
||||
} == 1
|
||||
} else
|
||||
db.delete(Notes) { it.uuid eq uuid and (it.userId eq userId) } == 1
|
||||
}
|
||||
|
||||
override fun restore(userId: Int, uuid: UUID): Boolean {
|
||||
return db.update(Notes) {
|
||||
it.deleted to false
|
||||
where { (it.userId eq userId) and (it.uuid eq uuid) }
|
||||
} == 1
|
||||
}
|
||||
|
||||
override fun getTags(userId: Int): List<String> =
|
||||
db.from(Tags)
|
||||
.leftJoin(Notes, on = Notes.uuid eq Tags.noteUuid)
|
||||
.selectDistinct(Tags.name)
|
||||
.where { (Notes.userId eq userId) and (Notes.deleted eq false) }
|
||||
.map { it[Tags.name]!! }
|
||||
|
||||
override fun count(userId: Int, tag: String?, deleted: Boolean): Int {
|
||||
return if (tag == null) db.notes.count { (it.userId eq userId) and (Notes.deleted eq deleted) }
|
||||
else db.sequenceOf(Tags).count {
|
||||
(it.name eq tag) and (it.note.userId eq userId) and (it.note.deleted eq deleted)
|
||||
}
|
||||
}
|
||||
|
||||
override fun export(userId: Int): List<ExportedNote> {
|
||||
|
||||
val notes = db.notes
|
||||
.filterColumns { it.columns - it.userId }
|
||||
.filter { it.userId eq userId }
|
||||
.sortedByDescending { it.updatedAt }
|
||||
.toList()
|
||||
|
||||
val tagsByUuid = notes.tagsByUuid()
|
||||
|
||||
return notes.map { note ->
|
||||
val tags = tagsByUuid[note.uuid] ?: emptyList()
|
||||
converter.toExportedNote(note, tags)
|
||||
}
|
||||
}
|
||||
|
||||
override fun findAllDetails(userId: Int): List<PersistedNote> {
|
||||
val notes = db.notes
|
||||
.filterColumns { it.columns - it.deleted }
|
||||
.filter { (it.userId eq userId) and (it.deleted eq false) }
|
||||
.toList()
|
||||
|
||||
val tagsByUuid = notes.tagsByUuid()
|
||||
|
||||
return notes.map { note ->
|
||||
val tags = tagsByUuid[note.uuid] ?: emptyList()
|
||||
converter.toPersistedNote(note, tags)
|
||||
}
|
||||
}
|
||||
|
||||
override fun makePublic(userId: Int, uuid: UUID) = db.update(Notes) {
|
||||
it.public to true
|
||||
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
|
||||
} == 1
|
||||
|
||||
override fun makePrivate(userId: Int, uuid: UUID) = db.update(Notes) {
|
||||
it.public to false
|
||||
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
|
||||
} == 1
|
||||
|
||||
override fun findPublic(uuid: UUID): PersistedNote? {
|
||||
val note = db.notes
|
||||
.filterColumns { it.columns - it.userId }
|
||||
.filter { it.uuid eq uuid }
|
||||
.filter { it.public eq true }
|
||||
.find { it.deleted eq false }
|
||||
?: return null
|
||||
|
||||
val tags = db.from(Tags)
|
||||
.select(Tags.name)
|
||||
.where { Tags.noteUuid eq uuid }
|
||||
.map { it[Tags.name]!! }
|
||||
|
||||
return converter.toPersistedNote(note, tags)
|
||||
}
|
||||
|
||||
private fun List<NoteEntity>.tagsByUuid(): Map<UUID, List<String>> {
|
||||
return if (isEmpty()) emptyMap()
|
||||
else db.tags
|
||||
.filterColumns { listOf(it.noteUuid, it.name) }
|
||||
.filter { it.noteUuid inList map { note -> note.uuid } }
|
||||
.groupByTo(HashMap(), { it.note.uuid }, { it.name })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package be.simplenotes.persistence.notes
|
||||
|
||||
import be.simplenotes.persistence.extensions.uuidBinary
|
||||
import be.simplenotes.persistence.users.UserEntity
|
||||
import be.simplenotes.persistence.users.Users
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.entity.Entity
|
||||
import me.liuwj.ktorm.entity.sequenceOf
|
||||
import me.liuwj.ktorm.schema.*
|
||||
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 deleted = boolean("deleted").bindTo { it.deleted }
|
||||
val public = boolean("public").bindTo { it.public }
|
||||
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 deleted: Boolean
|
||||
var public: Boolean
|
||||
|
||||
var user: UserEntity
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package be.simplenotes.persistence.notes
|
||||
|
||||
import be.simplenotes.persistence.extensions.uuidBinary
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.entity.Entity
|
||||
import me.liuwj.ktorm.entity.sequenceOf
|
||||
import me.liuwj.ktorm.schema.Table
|
||||
import me.liuwj.ktorm.schema.int
|
||||
import me.liuwj.ktorm.schema.varchar
|
||||
|
||||
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,35 @@
|
||||
package be.simplenotes.persistence.repositories
|
||||
|
||||
import be.simplenotes.types.ExportedNote
|
||||
import be.simplenotes.types.Note
|
||||
import be.simplenotes.types.PersistedNote
|
||||
import be.simplenotes.types.PersistedNoteMetadata
|
||||
import java.util.*
|
||||
|
||||
interface NoteRepository {
|
||||
|
||||
fun findAll(
|
||||
userId: Int,
|
||||
limit: Int = 20,
|
||||
offset: Int = 0,
|
||||
tag: String? = null,
|
||||
deleted: Boolean = false
|
||||
): List<PersistedNoteMetadata>
|
||||
|
||||
fun count(userId: Int, tag: String? = null, deleted: Boolean = false): Int
|
||||
fun delete(userId: Int, uuid: UUID, permanent: Boolean = false): Boolean
|
||||
fun restore(userId: Int, uuid: UUID): Boolean
|
||||
|
||||
// These methods only access notes where `Notes.deleted = false`
|
||||
fun getTags(userId: Int): List<String>
|
||||
fun exists(userId: Int, uuid: UUID): Boolean
|
||||
fun create(userId: Int, note: Note): PersistedNote
|
||||
fun find(userId: Int, uuid: UUID): PersistedNote?
|
||||
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
||||
fun export(userId: Int): List<ExportedNote>
|
||||
fun findAllDetails(userId: Int): List<PersistedNote>
|
||||
|
||||
fun makePublic(userId: Int, uuid: UUID): Boolean
|
||||
fun makePrivate(userId: Int, uuid: UUID): Boolean
|
||||
fun findPublic(uuid: UUID): PersistedNote?
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package be.simplenotes.persistence.repositories
|
||||
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import be.simplenotes.types.User
|
||||
|
||||
interface UserRepository {
|
||||
fun create(user: User): PersistedUser?
|
||||
fun find(username: String): PersistedUser?
|
||||
fun find(id: Int): PersistedUser?
|
||||
fun exists(username: String): Boolean
|
||||
fun exists(id: Int): Boolean
|
||||
fun delete(id: Int): Boolean
|
||||
fun findAll(): List<Int>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package be.simplenotes.persistence.transactions
|
||||
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
internal class KtormTransactionService(private val database: Database) : TransactionService {
|
||||
override fun <T> use(block: () -> T) = database.useTransaction { block() }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package be.simplenotes.persistence.transactions
|
||||
|
||||
interface TransactionService {
|
||||
fun <T> use(block: () -> T): T
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package be.simplenotes.persistence.users
|
||||
|
||||
import be.simplenotes.persistence.converters.UserConverter
|
||||
import be.simplenotes.persistence.repositories.UserRepository
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import be.simplenotes.types.User
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
import me.liuwj.ktorm.entity.any
|
||||
import me.liuwj.ktorm.entity.find
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
internal class UserRepositoryImpl(
|
||||
private val db: Database,
|
||||
private val converter: UserConverter,
|
||||
) : UserRepository {
|
||||
override fun create(user: User): PersistedUser? {
|
||||
return try {
|
||||
val id = db.insertAndGenerateKey(Users) {
|
||||
it.username to user.username
|
||||
it.password to user.password
|
||||
} as Int
|
||||
PersistedUser(user.username, user.password, id)
|
||||
} catch (e: SQLIntegrityConstraintViolationException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun find(username: String) = db.users.find { it.username eq username }
|
||||
?.let { converter.toPersistedUser(it) }
|
||||
|
||||
override fun find(id: Int) = db.users.find { it.id eq id }?.let {
|
||||
converter.toPersistedUser(it)
|
||||
}
|
||||
|
||||
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.delete(Users) { it.id eq id } == 1
|
||||
override fun findAll() = db.from(Users).select(Users.id).map { it[Users.id]!! }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package be.simplenotes.persistence.users
|
||||
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.entity.Entity
|
||||
import me.liuwj.ktorm.entity.sequenceOf
|
||||
import me.liuwj.ktorm.schema.Table
|
||||
import me.liuwj.ktorm.schema.int
|
||||
import me.liuwj.ktorm.schema.varchar
|
||||
|
||||
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>()
|
||||
|
||||
var id: Int
|
||||
var username: String
|
||||
var password: String
|
||||
}
|
||||
|
||||
internal val Database.users get() = this.sequenceOf(Users, withReferences = false)
|
||||
@@ -0,0 +1,8 @@
|
||||
package be.simplenotes.persistence.utils
|
||||
|
||||
import be.simplenotes.config.DataSourceConfig
|
||||
|
||||
enum class DbType { H2, MariaDb }
|
||||
|
||||
fun DataSourceConfig.type(): DbType = if (jdbcUrl.contains("mariadb")) DbType.MariaDb
|
||||
else DbType.H2
|
||||
Reference in New Issue
Block a user