package dp.kotul.solrapiapplication.service

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.stereotype.Service

@Service
class UpdateObjectService(val mapper: ObjectMapper) {

    fun createJsonForUpdate(document: Any): String {
        val map: Map<*, *> = mapper.convertValue(document, Map::class.java)
        val newMap = map.filter { it.value != null }.mapValues {
            if (it.key != "id") {
                mapOf("set" to it.value)
            } else {
                it.value
            }
        }
        return mapper.writeValueAsString(newMap)
    }
}
