Skip to content

Commit

Permalink
Merge pull request #101 from ASAP-Lettering/ASAP-238
Browse files Browse the repository at this point in the history
ASAP-238 refactor: 스페이스 이벤트를 도메인 안에 저장한 다음 영속화할 때 배포하도록 수정
  • Loading branch information
tlarbals824 authored Oct 20, 2024
2 parents 5ef5399 + 4301d8f commit f0da549
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 97 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ interface SpaceManagementPort {

fun getAllIndexedSpace(userId: DomainId): List<IndexedSpace>

fun getAllSpaceBy(
userId: DomainId,
spaceIds: List<DomainId>,
): List<Space>

fun getAllSpaceBy(userId: DomainId): List<Space>

fun save(space: Space): Space

fun update(space: Space): Space
Expand All @@ -31,17 +38,7 @@ interface SpaceManagementPort {
orders: List<IndexedSpace>,
)

fun deleteById(
userId: DomainId,
spaceId: DomainId,
)

fun deleteAllBySpaceIds(
userId: DomainId,
spaceIds: List<DomainId>,
)

fun deleteAllByUserId(userId: DomainId)
fun deleteBy(space: Space)

fun countByUserId(userId: DomainId): Long
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.asap.application.space.service

import com.asap.application.space.event.SpaceDeletedEvent
import com.asap.application.space.exception.SpaceException
import com.asap.application.space.port.`in`.CreateSpaceUsecase
import com.asap.application.space.port.`in`.DeleteSpaceUsecase
Expand All @@ -11,15 +10,13 @@ import com.asap.common.exception.DefaultException
import com.asap.domain.common.DomainId
import com.asap.domain.space.entity.Space
import com.asap.domain.space.service.SpaceIndexValidator
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional
class SpaceCommandService(
private val spaceManagementPort: SpaceManagementPort,
private val applicationEventPublisher: ApplicationEventPublisher,
) : CreateSpaceUsecase,
UpdateSpaceNameUsecase,
DeleteSpaceUsecase,
Expand All @@ -44,42 +41,39 @@ class SpaceCommandService(
userId = DomainId(command.userId),
spaceId = DomainId(command.spaceId),
)
val updatedSpace = space.updateName(command.name)
spaceManagementPort.update(updatedSpace)
space.updateName(command.name)
spaceManagementPort.update(space)
}

override fun deleteOne(command: DeleteSpaceUsecase.DeleteOneCommand) {
spaceManagementPort.deleteById(
userId = DomainId(command.userId),
spaceId = DomainId(command.spaceId),
)
applicationEventPublisher.publishEvent(
SpaceDeletedEvent(
userId = command.userId,
spaceId = command.spaceId,
),
)
spaceManagementPort
.getSpaceNotNull(
userId = DomainId(command.userId),
spaceId = DomainId(command.spaceId),
).apply {
delete()
spaceManagementPort.deleteBy(this)
}
reIndexingSpaceOrder(DomainId(command.userId))
}

override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllCommand) {
spaceManagementPort.deleteAllBySpaceIds(
userId = DomainId(command.userId),
spaceIds = command.spaceIds.map { DomainId(it) },
)
command.spaceIds.forEach {
applicationEventPublisher.publishEvent(
SpaceDeletedEvent(
userId = command.userId,
spaceId = it,
),
)
}
spaceManagementPort
.getAllSpaceBy(
userId = DomainId(command.userId),
spaceIds = command.spaceIds.map { DomainId(it) },
).forEach {
it.delete()
spaceManagementPort.deleteBy(it)
}
reIndexingSpaceOrder(DomainId(command.userId))
}

override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllUser) {
spaceManagementPort.deleteAllByUserId(DomainId(command.userId))
spaceManagementPort.getAllSpaceBy(DomainId(command.userId)).forEach {
it.delete()
spaceManagementPort.deleteBy(it)
}
}

