Import feature
This commit is contained in:
@@ -26,6 +26,8 @@ interface NoteRepository {
|
||||
fun create(userId: Int, note: Note): PersistedNote
|
||||
fun find(userId: Int, uuid: UUID): PersistedNote?
|
||||
fun update(userId: Int, uuid: UUID, note: Note): PersistedNote?
|
||||
fun import(userId: Int, notes: List<ExportedNote>)
|
||||
|
||||
fun export(userId: Int): List<ExportedNote>
|
||||
fun findAllDetails(userId: Int): List<PersistedNote>
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package be.simplenotes.persistence.repositories
|
||||
import be.simplenotes.persistence.*
|
||||
import be.simplenotes.persistence.converters.NoteConverter
|
||||
import be.simplenotes.persistence.extensions.arrayContains
|
||||
import be.simplenotes.types.ExportedNote
|
||||
import be.simplenotes.types.Note
|
||||
import be.simplenotes.types.PersistedNote
|
||||
import org.ktorm.database.Database
|
||||
@@ -118,6 +119,43 @@ internal class NoteRepositoryImpl(
|
||||
.map { it.getInt(1) }
|
||||
.first()
|
||||
|
||||
override fun import(userId: Int, notes: List<ExportedNote>) {
|
||||
if (notes.isEmpty()) return
|
||||
val notesByID = notes.associateBy { UUID.randomUUID() }
|
||||
val tags = sequence<Pair<UUID, String>> {
|
||||
notesByID.entries.forEach { (key, value) ->
|
||||
value.tags.forEach { tag ->
|
||||
yield(key to tag)
|
||||
}
|
||||
}
|
||||
}.toList()
|
||||
|
||||
db.batchInsert(Notes) {
|
||||
notesByID.forEach { (uuid, note) ->
|
||||
item {
|
||||
set(it.uuid, uuid)
|
||||
set(it.userId, userId)
|
||||
set(it.title, note.title)
|
||||
set(it.html, note.html)
|
||||
set(it.markdown, note.markdown)
|
||||
set(it.updatedAt, note.updatedAt)
|
||||
set(it.deleted, note.trash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tags.takeIf { it.isNotEmpty() }?.run {
|
||||
db.batchInsert(Tags) {
|
||||
forEach { (uuid, tagName) ->
|
||||
item {
|
||||
set(it.noteUuid, uuid)
|
||||
set(it.name, tagName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun export(userId: Int) = db.noteWithTags
|
||||
.filterColumns { it.columns - it.userId - it.public }
|
||||
.filter { it.userId eq userId }
|
||||
|
||||
Reference in New Issue
Block a user