Merge branch 'export'
This commit is contained in:
@@ -6,6 +6,8 @@ import be.simplenotes.domain.security.PasswordHash
|
||||
import be.simplenotes.domain.security.SimpleJwt
|
||||
import be.simplenotes.domain.usecases.NoteService
|
||||
import be.simplenotes.domain.usecases.UserService
|
||||
import be.simplenotes.domain.usecases.export.ExportUseCase
|
||||
import be.simplenotes.domain.usecases.export.ExportUseCaseImpl
|
||||
import be.simplenotes.domain.usecases.markdown.MarkdownConverter
|
||||
import be.simplenotes.domain.usecases.markdown.MarkdownConverterImpl
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteUseCase
|
||||
@@ -20,10 +22,11 @@ val domainModule = module {
|
||||
single<LoginUseCase> { LoginUseCaseImpl(get(), get(), get()) }
|
||||
single<RegisterUseCase> { RegisterUseCaseImpl(get(), get()) }
|
||||
single<DeleteUseCase> { DeleteUseCaseImpl(get(), get()) }
|
||||
single { UserService(get(), get(), get()) }
|
||||
single { UserService(get(), get(), get(), get()) }
|
||||
single<PasswordHash> { BcryptPasswordHash() }
|
||||
single { SimpleJwt(get()) }
|
||||
single { JwtPayloadExtractor(get()) }
|
||||
single { NoteService(get(), get()) }
|
||||
single<MarkdownConverter> { MarkdownConverterImpl() }
|
||||
single<ExportUseCase> { ExportUseCaseImpl(get()) }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package be.simplenotes.domain.model
|
||||
|
||||
import kotlinx.serialization.Contextual
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
@@ -28,3 +30,13 @@ data class PersistedNote(
|
||||
val updatedAt: LocalDateTime,
|
||||
val uuid: UUID,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ExportedNote(
|
||||
val title: String,
|
||||
val tags: List<String>,
|
||||
val markdown: String,
|
||||
val html: String,
|
||||
@Contextual val updatedAt: LocalDateTime,
|
||||
val trash: Boolean,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package be.simplenotes.domain.usecases
|
||||
|
||||
import be.simplenotes.domain.usecases.export.ExportUseCase
|
||||
import be.simplenotes.domain.usecases.users.delete.DeleteUseCase
|
||||
import be.simplenotes.domain.usecases.users.login.LoginUseCase
|
||||
import be.simplenotes.domain.usecases.users.register.RegisterUseCase
|
||||
@@ -8,4 +9,8 @@ class UserService(
|
||||
loginUseCase: LoginUseCase,
|
||||
registerUseCase: RegisterUseCase,
|
||||
deleteUseCase: DeleteUseCase,
|
||||
) : LoginUseCase by loginUseCase, RegisterUseCase by registerUseCase, DeleteUseCase by deleteUseCase
|
||||
exportUseCase: ExportUseCase,
|
||||
) : LoginUseCase by loginUseCase,
|
||||
RegisterUseCase by registerUseCase,
|
||||
DeleteUseCase by deleteUseCase,
|
||||
ExportUseCase by exportUseCase
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package be.simplenotes.domain.usecases.export
|
||||
|
||||
interface ExportUseCase {
|
||||
fun export(userId: Int): String
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package be.simplenotes.domain.usecases.export
|
||||
|
||||
import be.simplenotes.domain.model.ExportedNote
|
||||
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.descriptors.PrimitiveKind
|
||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.modules.SerializersModule
|
||||
import java.time.LocalDateTime
|
||||
|
||||
internal class ExportUseCaseImpl(private val noteRepository: NoteRepository) : ExportUseCase {
|
||||
override fun export(userId: Int): String {
|
||||
val module = SerializersModule {
|
||||
contextual(LocalDateTime::class, LocalDateTimeSerializer)
|
||||
}
|
||||
|
||||
val json = Json {
|
||||
prettyPrint = true
|
||||
serializersModule = module
|
||||
}
|
||||
|
||||
val notes = noteRepository.export(userId)
|
||||
return json.encodeToString(ListSerializer(ExportedNote.serializer()), notes)
|
||||
}
|
||||
}
|
||||
|
||||
internal object LocalDateTimeSerializer : KSerializer<LocalDateTime> {
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
|
||||
|
||||
override fun serialize(encoder: Encoder, value: LocalDateTime) {
|
||||
encoder.encodeString(value.toString())
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): LocalDateTime {
|
||||
TODO("Not implemented, isn't needed")
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package be.simplenotes.domain.usecases.repositories
|
||||
|
||||
import be.simplenotes.domain.model.ExportedNote
|
||||
import be.simplenotes.domain.model.Note
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
@@ -25,4 +26,5 @@ 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 export(userId: Int): List<ExportedNote>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user