Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit b05757c

Browse files
authored
Merge pull request #119 from ASAP-Lettering/ASAP-409
ASAP-409 is main 수정 api 추가
2 parents 2364503 + 92dd716 commit b05757c

20 files changed

Lines changed: 348 additions & 360 deletions

File tree

Application-Module/src/main/kotlin/com/asap/application/space/port/in/UpdateSpaceIndexUsecase.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.asap.application.space.port.`in`
2+
3+
interface UpdateSpaceUsecase {
4+
fun update(command: Command.Index)
5+
6+
fun update(command: Command.Main)
7+
8+
9+
sealed class Command {
10+
data class Index(
11+
val userId: String,
12+
val orders: List<SpaceOrder>,
13+
) : Command()
14+
15+
data class SpaceOrder(
16+
val spaceId: String,
17+
val index: Int,
18+
)
19+
20+
data class Main(
21+
val userId: String,
22+
val spaceId: String,
23+
) : Command()
24+
}
25+
}

Application-Module/src/main/kotlin/com/asap/application/space/port/out/SpaceManagementPort.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.asap.application.space.port.out
22

33
import com.asap.domain.common.DomainId
4-
import com.asap.domain.space.entity.IndexedSpace
54
import com.asap.domain.space.entity.MainSpace
65
import com.asap.domain.space.entity.Space
76

@@ -13,13 +12,6 @@ interface SpaceManagementPort {
1312
spaceId: DomainId,
1413
): Space
1514

