mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
refactor services
This commit is contained in:
@@ -8,7 +8,6 @@ import kotlinx.serialization.json.Json
|
|||||||
import kotlinx.serialization.json.encodeToJsonElement
|
import kotlinx.serialization.json.encodeToJsonElement
|
||||||
import org.springframework.kafka.core.KafkaTemplate
|
import org.springframework.kafka.core.KafkaTemplate
|
||||||
import org.springframework.kafka.support.KafkaHeaders
|
import org.springframework.kafka.support.KafkaHeaders
|
||||||
import org.springframework.messaging.Message
|
|
||||||
import org.springframework.messaging.support.MessageBuilder
|
import org.springframework.messaging.support.MessageBuilder
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@@ -18,17 +17,16 @@ class ProducerImpl(
|
|||||||
private val schemaValidator: SchemaValidator,
|
private val schemaValidator: SchemaValidator,
|
||||||
): Producer {
|
): Producer {
|
||||||
override fun produceProductInfo(topicName: String, product: Product) {
|
override fun produceProductInfo(topicName: String, product: Product) {
|
||||||
|
Json.encodeToJsonElement(ProductDto(product)).let {
|
||||||
|
schemaValidator.validate(SCHEMA_KAFKA_PRODUCT_SYNC, it)
|
||||||
|
|
||||||
val serializedProduct = Json.encodeToJsonElement(ProductDto(product))
|
MessageBuilder.withPayload(it.toString())
|
||||||
|
|
||||||
schemaValidator.validate(SCHEMA_KAFKA_PRODUCT_SYNC, serializedProduct)
|
|
||||||
|
|
||||||
val message: Message<String> = MessageBuilder
|
|
||||||
.withPayload(serializedProduct.toString())
|
|
||||||
.setHeader(KafkaHeaders.TOPIC, topicName)
|
.setHeader(KafkaHeaders.TOPIC, topicName)
|
||||||
.setHeader("X-Custom-Header", "some-custom-header")
|
.setHeader("X-Custom-Header", "some-custom-header")
|
||||||
.build()
|
.build()
|
||||||
|
}
|
||||||
kafkaTemplate.send(message)
|
.let {
|
||||||
|
msg -> kafkaTemplate.send(msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,9 @@ import java.time.format.DateTimeFormatter
|
|||||||
class OffsetDateTimeSerialization: KSerializer<OffsetDateTime> {
|
class OffsetDateTimeSerialization: KSerializer<OffsetDateTime> {
|
||||||
override val descriptor = PrimitiveSerialDescriptor("Time", PrimitiveKind.STRING)
|
override val descriptor = PrimitiveSerialDescriptor("Time", PrimitiveKind.STRING)
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): OffsetDateTime {
|
override fun deserialize(decoder: Decoder): OffsetDateTime = OffsetDateTime.parse(decoder.decodeString())
|
||||||
return OffsetDateTime.parse(decoder.decodeString())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: OffsetDateTime) {
|
override fun serialize(encoder: Encoder, value: OffsetDateTime) {
|
||||||
encoder.encodeString(value.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
|
encoder.encodeString(value.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,9 @@ import java.util.*
|
|||||||
class UuidSerialization: KSerializer<UUID> {
|
class UuidSerialization: KSerializer<UUID> {
|
||||||
override val descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
|
override val descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING)
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): UUID {
|
override fun deserialize(decoder: Decoder): UUID = UUID.fromString(decoder.decodeString())
|
||||||
return UUID.fromString(decoder.decodeString())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: UUID) {
|
override fun serialize(encoder: Encoder, value: UUID) {
|
||||||
encoder.encodeString(value.toString())
|
encoder.encodeString(value.toString())
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import com.github.dannecron.demo.services.validation.exceptions.SchemaNotFoundEx
|
|||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
|
||||||
interface SchemaValidator {
|
interface SchemaValidator {
|
||||||
@Throws(ElementNotValidException::class, SchemaNotFoundException::class)
|
|
||||||
fun validate(schemaName: String, value: JsonElement)
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SCHEMA_KAFKA_PRODUCT_SYNC = "kafka-product-sync"
|
const val SCHEMA_KAFKA_PRODUCT_SYNC = "kafka-product-sync"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(ElementNotValidException::class, SchemaNotFoundException::class)
|
||||||
|
fun validate(schemaName: String, value: JsonElement)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,18 +13,16 @@ class SchemaValidatorImp(
|
|||||||
private val loadedSchema: MutableMap<String, String> = mutableMapOf()
|
private val loadedSchema: MutableMap<String, String> = mutableMapOf()
|
||||||
|
|
||||||
override fun validate(schemaName: String, value: JsonElement) {
|
override fun validate(schemaName: String, value: JsonElement) {
|
||||||
|
JsonSchema.fromDefinition(
|
||||||
val schema = JsonSchema.fromDefinition(
|
|
||||||
getSchema(schemaName),
|
getSchema(schemaName),
|
||||||
)
|
).also {
|
||||||
|
|
||||||
val errors = mutableListOf<ValidationError>()
|
val errors = mutableListOf<ValidationError>()
|
||||||
|
|
||||||
val valid = schema.validate(value, errors::add)
|
if (!it.validate(value, errors::add)) {
|
||||||
if (!valid) {
|
|
||||||
throw ElementNotValidException(errors)
|
throw ElementNotValidException(errors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getSchema(schemaName: String): String {
|
private fun getSchema(schemaName: String): String {
|
||||||
val loaded = loadedSchema[schemaName]
|
val loaded = loadedSchema[schemaName]
|
||||||
|
|||||||
Reference in New Issue
Block a user