feat: ドメインイベントを受け取れるように

This commit is contained in:
usbharu 2024-06-27 01:00:13 +09:00
parent 593029e2c3
commit bf2d7986cc
25 changed files with 124 additions and 74 deletions

View File

@ -0,0 +1,10 @@
package dev.usbharu.hideout.core.application.domainevent.subscribers
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
interface DomainEventSubscriber {
fun <T : DomainEventBody> subscribe(eventName: String, domainEventConsumer: DomainEventConsumer<T>)
}
typealias DomainEventConsumer<T> = (DomainEvent<T>) -> Unit

View File

@ -0,0 +1,17 @@
package dev.usbharu.hideout.core.application.domainevent.subscribers
import dev.usbharu.hideout.core.domain.event.post.PostEvent
import dev.usbharu.hideout.core.domain.event.post.PostEventBody
import org.springframework.stereotype.Component
@Component
class TimelinePostCreateSubscriber(domainEventSubscriber: DomainEventSubscriber) {
init {
domainEventSubscriber.subscribe<PostEventBody>(PostEvent.CREATE.eventName) {
val post = it.body.getPost()
val actor = it.body.getActor()
println(post.toString())
}
}
}

View File

@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
class ActorDomainEventFactory(private val actor: Actor) {
fun createEvent(actorEvent: ActorEvent): DomainEvent {
fun createEvent(actorEvent: ActorEvent): DomainEvent<ActorEventBody> {
return DomainEvent.create(
actorEvent.eventName,
ActorEventBody(actor),

View File

@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
class ActorInstanceRelationshipDomainEventFactory(private val actorInstanceRelationship: ActorInstanceRelationship) {
fun createEvent(actorInstanceRelationshipEvent: ActorInstanceRelationshipEvent): DomainEvent {
fun createEvent(actorInstanceRelationshipEvent: ActorInstanceRelationshipEvent): DomainEvent<ActorInstanceRelationshipEventBody> {
return DomainEvent.create(
actorInstanceRelationshipEvent.eventName,
ActorInstanceRelationshipEventBody(actorInstanceRelationship)

View File

@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
class InstanceEventFactory(private val instance: Instance) {
fun createEvent(event: InstanceEvent): DomainEvent {
fun createEvent(event: InstanceEvent): DomainEvent<InstanceEventBody> {
return DomainEvent.create(
event.eventName,
InstanceEventBody(instance)

View File

@ -22,7 +22,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
class PostDomainEventFactory(private val post: Post, private val actor: Actor? = null) {
fun createEvent(postEvent: PostEvent): DomainEvent {
fun createEvent(postEvent: PostEvent): DomainEvent<PostEventBody> {
return DomainEvent.create(
postEvent.eventName,
PostEventBody(post, actor)
@ -30,7 +30,10 @@ class PostDomainEventFactory(private val post: Post, private val actor: Actor? =
}
}
class PostEventBody(post: Post, actor: Actor?) : DomainEventBody(mapOf("post" to post, "actor" to actor))
class PostEventBody(post: Post, actor: Actor?) : DomainEventBody(mapOf("post" to post, "actor" to actor)) {
fun getPost(): Post = toMap()["post"] as Post
fun getActor(): Actor? = toMap()["actor"] as Actor?
}
enum class PostEvent(val eventName: String) {
DELETE("PostDelete"),

View File

@ -21,7 +21,7 @@ import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
class RelationshipEventFactory(private val relationship: Relationship) {
fun createEvent(relationshipEvent: RelationshipEvent): DomainEvent =
fun createEvent(relationshipEvent: RelationshipEvent): DomainEvent<RelationshipEventBody> =
DomainEvent.create(relationshipEvent.eventName, RelationshipEventBody(relationship))
}

View File

@ -20,5 +20,5 @@ enum class Role {
LOCAL,
MODERATOR,
ADMINISTRATOR,
REMOTE;
REMOTE
}

View File

@ -1,9 +1,7 @@
package dev.usbharu.hideout.core.domain.model.filter
class FilterName(name: String) {
val name = name.take(LENGTH)
companion object {

View File

@ -28,23 +28,23 @@ import java.util.*
* @property body ドメインイベントのボディ
* @property collectable trueで同じドメインイベント名でをまとめる
*/
data class DomainEvent(
data class DomainEvent<out T : DomainEventBody>(
val id: String,
val name: String,
val occurredOn: Instant,
val body: DomainEventBody,
val body: T,
val collectable: Boolean = false
) {
companion object {
fun create(name: String, body: DomainEventBody, collectable: Boolean = false): DomainEvent =
DomainEvent(UUID.randomUUID().toString(), name, Instant.now(), body, collectable)
fun <T : DomainEventBody> create(name: String, body: T, collectable: Boolean = false): DomainEvent<T> =
DomainEvent<T>(UUID.randomUUID().toString(), name, Instant.now(), body, collectable)
fun reconstruct(
fun <T : DomainEventBody> reconstruct(
id: String,
name: String,
occurredOn: Instant,
body: DomainEventBody,
body: T,
collectable: Boolean
): DomainEvent = DomainEvent(id, name, occurredOn, body, collectable)
): DomainEvent<T> = DomainEvent(id, name, occurredOn, body, collectable)
}
}

View File

@ -17,6 +17,6 @@
package dev.usbharu.hideout.core.domain.shared.domainevent
@Suppress("UnnecessaryAbstractClass")
abstract class DomainEventBody(val map: Map<String, Any?>) {
abstract class DomainEventBody(private val map: Map<String, Any?>) {
fun toMap(): Map<String, Any?> = map
}

View File

@ -1,5 +1,5 @@
package dev.usbharu.hideout.core.domain.shared.domainevent
interface DomainEventPublisher {
suspend fun publishEvent(domainEvent: DomainEvent)
suspend fun publishEvent(domainEvent: DomainEvent<*>)
}

View File

@ -18,13 +18,13 @@ package dev.usbharu.hideout.core.domain.shared.domainevent
@Suppress("UnnecessaryAbstractClass")
abstract class DomainEventStorable {
private val domainEvents: MutableList<DomainEvent> = mutableListOf()
private val domainEvents: MutableList<DomainEvent<*>> = mutableListOf()
protected fun addDomainEvent(domainEvent: DomainEvent) {
protected fun addDomainEvent(domainEvent: DomainEvent<*>) {
domainEvents.add(domainEvent)
}
fun clearDomainEvents() = domainEvents.clear()
fun getDomainEvents(): List<DomainEvent> = domainEvents.toList()
fun getDomainEvents(): List<DomainEvent<*>> = domainEvents.toList()
}

View File

@ -1,7 +0,0 @@
package dev.usbharu.hideout.core.domain.shared.domainevent
interface DomainEventSubscriber {
fun subscribe(eventName: String, domainEventConsumer: DomainEventConsumer)
}
typealias DomainEventConsumer = (DomainEvent) -> Unit

View File

@ -24,7 +24,7 @@ import org.springframework.stereotype.Component
@Component
class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: ApplicationEventPublisher) :
DomainEventPublisher {
override suspend fun publishEvent(domainEvent: DomainEvent) {
override suspend fun publishEvent(domainEvent: DomainEvent<*>) {
applicationEventPublisher.publishEvent(domainEvent)
}
}

View File

@ -0,0 +1,29 @@
package dev.usbharu.hideout.core.infrastructure.springframework.domainevent
import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventConsumer
import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventSubscriber
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Component
@Component
class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber {
val map = mutableMapOf<String, MutableList<DomainEventConsumer<*>>>()
override fun <T : DomainEventBody> subscribe(eventName: String, domainEventConsumer: DomainEventConsumer<T>) {
map.getOrPut(eventName) { mutableListOf() }.add(domainEventConsumer as DomainEventConsumer<*>)
}
@EventListener
fun onDomainEventPublished(domainEvent: DomainEvent<*>) {
map[domainEvent.name]?.forEach {
try {
it.invoke(domainEvent)
}
catch (e: Exception) {
}
}
}
}