47 lines
2.1 KiB
Kotlin
47 lines
2.1 KiB
Kotlin
package space.luminic.budgerapp.mappers
|
|
|
|
import org.bson.Document
|
|
import org.bson.types.ObjectId
|
|
import org.springframework.stereotype.Component
|
|
import space.luminic.budgerapp.models.*
|
|
import java.time.ZoneId
|
|
|
|
@Component
|
|
class TransactionsMapper : FromDocumentMapper {
|
|
|
|
|
|
override fun fromDocument(document: Document): Transaction {
|
|
val categoryDocument = document.get("categoryDetails", Document::class.java)
|
|
val categoryTypeDocument = categoryDocument["type"] as Document
|
|
val spaceDocument = document["spaceDetails"] as Document
|
|
val userDocument = document.get("userDetails", Document::class.java)
|
|
return Transaction(
|
|
id = document.getObjectId("_id").toString(),
|
|
type = TransactionType(
|
|
document.get("type", Document::class.java).getString("code"),
|
|
document.get("type", Document::class.java).getString("name")
|
|
),
|
|
space = Space(
|
|
id = spaceDocument.getObjectId("_id").toString()
|
|
),
|
|
category = Category(
|
|
id = (categoryDocument["_id"] as ObjectId).toString(),
|
|
type = CategoryType(
|
|
categoryTypeDocument["code"] as String,
|
|
categoryTypeDocument["name"] as String
|
|
),
|
|
name = categoryDocument["name"] as String,
|
|
description = categoryDocument["description"] as String,
|
|
icon = categoryDocument["icon"] as String
|
|
),
|
|
comment = document.getString("comment"),
|
|
date = document.getDate("date").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
|
|
amount = document.getDouble("amount"),
|
|
isDone = document.getBoolean("isDone"),
|
|
user = User(userDocument.getObjectId("_id").toString(), userDocument.getString("username"), userDocument.getString("firstName"),),
|
|
parentId = document.getString("parentId"),
|
|
createdAt = document.getDate("createdAt").toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
|
|
)
|
|
|
|
}
|
|
} |