override fun update(command: UpdateSpaceIndexUsecase.Command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import io.kotest.core.spec.style.BehaviorSpec
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.springframework.context.ApplicationEventPublisher

class SpaceCommandServiceTest :
BehaviorSpec({

val spaceManagementPort = mockk<SpaceManagementPort>(relaxed = true)
val applicationEventPublisher = mockk<ApplicationEventPublisher>(relaxed = true)

val spaceCommandService =
SpaceCommandService(
spaceManagementPort,
applicationEventPublisher,
)

given("스페이스 생성 요청이 들어왔을 때") {
Expand Down Expand Up @@ -75,7 +72,7 @@ class SpaceCommandServiceTest :
)
}
verify {
spaceManagementPort.update(mockSpace.updateName(spaceUpdateNameCommand.name))
spaceManagementPort.update(mockSpace)
}
}
}
Expand All @@ -91,9 +88,8 @@ class SpaceCommandServiceTest :
spaceCommandService.deleteOne(spaceDeleteOneCommand)
then("스페이스를 삭제한다") {
verify {
spaceManagementPort.deleteById(
userId = DomainId(spaceDeleteOneCommand.userId),
spaceId = DomainId(spaceDeleteOneCommand.spaceId),
spaceManagementPort.deleteBy(
any(Space::class),
)
}
}
Expand All @@ -108,9 +104,8 @@ class SpaceCommandServiceTest :
spaceCommandService.deleteAllBy(spaceDeleteAllCommand)
then("여러 스페이스를 삭제한다") {
verify {
spaceManagementPort.deleteAllBySpaceIds(
userId = DomainId(spaceDeleteAllCommand.userId),
spaceIds = spaceDeleteAllCommand.spaceIds.map { DomainId(it) },
spaceManagementPort.deleteBy(
any(Space::class),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.asap.bootstrap.letter.handler

import com.asap.application.letter.port.`in`.RemoveLetterUsecase
import com.asap.application.space.event.SpaceDeletedEvent
import com.asap.domain.space.event.SpaceEvent
import org.springframework.context.event.EventListener
import org.springframework.web.bind.annotation.RestController

Expand All @@ -10,12 +10,12 @@ class SpaceLetterEventHandler(
private val removeLetterUsecase: RemoveLetterUsecase,
) {
@EventListener
fun deleteBySpace(event: SpaceDeletedEvent) {
fun deleteBySpace(event: SpaceEvent.SpaceDeletedEvent) {
removeLetterUsecase.removeSpaceLetterBy(
command =
RemoveLetterUsecase.Command.SpaceId(
userId = event.userId,
spaceId = event.spaceId,
userId = event.space.userId.value,
spaceId = event.space.id.value,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ class SpaceApiIntegrationTest : IntegrationSupporter() {
// given
val userId = userMockManager.settingUser()
val accessToken = jwtMockManager.generateAccessToken(userId)
val spaceId = spaceMockManager.settingSpace(userId)
val space = spaceMockManager.settingSpace(userId)
// when
val response =
mockMvc.delete("/api/v1/spaces/$spaceId") {
mockMvc.delete("/api/v1/spaces/${space.id.value}") {
header("Authorization", "Bearer $accessToken")
}
// then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.asap.domain.space.entity

import com.asap.domain.common.Aggregate
import com.asap.domain.common.DomainId

data class IndexedSpace(
val id: DomainId,
class IndexedSpace(
id: DomainId,
val userId: DomainId,
val name: String,
var index: Int,
val templateType: Int,
) {
) : Aggregate<IndexedSpace>(id) {
fun isMain(): Boolean = index == 0

fun updateIndex(index: Int) {
fun updateIndex(index: Int) {
check(index >= 0) { "Index must be greater than or equal to 0" }
this.index = index
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package com.asap.domain.space.entity

import com.asap.domain.common.Aggregate
import com.asap.domain.common.DomainId
import com.asap.domain.space.event.SpaceEvent

data class Space(
val id: DomainId = DomainId.generate(),
class Space(
id: DomainId,
val userId: DomainId,
val name: String,
var name: String,
val templateType: Int,
) {
) : Aggregate<Space>(id) {
companion object {
fun create(
id: DomainId = DomainId.generate(),
userId: DomainId,
name: String,
templateType: Int,
): Space =
Space(
id = id,
userId = userId,
name = name,
templateType = templateType,
)
).also {
it.registerEvent(SpaceEvent.SpaceCreatedEvent(it))
}
}

fun updateName(name: String): Space = this.copy(name = name)
fun updateName(name: String) {
this.name = name
}

fun delete() {
this.registerEvent(SpaceEvent.SpaceDeletedEvent(this))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.asap.domain.space.event

import com.asap.domain.common.DomainEvent
import com.asap.domain.space.entity.Space

sealed class SpaceEvent : DomainEvent<Space> {
data class SpaceCreatedEvent(
val space: Space,
) : SpaceEvent()

data class SpaceDeletedEvent(
val space: Space,
) : SpaceEvent()
}
Loading

0 comments on commit f0da549

Please sign in to comment.