Skip to content

Commit

Permalink
ASAP-409 기존 "첫번째 행성이면 메인 행섬이다" 정책 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
tlarbals824 committed Feb 8, 2025
1 parent 0e6188d commit d05221c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class SpaceCommandService(
private val spaceIndexValidator: SpaceIndexValidator = SpaceIndexValidator()

override fun create(command: CreateSpaceUsecase.Command) {
val userId = DomainId(command.userId)
Space
.create(
userId = DomainId(command.userId),
userId = userId,
name = command.spaceName,
templateType = command.templateType,
).apply {
spaceManagementPort.save(this)
}
reIndexingSpaceOrder(DomainId(command.userId))

updateMainSpace(userId)
reIndexingSpaceOrder(userId)
}

override fun update(command: UpdateSpaceNameUsecase.Command) {
Expand All @@ -46,27 +49,31 @@ class SpaceCommandService(
}

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

override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllCommand) {
val userId = DomainId(command.userId)
spaceManagementPort
.getAllSpaceBy(
userId = DomainId(command.userId),
userId = userId,
spaceIds = command.spaceIds.map { DomainId(it) },
).forEach {
it.delete()
spaceManagementPort.deleteBy(it)
}
reIndexingSpaceOrder(DomainId(command.userId))
updateMainSpace(userId)
reIndexingSpaceOrder(userId)
}

override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllUser) {
Expand Down Expand Up @@ -95,16 +102,6 @@ class SpaceCommandService(
spaceManagementPort.saveAll(spaces)
}

private fun reIndexingSpaceOrder(userId: DomainId) {
spaceManagementPort
.getAllSpaceBy(userId)
.sortedBy { it.index }
.forEachIndexed { index, indexedSpace ->
indexedSpace.updateIndex(index)
spaceManagementPort.update(indexedSpace)
}
}

override fun update(command: UpdateSpaceUsecase.Command.Main) {
val spaces = spaceManagementPort.getAllSpaceBy(
userId = DomainId(command.userId),
Expand All @@ -118,4 +115,29 @@ class SpaceCommandService(

spaceManagementPort.saveAll(spaces)
}

private fun reIndexingSpaceOrder(userId: DomainId) {
val spaces = spaceManagementPort
.getAllSpaceBy(userId)
.sortedBy { it.index }
.onEachIndexed { index, space ->
space.updateIndex(index)
}
spaceManagementPort.saveAll(spaces)
}


private fun updateMainSpace(userId: DomainId) {
val spaces = spaceManagementPort.getAllSpaceBy(userId)

if (spaces.isEmpty()) {
return
}

if (spaces.size == 1) {
spaces[0].updateToMain()
spaceManagementPort.save(spaces[0])
return
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.asap.application.letter.port.out.SpaceLetterManagementPort
import com.asap.application.space.port.out.SpaceManagementPort
import com.asap.application.user.port.out.UserManagementPort
import com.asap.domain.LetterFixture
import com.asap.domain.SpaceFixture
import com.asap.domain.UserFixture
import com.asap.domain.common.DomainId
import com.asap.domain.space.entity.Space
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -96,13 +96,9 @@ class LetterQueryServiceTest :
letterId = "letter-id",
userId = "user-id",
)
val space =
Space(
id = DomainId.generate(),
name = "space-name",
userId = DomainId(query.userId),
templateType = 1,
)
val space = SpaceFixture.createSpace(
userId = DomainId(query.userId),
)
val spaceLetter = LetterFixture.generateSpaceLetter(receiverId = DomainId(query.userId), spaceId = space.id)
val prevSpaceLetter =
LetterFixture.generateSpaceLetter(receiverId = DomainId(query.userId), spaceId = space.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ class SpaceCommandServiceTest :
spaceId = "spaceId",
name = "newName",
)
val mockSpace =
Space(
id = DomainId(spaceUpdateNameCommand.spaceId),
userId = DomainId(spaceUpdateNameCommand.userId),
name = "oldName",
templateType = 1,
)
val mockSpace = SpaceFixture.createSpace(
id = DomainId(spaceUpdateNameCommand.spaceId),
userId = DomainId(spaceUpdateNameCommand.userId),
name = "oldName",
templateType = 1,
)

every {
spaceManagementPort.getSpaceNotNull(
userId = DomainId(spaceUpdateNameCommand.userId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.asap.domain.SpaceFixture
import com.asap.domain.UserFixture
import com.asap.domain.common.DomainId
import com.asap.domain.space.entity.MainSpace
import com.asap.domain.space.entity.Space
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -40,13 +39,9 @@ class SpaceQueryServiceTest :
GetMainSpaceUsecase.Query(
userId = user.id.value,
)
val space =
Space(
id = mainSpace.id,
name = "name",
userId = user.id,
templateType = 1,
)
val space = SpaceFixture.createSpace(
userId = user.id,
)
every { spaceManagementPort.getMainSpace(any()) } returns mainSpace
every { userManagementPort.getUserNotNull(any()) } returns user
every { spaceManagementPort.getSpaceNotNull(any(), any()) } returns space
Expand Down Expand Up @@ -123,13 +118,10 @@ class SpaceQueryServiceTest :
}

given("행성 조회 요청이 들어왔을 때") {
val space =
Space(
id = DomainId.generate(),
name = "name",
userId = DomainId("userId"),
templateType = 1,
)
val space = SpaceFixture.createSpace(
id = DomainId("spaceId"),
userId = DomainId("userId"),
)
val query =
GetSpaceUsecase.GetQuery(
userId = "userId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ class SpaceMockManager(
fun settingSpace(
userId: String,
index: Int = 0,
isMain: Boolean = false,
): Space {
val space =
Space.create(
userId = DomainId(userId),
name = "test",
templateType = 0,
index = index,
)
if(isMain) space.updateToMain()

return spaceManagementPort.save(space).also {
spaceManagementPort.getSpaceNotNull(DomainId(userId), it.id).apply {
updateIndex(index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SpaceApiIntegrationTest(
// given
val userId = userMockManager.settingUser()
val accessToken = jwtMockManager.generateAccessToken(userId)
spaceMockManager.settingSpace(userId)
spaceMockManager.settingSpace(userId, isMain = true)
// when
val response =
mockMvc.get("/api/v1/spaces/main") {
Expand Down Expand Up @@ -140,13 +140,10 @@ class SpaceApiIntegrationTest(
}

@Test
fun createSpace_and_get_main_space() {
fun create_first_space_and_get_main_space() {
// given
val userId = userMockManager.settingUser()
val accessToken = jwtMockManager.generateAccessToken(userId)
(0..2).forEach {
spaceMockManager.settingSpace(userId)
}
val request =
CreateSpaceRequest(
spaceName = "createdSpace",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Space(
id: DomainId,
val userId: DomainId,
var name: String,
var index: Int = 0,
var index: Int,
val templateType: Int,
var isMain: Boolean = false,
) : Aggregate<Space>(id) {
Expand All @@ -18,12 +18,14 @@ class Space(
userId: DomainId,
name: String,
templateType: Int,
index: Int = -1,
): Space =
Space(
id = id,
userId = userId,
name = name,
templateType = templateType,
index = index,
).also {
it.registerEvent(SpaceEvent.SpaceCreatedEvent(it))
}
Expand Down

0 comments on commit d05221c

Please sign in to comment.