This commit is contained in:
2020-04-20 19:21:52 +02:00
parent cbee40f5e4
commit 1f9923dc82
21 changed files with 333 additions and 401 deletions
+70 -12
View File
@@ -1,5 +1,6 @@
package be.vandewalleh.services
import be.vandewalleh.tables.Chapters
import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Tags
import me.liuwj.ktorm.database.Database
@@ -13,22 +14,20 @@ import java.time.format.DateTimeFormatter
* service to handle database queries at the Notes level.
*/
class NotesService(override val kodein: Kodein) : KodeinAware {
val db by instance<Database>()
private val db by instance<Database>()
/**
* returns a list of [NotesDTO] associated with the userId
*/
fun getNotes(userId: Int): List<NotesDTO> {
val notes = db.from(Notes)
.select(Notes.id, Notes.title, Notes.updatedAt)
.where { Notes.userId eq userId }
.orderBy(Notes.updatedAt.desc())
.map { row ->
Notes.createEntity(row)
}
.toList()
return notes.map { note ->
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 ->
Notes.createEntity(row)
}
.toList()
.map { note ->
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteId eq note.id }
@@ -40,8 +39,67 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
NotesDTO(note.title, tags, 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 createNote(userId: Int, title: String, tags: List<String>) {
TODO()
}
fun getTagsAndChapters(noteId: Int): TagsChaptersDTO {
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteId eq noteId }
.map { it[Tags.name]!! }
.toList()
val chapters = db.from(Chapters)
.select(Chapters.title, Chapters.content)
.where { Chapters.noteId eq noteId }
.orderBy(Chapters.number.asc())
.map { ChaptersDTO(it[Chapters.title]!!, it[Chapters.content]!!) }
.toList()
return TagsChaptersDTO(tags, chapters)
}
fun updateNote(noteId: Int, tags: List<String>?, title: String?): Unit =
db.useTransaction {
if (title != null) {
db.update(Notes) {
it.title to title
where { it.id eq noteId }
}
}
if (tags != null) {
// delete all tags
db.delete(Tags) {
it.noteId eq noteId
}
// put new ones
tags.forEach { tagName ->
db.insert(Tags) {
it.name to tagName
it.noteId to noteId
}
}
}
}
fun deleteNote(noteId: Int): Unit =
db.useTransaction {
db.delete(Tags) { it.noteId eq noteId }
db.delete(Chapters) { it.noteId eq noteId }
db.delete(Notes) { it.id eq noteId }
}
}
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)
+1
View File
@@ -10,4 +10,5 @@ import org.kodein.di.generic.singleton
*/
val serviceModule = Kodein.Module(name = "Services") {
bind<NotesService>() with singleton { NotesService(this.kodein) }
bind<UserService>() with singleton { UserService(this.kodein) }
}