Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore: πŸ”§ Move the Date Field to the Payload in Spending Sharing Message #235

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.co.pennyway.socket.config;

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration;

@Configuration
class JacksonConfig {
@Bean
fun objectMapper(): ObjectMapper {
return ObjectMapper().apply {
registerModule(JavaTimeModule())
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.amqp.rabbit.annotation.QueueBinding
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Component
import java.time.LocalDate

@Slf4j
@Component
Expand All @@ -27,6 +28,11 @@ class SpendingShareEventListener(
private val log = logger()
}

private data class Payload(
val date: LocalDate,
val spendingOnDates: List<SpendingChatShareEvent.SpendingOnDate>
)

@RabbitListener(
containerFactory = "simpleRabbitListenerContainerFactory",
bindings = [QueueBinding(
Expand All @@ -38,7 +44,7 @@ class SpendingShareEventListener(
fun handle(event: SpendingChatShareEvent) {
log.debug("handle: {}", event)

convertToJson(event.spendingOnDates())
convertToJson(Payload(event.date(), event.spendingOnDates()))
.getOrNull()
?.let { payload ->
chatMessageSendService.execute(
Expand All @@ -50,15 +56,15 @@ class SpendingShareEventListener(
event.senderId(),
event.name(),
null,
mapOf("Content-Type" to "application/json", "date" to event.date())
mapOf("Content-Type" to "application/json")
)
)
}
}

private fun convertToJson(spendingOnDates: List<SpendingChatShareEvent.SpendingOnDate>): Result<String> =
private fun convertToJson(payload: Payload): Result<String> =
runCatching {
objectMapper.writeValueAsString(spendingOnDates)
objectMapper.writeValueAsString(payload)
}.onFailure {
log.error("Failed to serialize spendingOnDates", it)
}
Expand Down