mirror of https://github.com/usbharu/Hideout.git
feat: ローカルユーザー追加時に自動でホームタイムライン等を作成するように
This commit is contained in:
parent
711084e366
commit
8d244b74c1
|
@ -0,0 +1,5 @@
|
||||||
|
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||||
|
|
||||||
|
data class RegisterHomeTimeline(
|
||||||
|
val userDetailId: Long
|
||||||
|
)
|
|
@ -1,9 +1,23 @@
|
||||||
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||||
|
|
||||||
class RegisterLocalUserSetHomeTimelineSubscriber(private val domainEventSubscriber: DomainEventSubscriber) :
|
import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailEvent
|
||||||
|
import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailEventBody
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class RegisterLocalUserSetHomeTimelineSubscriber(
|
||||||
|
domainEventSubscriber: DomainEventSubscriber,
|
||||||
|
private val userRegisterHomeTimelineApplicationService: UserRegisterHomeTimelineApplicationService
|
||||||
|
) :
|
||||||
Subscriber {
|
Subscriber {
|
||||||
init {
|
init {
|
||||||
domainEventSubscriber.subscribe<>()
|
domainEventSubscriber.subscribe<UserDetailEventBody>(UserDetailEvent.CREATE.eventName) {
|
||||||
|
userRegisterHomeTimelineApplicationService.execute(
|
||||||
|
RegisterHomeTimeline(it.body.getUserDetail().id),
|
||||||
|
Anonymous
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.timeline.SetTimelineToTimelineStoreApplicationService
|
||||||
|
import dev.usbharu.hideout.core.application.timeline.SetTimleineStore
|
||||||
|
import dev.usbharu.hideout.core.domain.event.timeline.TimelineEvent
|
||||||
|
import dev.usbharu.hideout.core.domain.event.timeline.TimelineEventBody
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class RegisterTimelineSetTimelineStoreSubscriber(
|
||||||
|
domainEventSubscriber: DomainEventSubscriber,
|
||||||
|
private val setTimelineToTimelineStoreApplicationService: SetTimelineToTimelineStoreApplicationService
|
||||||
|
) :
|
||||||
|
Subscriber {
|
||||||
|
init {
|
||||||
|
domainEventSubscriber.subscribe<TimelineEventBody>(TimelineEvent.CREATE.eventName) {
|
||||||
|
setTimelineToTimelineStoreApplicationService.execute(SetTimleineStore(it.body.getTimelineId()), Anonymous)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +1,21 @@
|
||||||
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.timeline.AddPost
|
||||||
|
import dev.usbharu.hideout.core.application.timeline.TimelineAddPostApplicationService
|
||||||
import dev.usbharu.hideout.core.domain.event.post.PostEvent
|
import dev.usbharu.hideout.core.domain.event.post.PostEvent
|
||||||
import dev.usbharu.hideout.core.domain.event.post.PostEventBody
|
import dev.usbharu.hideout.core.domain.event.post.PostEventBody
|
||||||
import dev.usbharu.hideout.core.external.timeline.TimelineStore
|
import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
class TimelinePostCreateSubscriber(
|
class TimelinePostCreateSubscriber(
|
||||||
private val timelineStore: TimelineStore,
|
private val timelineAddPostApplicationService: TimelineAddPostApplicationService,
|
||||||
domainEventSubscriber: DomainEventSubscriber
|
domainEventSubscriber: DomainEventSubscriber,
|
||||||
) : Subscriber {
|
) : Subscriber {
|
||||||
init {
|
init {
|
||||||
domainEventSubscriber.subscribe<PostEventBody>(PostEvent.CREATE.eventName) {
|
domainEventSubscriber.subscribe<PostEventBody>(PostEvent.CREATE.eventName) {
|
||||||
timelineStore.addPost(it.body.getPost())
|
timelineAddPostApplicationService.execute(AddPost(it.body.getPostId()), Anonymous)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ class TimelineRelationshipFollowSubscriber(
|
||||||
AddTimelineRelationship(
|
AddTimelineRelationship(
|
||||||
TimelineRelationship(
|
TimelineRelationship(
|
||||||
TimelineRelationshipId(idGenerateService.generateId()),
|
TimelineRelationshipId(idGenerateService.generateId()),
|
||||||
userDetail.homeTimelineId,
|
userDetail.homeTimelineId!!,
|
||||||
relationship.targetActorId,
|
relationship.targetActorId,
|
||||||
Visible.FOLLOWERS
|
Visible.FOLLOWERS
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package dev.usbharu.hideout.core.application.domainevent.subscribers
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.shared.AbstractApplicationService
|
||||||
|
import dev.usbharu.hideout.core.application.shared.Transaction
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.Principal
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.*
|
||||||
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
|
||||||
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class UserRegisterHomeTimelineApplicationService(
|
||||||
|
private val userDetailRepository: UserDetailRepository,
|
||||||
|
private val timelineRepository: TimelineRepository,
|
||||||
|
private val idGenerateService: IdGenerateService, transaction: Transaction,
|
||||||
|
) : AbstractApplicationService<RegisterHomeTimeline, Unit>(transaction, logger) {
|
||||||
|
override suspend fun internalExecute(command: RegisterHomeTimeline, principal: Principal) {
|
||||||
|
|
||||||
|
val userDetail = (userDetailRepository.findById(UserDetailId(command.userDetailId))
|
||||||
|
?: throw IllegalArgumentException("UserDetail ${command.userDetailId} not found."))
|
||||||
|
|
||||||
|
val timeline = Timeline.create(
|
||||||
|
TimelineId(idGenerateService.generateId()),
|
||||||
|
UserDetailId(command.userDetailId),
|
||||||
|
TimelineName("System-LocalUser-HomeTimeline-${command.userDetailId}"),
|
||||||
|
TimelineVisibility.PRIVATE,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
timelineRepository.save(timeline)
|
||||||
|
userDetail.homeTimelineId = timeline.id
|
||||||
|
|
||||||
|
userDetailRepository.save(userDetail)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(UserRegisterHomeTimelineApplicationService::class.java)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.domain.model.post.PostId
|
||||||
|
|
||||||
|
data class AddPost(val postId: PostId)
|
|
@ -0,0 +1,8 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineVisibility
|
||||||
|
|
||||||
|
data class RegisterTimeline(
|
||||||
|
val timelineName: String,
|
||||||
|
val visibility: TimelineVisibility
|
||||||
|
)
|
|
@ -0,0 +1,29 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.shared.AbstractApplicationService
|
||||||
|
import dev.usbharu.hideout.core.application.shared.Transaction
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.Principal
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineRepository
|
||||||
|
import dev.usbharu.hideout.core.external.timeline.TimelineStore
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class SetTimelineToTimelineStoreApplicationService(
|
||||||
|
transaction: Transaction,
|
||||||
|
private val timelineStore: TimelineStore,
|
||||||
|
private val timelineRepository: TimelineRepository
|
||||||
|
) :
|
||||||
|
AbstractApplicationService<SetTimleineStore, Unit>(
|
||||||
|
transaction, logger
|
||||||
|
) {
|
||||||
|
override suspend fun internalExecute(command: SetTimleineStore, principal: Principal) {
|
||||||
|
val findById = timelineRepository.findById(command.timelineId)
|
||||||
|
?: throw IllegalArgumentException("Timeline ${command.timelineId} not found")
|
||||||
|
timelineStore.addTimeline(findById, emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(SetTimelineToTimelineStoreApplicationService::class.java)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineId
|
||||||
|
|
||||||
|
data class SetTimleineStore(val timelineId: TimelineId)
|
|
@ -0,0 +1,29 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.shared.AbstractApplicationService
|
||||||
|
import dev.usbharu.hideout.core.application.shared.Transaction
|
||||||
|
import dev.usbharu.hideout.core.domain.model.post.PostRepository
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.Principal
|
||||||
|
import dev.usbharu.hideout.core.external.timeline.TimelineStore
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class TimelineAddPostApplicationService(
|
||||||
|
private val timelineStore: TimelineStore,
|
||||||
|
private val postRepository: PostRepository,
|
||||||
|
transaction: Transaction
|
||||||
|
) : AbstractApplicationService<AddPost, Unit>(
|
||||||
|
transaction,
|
||||||
|
logger
|
||||||
|
) {
|
||||||
|
override suspend fun internalExecute(command: AddPost, principal: Principal) {
|
||||||
|
val findById = postRepository.findById(command.postId)
|
||||||
|
?: throw IllegalArgumentException("Post ${command.postId} not found.")
|
||||||
|
timelineStore.addPost(findById)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(TimelineAddPostApplicationService::class.java)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package dev.usbharu.hideout.core.application.timeline
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.application.shared.LocalUserAbstractApplicationService
|
||||||
|
import dev.usbharu.hideout.core.application.shared.Transaction
|
||||||
|
import dev.usbharu.hideout.core.domain.model.support.principal.LocalUser
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.Timeline
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineId
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineName
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineRepository
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.id.IdGenerateService
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class UserRegisterTimelineApplicationService(
|
||||||
|
private val idGenerateService: IdGenerateService,
|
||||||
|
private val timelineRepository: TimelineRepository,
|
||||||
|
transaction: Transaction
|
||||||
|
) :
|
||||||
|
LocalUserAbstractApplicationService<RegisterTimeline, TimelineId>(transaction, logger) {
|
||||||
|
override suspend fun internalExecute(command: RegisterTimeline, principal: LocalUser): TimelineId {
|
||||||
|
val timeline = Timeline.create(
|
||||||
|
id = TimelineId(idGenerateService.generateId()),
|
||||||
|
userDetailId = principal.userDetailId,
|
||||||
|
name = TimelineName(command.timelineName),
|
||||||
|
visibility = command.visibility,
|
||||||
|
isSystem = false
|
||||||
|
)
|
||||||
|
|
||||||
|
timelineRepository.save(timeline)
|
||||||
|
return timeline.id
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(UserRegisterTimelineApplicationService::class.java)
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
package dev.usbharu.hideout.core.domain.event.actor
|
package dev.usbharu.hideout.core.domain.event.actor
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.model.actor.Actor
|
import dev.usbharu.hideout.core.domain.model.actor.Actor
|
||||||
|
import dev.usbharu.hideout.core.domain.model.actor.ActorId
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||||
|
|
||||||
|
@ -24,13 +25,13 @@ class ActorDomainEventFactory(private val actor: Actor) {
|
||||||
fun createEvent(actorEvent: ActorEvent): DomainEvent<ActorEventBody> {
|
fun createEvent(actorEvent: ActorEvent): DomainEvent<ActorEventBody> {
|
||||||
return DomainEvent.create(
|
return DomainEvent.create(
|
||||||
actorEvent.eventName,
|
actorEvent.eventName,
|
||||||
ActorEventBody(actor),
|
ActorEventBody(actor.id),
|
||||||
actorEvent.collectable
|
actorEvent.collectable
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ActorEventBody(actor: Actor) : DomainEventBody(
|
class ActorEventBody(actor: ActorId) : DomainEventBody(
|
||||||
mapOf(
|
mapOf(
|
||||||
"actor" to actor
|
"actor" to actor
|
||||||
),
|
),
|
||||||
|
|
|
@ -17,7 +17,9 @@
|
||||||
package dev.usbharu.hideout.core.domain.event.post
|
package dev.usbharu.hideout.core.domain.event.post
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.model.actor.Actor
|
import dev.usbharu.hideout.core.domain.model.actor.Actor
|
||||||
|
import dev.usbharu.hideout.core.domain.model.actor.ActorId
|
||||||
import dev.usbharu.hideout.core.domain.model.post.Post
|
import dev.usbharu.hideout.core.domain.model.post.Post
|
||||||
|
import dev.usbharu.hideout.core.domain.model.post.PostId
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||||
|
|
||||||
|
@ -25,14 +27,14 @@ class PostDomainEventFactory(private val post: Post, private val actor: Actor? =
|
||||||
fun createEvent(postEvent: PostEvent): DomainEvent<PostEventBody> {
|
fun createEvent(postEvent: PostEvent): DomainEvent<PostEventBody> {
|
||||||
return DomainEvent.create(
|
return DomainEvent.create(
|
||||||
postEvent.eventName,
|
postEvent.eventName,
|
||||||
PostEventBody(post, actor)
|
PostEventBody(post.id, actor?.id)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PostEventBody(post: Post, actor: Actor?) : DomainEventBody(mapOf("post" to post, "actor" to actor)) {
|
class PostEventBody(post: PostId, actor: ActorId?) : DomainEventBody(mapOf("post" to post, "actor" to actor)) {
|
||||||
fun getPost(): Post = toMap()["post"] as Post
|
fun getPostId(): PostId = toMap()["post"] as PostId
|
||||||
fun getActor(): Actor? = toMap()["actor"] as Actor?
|
fun getActorId(): ActorId? = toMap()["actor"] as ActorId?
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class PostEvent(val eventName: String) {
|
enum class PostEvent(val eventName: String) {
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
package dev.usbharu.hideout.core.domain.event.timeline
|
package dev.usbharu.hideout.core.domain.event.timeline
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.model.timeline.Timeline
|
import dev.usbharu.hideout.core.domain.model.timeline.Timeline
|
||||||
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineId
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||||
|
|
||||||
class TimelineEventFactory(private val timeline: Timeline) {
|
class TimelineEventFactory(private val timeline: Timeline) {
|
||||||
fun createEvent(timelineEvent: TimelineEvent): DomainEvent<TimelineEventBody> =
|
fun createEvent(timelineEvent: TimelineEvent): DomainEvent<TimelineEventBody> =
|
||||||
DomainEvent.create(timelineEvent.eventName, TimelineEventBody(timeline))
|
DomainEvent.create(timelineEvent.eventName, TimelineEventBody(timeline.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
class TimelineEventBody(timeline: Timeline) : DomainEventBody(mapOf("timeline" to timeline))
|
class TimelineEventBody(timelineId: TimelineId) : DomainEventBody(mapOf("timeline" to timelineId)) {
|
||||||
|
fun getTimelineId(): TimelineId {
|
||||||
|
return toMap()["timeline"] as TimelineId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum class TimelineEvent(val eventName: String) {
|
enum class TimelineEvent(val eventName: String) {
|
||||||
CHANGE_VISIBILITY("ChangeVisibility")
|
CHANGE_VISIBILITY("ChangeVisibility"),
|
||||||
|
CREATE("TimelineCreate")
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package dev.usbharu.hideout.core.domain.event.userdetail
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
|
||||||
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||||
|
|
||||||
|
class UserDetailDomainEventFactory(private val userDetail: UserDetail) {
|
||||||
|
fun createEvent(userDetailEvent: UserDetailEvent): DomainEvent<UserDetailEventBody> {
|
||||||
|
return DomainEvent.create(
|
||||||
|
userDetailEvent.eventName,
|
||||||
|
UserDetailEventBody(userDetail.id)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserDetailEventBody(userDetail: UserDetailId) : DomainEventBody(
|
||||||
|
mapOf(
|
||||||
|
"userDetail" to userDetail
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
fun getUserDetail(): UserDetailId {
|
||||||
|
return toMap()["userDetail"] as UserDetailId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class UserDetailEvent(val eventName: String) {
|
||||||
|
CREATE("UserDetailCreate"),
|
||||||
|
}
|
|
@ -25,4 +25,24 @@ class Timeline(
|
||||||
|
|
||||||
var name = name
|
var name = name
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(
|
||||||
|
id: TimelineId,
|
||||||
|
userDetailId: UserDetailId,
|
||||||
|
name: TimelineName,
|
||||||
|
visibility: TimelineVisibility,
|
||||||
|
isSystem: Boolean
|
||||||
|
): Timeline {
|
||||||
|
val timeline = Timeline(
|
||||||
|
id = id,
|
||||||
|
userDetailId = userDetailId,
|
||||||
|
name = name,
|
||||||
|
visibility = visibility,
|
||||||
|
isSystem = isSystem
|
||||||
|
)
|
||||||
|
timeline.addDomainEvent(TimelineEventFactory(timeline).createEvent(TimelineEvent.CREATE))
|
||||||
|
return timeline
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,18 +16,21 @@
|
||||||
|
|
||||||
package dev.usbharu.hideout.core.domain.model.userdetails
|
package dev.usbharu.hideout.core.domain.model.userdetails
|
||||||
|
|
||||||
|
import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailDomainEventFactory
|
||||||
|
import dev.usbharu.hideout.core.domain.event.userdetail.UserDetailEvent
|
||||||
import dev.usbharu.hideout.core.domain.model.actor.ActorId
|
import dev.usbharu.hideout.core.domain.model.actor.ActorId
|
||||||
import dev.usbharu.hideout.core.domain.model.timeline.TimelineId
|
import dev.usbharu.hideout.core.domain.model.timeline.TimelineId
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
class UserDetail private constructor(
|
class UserDetail(
|
||||||
val id: UserDetailId,
|
val id: UserDetailId,
|
||||||
val actorId: ActorId,
|
val actorId: ActorId,
|
||||||
var password: UserDetailHashedPassword,
|
var password: UserDetailHashedPassword,
|
||||||
var autoAcceptFolloweeFollowRequest: Boolean,
|
var autoAcceptFolloweeFollowRequest: Boolean,
|
||||||
var lastMigration: Instant? = null,
|
var lastMigration: Instant? = null,
|
||||||
val homeTimelineId: TimelineId?
|
var homeTimelineId: TimelineId?
|
||||||
) {
|
) : DomainEventStorable() {
|
||||||
|
|
||||||
override fun equals(other: Any?): Boolean {
|
override fun equals(other: Any?): Boolean {
|
||||||
if (this === other) return true
|
if (this === other) return true
|
||||||
|
@ -49,7 +52,7 @@ class UserDetail private constructor(
|
||||||
lastMigration: Instant? = null,
|
lastMigration: Instant? = null,
|
||||||
homeTimelineId: TimelineId? = null
|
homeTimelineId: TimelineId? = null
|
||||||
): UserDetail {
|
): UserDetail {
|
||||||
return UserDetail(
|
val userDetail = UserDetail(
|
||||||
id,
|
id,
|
||||||
actorId,
|
actorId,
|
||||||
password,
|
password,
|
||||||
|
@ -57,6 +60,8 @@ class UserDetail private constructor(
|
||||||
lastMigration,
|
lastMigration,
|
||||||
homeTimelineId
|
homeTimelineId
|
||||||
)
|
)
|
||||||
|
userDetail.addDomainEvent(UserDetailDomainEventFactory(userDetail).createEvent(UserDetailEvent.CREATE))
|
||||||
|
return userDetail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,16 @@ package dev.usbharu.hideout.core.domain.shared.repository
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable
|
||||||
|
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||||
import org.springframework.stereotype.Repository
|
import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
interface DomainEventPublishableRepository<T : DomainEventStorable> {
|
interface DomainEventPublishableRepository<T : DomainEventStorable> {
|
||||||
val domainEventPublisher: DomainEventPublisher
|
val domainEventPublisher: DomainEventPublisher
|
||||||
suspend fun update(entity: T) {
|
suspend fun update(entity: T) {
|
||||||
|
println(entity.getDomainEvents().joinToString())
|
||||||
|
val current = TransactionManager.current()
|
||||||
|
current.registerInterceptor()
|
||||||
entity.getDomainEvents().distinctBy {
|
entity.getDomainEvents().distinctBy {
|
||||||
if (it.collectable) {
|
if (it.collectable) {
|
||||||
it.name
|
it.name
|
||||||
|
|
|
@ -22,6 +22,8 @@ import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
|
||||||
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailHashedPassword
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailHashedPassword
|
||||||
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
|
||||||
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
|
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
||||||
|
import dev.usbharu.hideout.core.domain.shared.repository.DomainEventPublishableRepository
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||||
import org.jetbrains.exposed.sql.javatime.timestamp
|
import org.jetbrains.exposed.sql.javatime.timestamp
|
||||||
|
@ -30,11 +32,13 @@ import org.slf4j.LoggerFactory
|
||||||
import org.springframework.stereotype.Repository
|
import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
class UserDetailRepositoryImpl : UserDetailRepository, AbstractRepository() {
|
class UserDetailRepositoryImpl(override val domainEventPublisher: DomainEventPublisher) : UserDetailRepository,
|
||||||
|
AbstractRepository(), DomainEventPublishableRepository<UserDetail> {
|
||||||
override val logger: Logger
|
override val logger: Logger
|
||||||
get() = Companion.logger
|
get() = Companion.logger
|
||||||
|
|
||||||
override suspend fun save(userDetail: UserDetail): UserDetail = query {
|
override suspend fun save(userDetail: UserDetail): UserDetail {
|
||||||
|
val userDetail1 = query {
|
||||||
val singleOrNull =
|
val singleOrNull =
|
||||||
UserDetails.selectAll().where { UserDetails.id eq userDetail.id.id }.forUpdate().singleOrNull()
|
UserDetails.selectAll().where { UserDetails.id eq userDetail.id.id }.forUpdate().singleOrNull()
|
||||||
if (singleOrNull == null) {
|
if (singleOrNull == null) {
|
||||||
|
@ -53,12 +57,19 @@ class UserDetailRepositoryImpl : UserDetailRepository, AbstractRepository() {
|
||||||
it[lastMigration] = userDetail.lastMigration
|
it[lastMigration] = userDetail.lastMigration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return@query userDetail
|
|
||||||
|
userDetail
|
||||||
|
}
|
||||||
|
update(userDetail)
|
||||||
|
return userDetail1
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun delete(userDetail: UserDetail): Unit = query {
|
override suspend fun delete(userDetail: UserDetail): Unit {
|
||||||
|
query {
|
||||||
UserDetails.deleteWhere { id eq userDetail.id.id }
|
UserDetails.deleteWhere { id eq userDetail.id.id }
|
||||||
}
|
}
|
||||||
|
update(userDetail)
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun findByActorId(actorId: Long): UserDetail? = query {
|
override suspend fun findByActorId(actorId: Long): UserDetail? = query {
|
||||||
return@query UserDetails
|
return@query UserDetails
|
||||||
|
@ -89,7 +100,7 @@ class UserDetailRepositoryImpl : UserDetailRepository, AbstractRepository() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun userDetail(it: ResultRow) = UserDetail.create(
|
private fun userDetail(it: ResultRow) = UserDetail(
|
||||||
UserDetailId(it[UserDetails.id]),
|
UserDetailId(it[UserDetails.id]),
|
||||||
ActorId(it[UserDetails.actorId]),
|
ActorId(it[UserDetails.actorId]),
|
||||||
UserDetailHashedPassword(it[UserDetails.password]),
|
UserDetailHashedPassword(it[UserDetails.password]),
|
||||||
|
|
|
@ -18,6 +18,7 @@ package dev.usbharu.hideout.core.infrastructure.springframework.domainevent
|
||||||
|
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEvent
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventPublisher
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.context.ApplicationEventPublisher
|
import org.springframework.context.ApplicationEventPublisher
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@ -25,6 +26,11 @@ import org.springframework.stereotype.Component
|
||||||
class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: ApplicationEventPublisher) :
|
class SpringFrameworkDomainEventPublisher(private val applicationEventPublisher: ApplicationEventPublisher) :
|
||||||
DomainEventPublisher {
|
DomainEventPublisher {
|
||||||
override suspend fun publishEvent(domainEvent: DomainEvent<*>) {
|
override suspend fun publishEvent(domainEvent: DomainEvent<*>) {
|
||||||
|
logger.trace("Publish ${domainEvent.id} ${domainEvent.name}")
|
||||||
applicationEventPublisher.publishEvent(domainEvent)
|
applicationEventPublisher.publishEvent(domainEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(SpringFrameworkDomainEventPublisher::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventC
|
||||||
import dev.usbharu.hideout.core.application.domainevent.subscribers.DomainEventSubscriber
|
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.DomainEvent
|
||||||
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventBody
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.context.event.EventListener
|
import org.springframework.context.event.EventListener
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@ -18,11 +19,17 @@ class SpringFrameworkDomainEventSubscriber : DomainEventSubscriber {
|
||||||
|
|
||||||
@EventListener
|
@EventListener
|
||||||
suspend fun onDomainEventPublished(domainEvent: DomainEvent<*>) {
|
suspend fun onDomainEventPublished(domainEvent: DomainEvent<*>) {
|
||||||
|
logger.trace("Domain Event Published: $domainEvent")
|
||||||
map[domainEvent.name]?.forEach {
|
map[domainEvent.name]?.forEach {
|
||||||
try {
|
try {
|
||||||
it.invoke(domainEvent)
|
it.invoke(domainEvent)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
logger.error("", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(SpringFrameworkDomainEventSubscriber::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue