81 lines
2.8 KiB
Kotlin
81 lines
2.8 KiB
Kotlin
package be.simplenotes.persistance.converters
|
|
|
|
import be.simplenotes.domain.model.*
|
|
import be.simplenotes.persistance.notes.NoteEntity
|
|
import me.liuwj.ktorm.entity.Entity
|
|
import org.mapstruct.Mapper
|
|
import org.mapstruct.Mapping
|
|
import org.mapstruct.Mappings
|
|
import java.time.LocalDateTime
|
|
import java.util.*
|
|
|
|
|
|
/**
|
|
* This is an abstract class because kotlin default methods in interface are not seen as default in kapt
|
|
* @see [KT-25960](https://youtrack.jetbrains.com/issue/KT-25960)
|
|
*/
|
|
@Mapper(uses = [NoteEntityFactory::class, UserEntityFactory::class])
|
|
internal abstract class NoteConverter {
|
|
|
|
fun toNote(entity: NoteEntity, tags: Tags) =
|
|
Note(NoteMetadata(title = entity.title, tags = tags), entity.markdown, entity.html)
|
|
|
|
@Mappings(
|
|
Mapping(target = ".", source = "entity"),
|
|
Mapping(target = "tags", source = "tags"),
|
|
)
|
|
abstract fun toNoteMetadata(entity: NoteEntity, tags: Tags): NoteMetadata
|
|
|
|
@Mappings(
|
|
Mapping(target = ".", source = "entity"),
|
|
Mapping(target = "tags", source = "tags"),
|
|
)
|
|
abstract fun toPersistedNoteMetadata(entity: NoteEntity, tags: Tags): PersistedNoteMetadata
|
|
|
|
fun toPersistedNote(entity: NoteEntity, tags: Tags) = PersistedNote(
|
|
NoteMetadata(title = entity.title, tags = tags),
|
|
entity.markdown, entity.html, entity.updatedAt, entity.uuid, entity.public
|
|
)
|
|
|
|
@Mappings(
|
|
Mapping(target = ".", source = "entity"),
|
|
Mapping(target = "trash", source = "entity.deleted"),
|
|
Mapping(target = "tags", source = "tags"),
|
|
)
|
|
abstract fun toExportedNote(entity: NoteEntity, tags: Tags): ExportedNote
|
|
|
|
fun toEntity(note: Note, uuid: UUID, userId: Int, updatedAt: LocalDateTime) = NoteEntity {
|
|
this.title = note.meta.title
|
|
this.markdown = note.markdown
|
|
this.html = note.html
|
|
this.uuid = uuid
|
|
this.deleted = false
|
|
this.public = false
|
|
this.user.id = userId
|
|
this.updatedAt = updatedAt
|
|
}
|
|
|
|
@Mappings(
|
|
Mapping(target = ".", source = "note"),
|
|
Mapping(target = "updatedAt", source = "updatedAt"),
|
|
Mapping(target = "uuid", source = "uuid"),
|
|
Mapping(target = "public", constant = "false"),
|
|
)
|
|
abstract fun toPersistedNote(note: Note, updatedAt: LocalDateTime, uuid: UUID): PersistedNote
|
|
|
|
abstract fun toEntity(persistedNoteMetadata: PersistedNoteMetadata): NoteEntity
|
|
|
|
abstract fun toEntity(noteMetadata: NoteMetadata): NoteEntity
|
|
|
|
@Mapping(target = "title", source = "meta.title")
|
|
abstract fun toEntity(persistedNote: PersistedNote): NoteEntity
|
|
|
|
@Mapping(target = "deleted", source = "trash")
|
|
abstract fun toEntity(exportedNote: ExportedNote): NoteEntity
|
|
|
|
}
|
|
|
|
typealias Tags = List<String>
|
|
|
|
internal class NoteEntityFactory : Entity.Factory<NoteEntity>()
|