16-
fun getIndexedSpaceNotNull(
17-
userId: DomainId,
18-
spaceId: DomainId,
19-
): IndexedSpace
20-
21-
fun getAllIndexedSpace(userId: DomainId): List<IndexedSpace>
22-
2315
fun getAllSpaceBy(
2416
userId: DomainId,
2517
spaceIds: List<DomainId>,
@@ -29,14 +21,9 @@ interface SpaceManagementPort {
2921

3022
fun save(space: Space): Space
3123

32-
fun update(space: Space): Space
33-
34-
fun update(indexedSpace: IndexedSpace): IndexedSpace
24+
fun saveAll(spaces: List<Space>): List<Space>
3525

36-
fun updateIndexes(
37-
userId: DomainId,
38-
orders: List<IndexedSpace>,
39-
)
26+
fun update(space: Space): Space
4027

4128
fun deleteBy(space: Space)
4229

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

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.asap.application.space.service
33
import com.asap.application.space.exception.SpaceException
44
import com.asap.application.space.port.`in`.CreateSpaceUsecase
55
import com.asap.application.space.port.`in`.DeleteSpaceUsecase
6-
import com.asap.application.space.port.`in`.UpdateSpaceIndexUsecase
76
import com.asap.application.space.port.`in`.UpdateSpaceNameUsecase
7+
import com.asap.application.space.port.`in`.UpdateSpaceUsecase
88
import com.asap.application.space.port.out.SpaceManagementPort
99
import com.asap.common.exception.DefaultException
1010
import com.asap.domain.common.DomainId
@@ -20,53 +20,53 @@ class SpaceCommandService(
2020
) : CreateSpaceUsecase,
2121
UpdateSpaceNameUsecase,
2222
DeleteSpaceUsecase,
23-
UpdateSpaceIndexUsecase {
23+
UpdateSpaceUsecase {
2424
private val spaceIndexValidator: SpaceIndexValidator = SpaceIndexValidator()
2525

2626
override fun create(command: CreateSpaceUsecase.Command) {
27-
Space
28-
.create(
29-
userId = DomainId(command.userId),
30-
name = command.spaceName,
31-
templateType = command.templateType,
32-
).apply {
33-
spaceManagementPort.save(this)
34-
}
35-
reIndexingSpaceOrder(DomainId(command.userId))
36-
}
27+
val userId = DomainId(command.userId)
28+
Space.create(
29+
userId = userId,
30+
name = command.spaceName,
31+
templateType = command.templateType,
32+
).apply {
33+
spaceManagementPort.save(this)
34+
}
3735

38-
override fun update(command: UpdateSpaceNameUsecase.Command) {
39-
val space =
40-
spaceManagementPort.getSpaceNotNull(
41-
userId = DomainId(command.userId),
42-
spaceId = DomainId(command.spaceId),
43-
)
44-
space.updateName(command.name)
45-
spaceManagementPort.update(space)
36+
// TODO 동시성 문제가 발생한다면?
37+
if (spaceManagementPort.countByUserId(userId) == 1L) {
38+
updateMainSpace(userId)
39+
}
40+
41+
reIndexingSpaceOrder(userId)
4642
}
4743

4844
override fun deleteOne(command: DeleteSpaceUsecase.DeleteOneCommand) {
45+
val userId = DomainId(command.userId)
4946
spaceManagementPort
5047
.getSpaceNotNull(
51-
userId = DomainId(command.userId),
48+
userId = userId,
5249
spaceId = DomainId(command.spaceId),
5350
).apply {
5451
delete()
5552
spaceManagementPort.deleteBy(this)
5653
}
57-
reIndexingSpaceOrder(DomainId(command.userId))
54+
updateMainSpace(userId)
55+
reIndexingSpaceOrder(userId)
5856
}
5957

6058
override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllCommand) {
59+
val userId = DomainId(command.userId)
6160
spaceManagementPort
6261
.getAllSpaceBy(
63-
userId = DomainId(command.userId),
62+
userId = userId,
6463
spaceIds = command.spaceIds.map { DomainId(it) },
6564
).forEach {
6665
it.delete()
6766
spaceManagementPort.deleteBy(it)
6867
}
69-
reIndexingSpaceOrder(DomainId(command.userId))
68+
updateMainSpace(userId)
69+
reIndexingSpaceOrder(userId)
7070
}
7171

7272
override fun deleteAllBy(command: DeleteSpaceUsecase.DeleteAllUser) {
@@ -76,35 +76,71 @@ class SpaceCommandService(
7676
}
7777
}
7878

79-
override fun update(command: UpdateSpaceIndexUsecase.Command) {
80-
val indexedSpaces = spaceManagementPort.getAllIndexedSpace(DomainId(command.userId))
79+
override fun update(command: UpdateSpaceUsecase.Command.Index) {
80+
val spaces = spaceManagementPort.getAllSpaceBy(DomainId(command.userId))
8181
val changeIndexMap = command.orders.associateBy({ DomainId(it.spaceId) }, { it.index })
8282

8383
try {
8484
spaceIndexValidator.validate(
85-
indexedSpaces = indexedSpaces,
85+
spaces = spaces,
8686
validateIndex = changeIndexMap,
8787
)
8888
} catch (e: DefaultException.InvalidArgumentException) {
89-
throw SpaceException.InvalidSpaceUpdateException()
89+
throw SpaceException.InvalidSpaceUpdateException(message = e.message)
9090
}
9191

92-
indexedSpaces.map {
92+
spaces.map {
9393
it.updateIndex(changeIndexMap.getValue(it.id))
9494
}
95-
spaceManagementPort.updateIndexes(
95+
spaceManagementPort.saveAll(spaces)
96+
}
97+
98+
override fun update(command: UpdateSpaceUsecase.Command.Main) {
99+
val spaces = spaceManagementPort.getAllSpaceBy(
96100
userId = DomainId(command.userId),
97-
orders = indexedSpaces,
98-
)
101+
).onEach {
102+
if (it.id.value == command.spaceId) {
103+
it.updateToMain()
104+
} else {
105+
it.updateToSub()
106+
}
107+
}
108+
109+
spaceManagementPort.saveAll(spaces)
110+
}
111+
112+
override fun update(command: UpdateSpaceNameUsecase.Command) {
113+
val space =
114+
spaceManagementPort.getSpaceNotNull(
115+
userId = DomainId(command.userId),
116+
spaceId = DomainId(command.spaceId),
117+
)
118+
119+
space.updateName(command.name)
120+
spaceManagementPort.update(space)
99121
}
100122

101123
private fun reIndexingSpaceOrder(userId: DomainId) {
102-
spaceManagementPort
103-
.getAllIndexedSpace(userId)
124+
val spaces = spaceManagementPort
125+
.getAllSpaceBy(userId)
104126
.sortedBy { it.index }
105-
.forEachIndexed { index, indexedSpace ->
106-
indexedSpace.updateIndex(index)
107-
spaceManagementPort.update(indexedSpace)
127+
.onEachIndexed { index, space ->
128+
space.updateIndex(index)
108129
}
130+
spaceManagementPort.saveAll(spaces)
131+
}
132+
133+
134+
private fun updateMainSpace(userId: DomainId) {
135+
val spaces = spaceManagementPort.getAllSpaceBy(userId)
136+
137+
if (spaces.isEmpty()) {
138+
return
139+
}
140+
141+
spaces.forEach { it.updateToSub() }
142+
spaces.first().updateToMain()
143+
144+
spaceManagementPort.saveAll(spaces)
109145
}
110146
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class SpaceQueryService(
3737

3838
override fun getAll(query: GetSpaceUsecase.GetAllQuery): GetSpaceUsecase.GetAllResponse {
3939
val spaces =
40-
spaceManagementPort.getAllIndexedSpace(
40+
spaceManagementPort.getAllSpaceBy(
4141
userId = DomainId(query.userId),
4242
)
4343

@@ -47,7 +47,7 @@ class SpaceQueryService(
4747
GetSpaceUsecase.SpaceDetail(
4848
spaceName = it.name,
4949
letterCount = spaceLetterManagementPort.countSpaceLetterBy(it.id, DomainId(query.userId)),
50-
isMainSpace = it.isMain(),
50+
isMainSpace = it.isMain,
5151
spaceIndex = it.index,
5252
spaceId = it.id.value,
5353
)

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)

0 commit comments

Comments
 (0)