Start migration of notesServices + controllers

This commit is contained in:
2020-04-25 17:24:43 +02:00
parent 573963b161
commit 52aae6773f
8 changed files with 130 additions and 106 deletions
+82 -42
View File
@@ -5,11 +5,13 @@ import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Tags
import me.liuwj.ktorm.database.*
import me.liuwj.ktorm.dsl.*
import me.liuwj.ktorm.entity.*
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.generic.instance
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
/**
* service to handle database queries at the Notes level.
@@ -18,102 +20,140 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
private val db by instance<Database>()
/**
* returns a list of [NotesDTO] associated with the userId
* returns a list of [BasicNoteDTO] associated with the userId
*/
fun getNotes(userId: Int): List<NotesDTO> = db.from(Notes)
.select(Notes.id, Notes.title, Notes.updatedAt)
.where { Notes.userId eq userId }
.orderBy(Notes.updatedAt.desc())
.map { row ->
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteId eq row[Notes.id]!! }
.map { it[Tags.name]!! }
fun getNotes(userId: Int): List<BasicNoteDTO> {
val notes = db.sequenceOf(Notes)
.filterColumns { listOf(it.uuid, it.title, it.updatedAt) }
.filter { it.userId eq userId }
.sortedByDescending { it.updatedAt }
.toList()
val updatedAt = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(row[Notes.updatedAt]!!)
if(notes.isEmpty()) return emptyList()
NotesDTO(row[Notes.title]!!, tags, updatedAt)
val tags = db.sequenceOf(Tags)
.filterColumns { listOf(it.noteUuid, it.name) }
.filter { it.noteUuid inList notes.map { it.uuid } }
.toList()
return notes.map { note ->
val noteTags = tags.asSequence()
.filter { it.note.uuid == note.uuid }
.map { it.name }
.toList()
BasicNoteDTO(note.uuid, note.title, noteTags, DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(note.updatedAt))
}
}
fun getNoteIdFromUserIdAndTitle(userId: Int, noteTitle: String): Int? = db.from(Notes)
.select(Notes.id)
.where { Notes.userId eq userId and (Notes.title eq noteTitle) }
.limit(0, 1)
.map { it[Notes.id]!! }
.firstOrNull()
fun noteExistsWithTitle(userId: Int, title: String): Boolean {
TODO()
}
fun noteExists(userId: Int, uuid: UUID): Boolean {
TODO()
}
fun createNote(userId: Int, title: String, tags: List<String>) {
db.useTransaction {
val noteId = db.insertAndGenerateKey(Notes) {
val uuid = UUID.randomUUID()
db.insert(Notes) {
it.uuid to uuid
it.title to title
it.userId to userId
it.updatedAt to LocalDateTime.now()
}
tags.forEach { tagName ->
db.insert(Tags) {
it.name to tagName
it.noteId to noteId
db.batchInsert(Tags) {
tags.forEach { tagName ->
item {
it.noteUuid to uuid
it.name to tagName
}
}
}
}
}
fun getTagsAndChapters(noteId: Int): TagsChaptersDTO {
fun getNote(noteUuid: UUID): FullNoteDTO {
TODO()
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteId eq noteId }
.where { Tags.noteUuid eq noteUuid }
.map { it[Tags.name]!! }
.toList()
val chapters = db.from(Chapters)
.select(Chapters.title, Chapters.content)
.where { Chapters.noteId eq noteId }
.where { Chapters.noteUuid eq noteUuid }
.orderBy(Chapters.number.asc())
.map { ChaptersDTO(it[Chapters.title]!!, it[Chapters.content]!!) }
.map { ChapterDTO(it[Chapters.title]!!, it[Chapters.content]!!) }
.toList()
return TagsChaptersDTO(tags, chapters)
}
fun updateNote(noteId: Int, tags: List<String>?, title: String?): Unit =
fun updateNote(patch: FullNoteDTOPatch) {
if(patch.uuid == null) return
db.useTransaction {
if (title != null) {
if (patch.title != null) {
db.update(Notes) {
it.title to title
it.title to patch.title
it.updatedAt to LocalDateTime.now()
where { it.id eq noteId }
where { it.uuid eq patch.uuid }
}
}
if (tags != null) {
if (patch.tags != null) {
// delete all tags
db.delete(Tags) {
it.noteId eq noteId
it.noteUuid eq patch.uuid
}
// put new ones
tags.forEach { tagName ->
patch.tags.forEach { tagName ->
db.insert(Tags) {
it.name to tagName
it.noteId to noteId
it.noteUuid to patch.uuid
}
}
}
}
fun deleteNote(noteId: Int): Unit =
TODO("get chapters")
}
fun deleteNote(noteUuid: UUID): Unit =
db.useTransaction {
db.delete(Notes) { it.id eq noteId }
db.delete(Notes) { it.uuid eq noteUuid }
}
fun getTags(userId: Int): List<String> = db.from(Tags)
.leftJoin(Notes, on = Tags.noteId eq Notes.id)
.leftJoin(Notes, on = Tags.noteUuid eq Notes.uuid)
.select(Tags.name)
.where { Notes.userId eq userId }
.map { it[Tags.name]!! }
}
data class ChaptersDTO(val title: String, val content: String)
data class TagsChaptersDTO(val tags: List<String>, val chapters: List<ChaptersDTO>)
data class NotesDTO(val title: String, val tags: List<String>, val updatedAt: String)
data class ChapterDTO(val title: String, val content: String)
data class FullNoteDTO(
val uuid: UUID,
val title: String,
val updatedAt: String,
val tags: List<String>,
val chapters: List<ChapterDTO>
)
data class FullNoteDTOPatch(
val uuid: UUID? = null,
val title: String? = null,
val updatedAt: String? = null,
val tags: List<String>? = null,
val chapters: List<ChapterDTO>? = null
)
data class BasicNoteDTO(
val uuid: UUID,
val title: String,
val tags: List<String>,
val updatedAt: String
)