Markdown improvements

This commit is contained in:
2020-08-24 18:50:54 +02:00
parent 815770303c
commit f6e33ed3b4
4 changed files with 64 additions and 69 deletions
@@ -1,9 +1,6 @@
package be.simplenotes.persistance.notes
import be.simplenotes.domain.model.ExportedNote
import be.simplenotes.domain.model.Note
import be.simplenotes.domain.model.PersistedNote
import be.simplenotes.domain.model.PersistedNoteMetadata
import be.simplenotes.domain.model.*
import be.simplenotes.domain.usecases.repositories.NoteRepository
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
@@ -45,14 +42,7 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
.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 })
val tagsByUuid = notes.tagsByUuid()
return notes.map { note ->
val tags = tagsByUuid[note.uuid] ?: emptyList()
@@ -101,15 +91,17 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
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) and (it.deleted eq false) }
?: return null
currentNote.title = note.meta.title
currentNote.markdown = note.markdown
currentNote.html = note.html
currentNote.updatedAt = LocalDateTime.now()
currentNote.flushChanges()
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) {
@@ -123,22 +115,27 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
it.noteUuid to uuid
}
}
return currentNote.toPersistedNote(note.meta.tags)
return PersistedNote(
meta = note.meta,
markdown = note.markdown,
html = note.html,
updatedAt = now,
uuid = uuid
)
}
}
override fun delete(userId: Int, uuid: UUID, permanent: Boolean): Boolean {
if (!permanent) {
return db.useTransaction {
return if (!permanent) {
db.useTransaction {
db.update(Notes) {
it.deleted to true
it.updatedAt to LocalDateTime.now()
where { it.userId eq userId and (it.uuid eq uuid) }
}
} == 1
}
return db.useTransaction {
} else db.useTransaction {
db.delete(Notes) { it.uuid eq uuid and (it.userId eq userId) } == 1
}
}
@@ -147,9 +144,7 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
return db.useTransaction {
db.update(Notes) {
it.deleted to false
where {
(it.userId eq userId) and (it.uuid eq uuid)
}
where { (it.userId eq userId) and (it.uuid eq uuid) }
} == 1
}
}
@@ -162,9 +157,8 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
.map { it[Tags.name]!! }
override fun count(userId: Int, tag: String?, deleted: Boolean): Int {
if (tag == null) return db.notes.count { (it.userId eq userId) and (Notes.deleted eq deleted) }
return db.sequenceOf(Tags).count {
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)
}
}
@@ -177,14 +171,7 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
.sortedByDescending { it.updatedAt }
.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 })
val tagsByUuid = notes.tagsByUuid()
return notes.map { note ->
ExportedNote(
@@ -204,18 +191,20 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
.filter { (it.userId eq userId) and (it.deleted eq false) }
.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 })
val tagsByUuid = notes.tagsByUuid()
return notes.map { note ->
val tags = tagsByUuid[note.uuid] ?: emptyList()
note.toPersistedNote(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 })
}
}