44 lines
2.1 KiB
Kotlin
44 lines
2.1 KiB
Kotlin
package space.luminic.budgerapp.mappers
|
|
|
|
import com.mongodb.DBRef
|
|
import org.bson.Document
|
|
import org.springframework.stereotype.Component
|
|
import space.luminic.budgerapp.models.*
|
|
import java.time.ZoneId
|
|
|
|
@Component
|
|
class BudgetMapper(private val categoryMapper: CategoryMapper) : FromDocumentMapper {
|
|
|
|
|
|
override fun fromDocument(document: Document): Budget {
|
|
val spaceId = document.get("spaceDetails", Document::class.java)?.getObjectId("_id")?.toString()
|
|
val categoriesList = document.getList("categories", Document::class.java).orEmpty()
|
|
val incomeCategoriesList = document.getList("incomeCategories", Document::class.java).orEmpty()
|
|
return Budget(
|
|
id = document.getObjectId("_id").toString(),
|
|
space = Space(id=spaceId),
|
|
name = document.getString("name"),
|
|
dateFrom = document.getDate("dateFrom").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
|
|
dateTo = document.getDate("dateTo").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
|
|
categories = categoriesList.map { cat ->
|
|
val categoryDetailed = document.getList("categoriesDetails", Document::class.java).first {
|
|
it.getObjectId("_id").toString() == cat.get("category", DBRef::class.java).id.toString()
|
|
}
|
|
BudgetCategory(
|
|
category = categoryMapper.fromDocument(categoryDetailed),
|
|
currentLimit = cat.getDouble("currentLimit")
|
|
)
|
|
}.toMutableList(),
|
|
incomeCategories = incomeCategoriesList.map { cat ->
|
|
val categoryDetailed =
|
|
document.getList("incomeCategoriesDetails", Document::class.java).first { it ->
|
|
it.getObjectId("_id").toString() == cat.get("category", DBRef::class.java).id.toString()
|
|
}
|
|
BudgetCategory(
|
|
category = categoryMapper.fromDocument(categoryDetailed),
|
|
currentLimit = cat.getDouble("currentLimit")
|
|
)
|
|
}.toMutableList()
|
|
)
|
|
}
|
|
} |