Skip to content

Commit d05221c

Browse files
committed
ASAP-409 기존 "첫번째 행성이면 메인 행섬이다" 정책 제거
1 parent 0e6188d commit d05221c

File tree

7 files changed

+65
-52
lines changed

7 files changed

+65
-52
lines changed

Application-Module/src/main/kotlin/com/asap/application/space/service/SpaceCommandService.kt

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ class SpaceCommandService(
2424
private val spaceIndexValidator: SpaceIndexValidator = SpaceIndexValidator()
2525

2626
override fun create(command: CreateSpaceUsecase.Command) {
27+
val userId = DomainId(command.userId)
2728
Space
2829
.create(
29-
userId = DomainId(command.userId),
30+
userId = userId,
3031
name = command.spaceName,
3132
templateType = command.templateType,
3233
).apply {
3334
spaceManagementPort.save(this)
3435
}
35-
reIndexingSpaceOrder(DomainId(command.userId))
36+
37+
updateMainSpace(userId)
38+
reIndexingSpaceOrder(userId)
3639
}
3740

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

4851
override fun deleteOne(command: DeleteSpaceUsecase.DeleteOneCommand) {
52+
val userId = DomainId(command.userId)
4953
spaceManagementPort
5054
.getSpaceNotNull(
51-
userId = DomainId(command.userId),
55+
userId = userId,
5256
spaceId = DomainId(command.spaceId),
5357
).apply {
5458
delete()
5559
spaceManagementPort.deleteBy(this)
5660
}
57-
reIndexingSpaceOrder(DomainId(command.userId))
61+
updateMainSpace(userId)
62+
reIndexingSpaceOrder(userId)
5863
}
5964

6065
override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllCommand) {
66+
val userId = DomainId(command.userId)
6167
spaceManagementPort
6268
.getAllSpaceBy(
63-
userId = DomainId(command.userId),
69+
userId = userId,
6470
spaceIds = command.spaceIds.map { DomainId(it) },
6571
).forEach {
6672
it.delete()
6773
spaceManagementPort.deleteBy(it)
6874
}
69-
reIndexingSpaceOrder(DomainId(command.userId))
75+
updateMainSpace(userId)
76+
reIndexingSpaceOrder(userId)
7077
}
7178

