Migration done for the backend
This commit is contained in:
@@ -29,7 +29,7 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
|
||||
.sortedByDescending { it.updatedAt }
|
||||
.toList()
|
||||
|
||||
if(notes.isEmpty()) return emptyList()
|
||||
if (notes.isEmpty()) return emptyList()
|
||||
|
||||
val tags = db.sequenceOf(Tags)
|
||||
.filterColumns { listOf(it.noteUuid, it.name) }
|
||||
@@ -46,37 +46,54 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
|
||||
}
|
||||
}
|
||||
|
||||
fun noteExistsWithTitle(userId: Int, title: String): Boolean {
|
||||
TODO()
|
||||
}
|
||||
|
||||
fun noteExists(userId: Int, uuid: UUID): Boolean {
|
||||
TODO()
|
||||
return db.from(Notes)
|
||||
.select(Notes.uuid)
|
||||
.where { Notes.userId eq userId }
|
||||
.where { Notes.uuid eq uuid }
|
||||
.limit(0, 1)
|
||||
.toList().size == 1
|
||||
}
|
||||
|
||||
fun createNote(userId: Int, title: String, tags: List<String>) {
|
||||
fun createNote(userId: Int, note: FullNoteCreateDTO): UUID {
|
||||
val uuid = UUID.randomUUID()
|
||||
db.useTransaction {
|
||||
val uuid = UUID.randomUUID()
|
||||
db.insert(Notes) {
|
||||
it.uuid to uuid
|
||||
it.title to title
|
||||
it.title to note.title
|
||||
it.userId to userId
|
||||
it.updatedAt to LocalDateTime.now()
|
||||
}
|
||||
|
||||
db.batchInsert(Tags) {
|
||||
tags.forEach { tagName ->
|
||||
note.tags.forEach { tagName ->
|
||||
item {
|
||||
it.noteUuid to uuid
|
||||
it.name to tagName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db.batchInsert(Chapters) {
|
||||
note.chapters.forEachIndexed { index, chapter ->
|
||||
item {
|
||||
it.noteUuid to uuid
|
||||
it.title to chapter.title
|
||||
it.number to index
|
||||
it.content to chapter.content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
fun getNote(noteUuid: UUID): FullNoteDTO {
|
||||
TODO()
|
||||
val note = db.sequenceOf(Notes)
|
||||
.filterColumns { listOf(it.title, it.updatedAt) }
|
||||
.find { it.uuid eq noteUuid } ?: error("Note not found")
|
||||
|
||||
val tags = db.from(Tags)
|
||||
.select(Tags.name)
|
||||
.where { Tags.noteUuid eq noteUuid }
|
||||
@@ -90,10 +107,18 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
|
||||
.map { ChapterDTO(it[Chapters.title]!!, it[Chapters.content]!!) }
|
||||
.toList()
|
||||
|
||||
val updatedAtFormatted = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(note.updatedAt)
|
||||
return FullNoteDTO(
|
||||
uuid = noteUuid,
|
||||
title = note.title,
|
||||
updatedAt = updatedAtFormatted,
|
||||
tags = tags,
|
||||
chapters = chapters
|
||||
)
|
||||
}
|
||||
|
||||
fun updateNote(patch: FullNoteDTOPatch) {
|
||||
if(patch.uuid == null) return
|
||||
fun updateNote(patch: FullNotePatchDTO) {
|
||||
if (patch.uuid == null) return
|
||||
db.useTransaction {
|
||||
if (patch.title != null) {
|
||||
db.update(Notes) {
|
||||
@@ -134,7 +159,11 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
|
||||
.map { it[Tags.name]!! }
|
||||
}
|
||||
|
||||
data class ChapterDTO(val title: String, val content: String)
|
||||
data class ChapterDTO(
|
||||
val title: String,
|
||||
val content: String
|
||||
)
|
||||
|
||||
data class FullNoteDTO(
|
||||
val uuid: UUID,
|
||||
val title: String,
|
||||
@@ -143,7 +172,13 @@ data class FullNoteDTO(
|
||||
val chapters: List<ChapterDTO>
|
||||
)
|
||||
|
||||
data class FullNoteDTOPatch(
|
||||
data class FullNoteCreateDTO(
|
||||
val title: String,
|
||||
val tags: List<String>,
|
||||
val chapters: List<ChapterDTO>
|
||||
)
|
||||
|
||||
data class FullNotePatchDTO(
|
||||
val uuid: UUID? = null,
|
||||
val title: String? = null,
|
||||
val updatedAt: String? = null,
|
||||
|
||||
Reference in New Issue
Block a user