7279
override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllUser) {
@@ -95,16 +102,6 @@ class SpaceCommandService(
95102
spaceManagementPort.saveAll(spaces)
96103
}
97104

98-
private fun reIndexingSpaceOrder(userId: DomainId) {
99-
spaceManagementPort
100-
.getAllSpaceBy(userId)
101-
.sortedBy { it.index }
102-
.forEachIndexed { index, indexedSpace ->
103-
indexedSpace.updateIndex(index)
104-
spaceManagementPort.update(indexedSpace)
105-
}
106-
}
107-
108105
override fun update(command: UpdateSpaceUsecase.Command.Main) {
109106
val spaces = spaceManagementPort.getAllSpaceBy(
110107
userId = DomainId(command.userId),
@@ -118,4 +115,29 @@ class SpaceCommandService(
118115

119116
spaceManagementPort.saveAll(spaces)
120117
}
118+
119+
private fun reIndexingSpaceOrder(userId: DomainId) {
120+
val spaces = spaceManagementPort
121+
.getAllSpaceBy(userId)
122+
.sortedBy { it.index }
123+
.onEachIndexed { index, space ->
124+
space.updateIndex(index)
125+
}
126+
spaceManagementPort.saveAll(spaces)
127+
}
128+
129+
130+
private fun updateMainSpace(userId: DomainId) {
131+
val spaces = spaceManagementPort.getAllSpaceBy(userId)
132+
133+
if (spaces.isEmpty()) {
134+
return
135+
}
136+
137+
if (spaces.size == 1) {
138+
spaces[0].updateToMain()
139+
spaceManagementPort.save(spaces[0])
140+
return
141+
}
142+
}
121143
}

Application-Module/src/test/kotlin/com/asap/application/letter/service/LetterQueryServiceTest.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import com.asap.application.letter.port.out.SpaceLetterManagementPort
77
import com.asap.application.space.port.out.SpaceManagementPort
88
import com.asap.application.user.port.out.UserManagementPort
99
import com.asap.domain.LetterFixture
10+
import com.asap.domain.SpaceFixture
1011
import com.asap.domain.UserFixture
1112
import com.asap.domain.common.DomainId
12-
import com.asap.domain.space.entity.Space
1313
import io.kotest.core.spec.style.BehaviorSpec
1414
import io.kotest.matchers.nulls.shouldNotBeNull
1515
import io.kotest.matchers.shouldBe
@@ -96,13 +96,9 @@ class LetterQueryServiceTest :
9696
letterId = "letter-id",
9797
userId = "user-id",
9898
)
99-
val space =
100-
Space(
101-
id = DomainId.generate(),
102-
name = "space-name",
103-
userId = DomainId(query.userId),
104-
templateType = 1,
105-
)
99+
val space = SpaceFixture.createSpace(
100+
userId = DomainId(query.userId),
101+
)
106102
val spaceLetter = LetterFixture.generateSpaceLetter(receiverId = DomainId(query.userId), spaceId = space.id)
107103
val prevSpaceLetter =
108104
LetterFixture.generateSpaceLetter(receiverId = DomainId(query.userId), spaceId = space.id)

Application-Module/src/test/kotlin/com/asap/application/space/service/SpaceCommandServiceTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class SpaceCommandServiceTest :
4949
spaceId = "spaceId",
5050
name = "newName",
5151
)
52-
val mockSpace =
53-
Space(
54-
id = DomainId(spaceUpdateNameCommand.spaceId),
55-
userId = DomainId(spaceUpdateNameCommand.userId),
56-
name = "oldName",
57-
templateType = 1,
58-
)
52+
val mockSpace = SpaceFixture.createSpace(
53+
id = DomainId(spaceUpdateNameCommand.spaceId),
54+
userId = DomainId(spaceUpdateNameCommand.userId),
55+
name = "oldName",
56+
templateType = 1,
57+
)
58+
5959
every {
6060
spaceManagementPort.getSpaceNotNull(
6161
userId = DomainId(spaceUpdateNameCommand.userId),

Application-Module/src/test/kotlin/com/asap/application/space/service/SpaceQueryServiceTest.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.asap.domain.SpaceFixture
99
import com.asap.domain.UserFixture
1010
import com.asap.domain.common.DomainId
1111
import com.asap.domain.space.entity.MainSpace
12-
import com.asap.domain.space.entity.Space
1312
import io.kotest.core.spec.style.BehaviorSpec
1413
import io.kotest.matchers.nulls.shouldNotBeNull
1514
import io.kotest.matchers.shouldBe
@@ -40,13 +39,9 @@ class SpaceQueryServiceTest :
4039
GetMainSpaceUsecase.Query(
4140
userId = user.id.value,
4241
)
43-
val space =
44-
Space(
45-
id = mainSpace.id,
46-
name = "name",
47-
userId = user.id,
48-
templateType = 1,
49-
)
42+
val space = SpaceFixture.createSpace(
43+
userId = user.id,
44+
)
5045
every { spaceManagementPort.getMainSpace(any()) } returns mainSpace
5146
every { userManagementPort.getUserNotNull(any()) } returns user
5247
every { spaceManagementPort.getSpaceNotNull(any(), any()) } returns space
@@ -123,13 +118,10 @@ class SpaceQueryServiceTest :
123118
}
124119

125120
given("행성 조회 요청이 들어왔을 때") {
126-
val space =
127-
Space(
128-
id = DomainId.generate(),
129-
name = "name",
130-
userId = DomainId("userId"),
131-
templateType = 1,
132-
)
121+
val space = SpaceFixture.createSpace(
122+
id = DomainId("spaceId"),
123+
userId = DomainId("userId"),
124+
)
133125
val query =
134126
GetSpaceUsecase.GetQuery(
135127
userId = "userId",

Application-Module/src/testFixtures/kotlin/com/asap/application/space/SpaceMockManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ class SpaceMockManager(
1010
fun settingSpace(
1111
userId: String,
1212
index: Int = 0,
13+
isMain: Boolean = false,
1314
): Space {
1415
val space =
1516
Space.create(
1617
userId = DomainId(userId),
1718
name = "test",
1819
templateType = 0,
20+
index = index,
1921
)
22+
if(isMain) space.updateToMain()
23+
2024
return spaceManagementPort.save(space).also {
2125
spaceManagementPort.getSpaceNotNull(DomainId(userId), it.id).apply {
2226
updateIndex(index)

Bootstrap-Module/src/test/kotlin/com/asap/bootstrap/integration/space/SpaceApiIntegrationTest.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SpaceApiIntegrationTest(
3333
// given
3434
val userId = userMockManager.settingUser()
3535
val accessToken = jwtMockManager.generateAccessToken(userId)
36-
spaceMockManager.settingSpace(userId)
36+
spaceMockManager.settingSpace(userId, isMain = true)
3737
// when
3838
val response =
3939
mockMvc.get("/api/v1/spaces/main") {
@@ -140,13 +140,10 @@ class SpaceApiIntegrationTest(
140140
}
141141

142142
@Test
143-
fun createSpace_and_get_main_space() {
143+
fun create_first_space_and_get_main_space() {
144144
// given
145145
val userId = userMockManager.settingUser()
146146
val accessToken = jwtMockManager.generateAccessToken(userId)
147-
(0..2).forEach {
148-
spaceMockManager.settingSpace(userId)
149-
}
150147
val request =
151148
CreateSpaceRequest(
152149
spaceName = "createdSpace",

Domain-Module/src/main/kotlin/com/asap/domain/space/entity/Space.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Space(
88
id: DomainId,
99
val userId: DomainId,
1010
var name: String,
11-
var index: Int = 0,
11+
var index: Int,
1212
val templateType: Int,
1313
var isMain: Boolean = false,
1414
) : Aggregate<Space>(id) {
@@ -18,12 +18,14 @@ class Space(
1818
userId: DomainId,
1919
name: String,
2020
templateType: Int,
21+
index: Int = -1,
2122
): Space =
2223
Space(
2324
id = id,
2425
userId = userId,
2526
name = name,
2627
templateType = templateType,
28+
index = index,
2729
).also {
2830
it.registerEvent(SpaceEvent.SpaceCreatedEvent(it))
2931
}

0 commit comments

Comments
 (0)