Skip to content

Commit 5744326

Browse files
authored
๐Ÿ”€ :: (#71) ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™” ๋ณ€๊ฒฝ
๐Ÿ”€ :: (#71) ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™” ๋ณ€๊ฒฝ
2 parents 4b16de5 + e00ac46 commit 5744326

File tree

13 files changed

+113
-166
lines changed

13 files changed

+113
-166
lines changed

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/error/ErrorCode.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ enum class ErrorCode(
2222

2323
INTERNAL_SERVER_ERROR(500, "GLOBAL-500-1", "Internal Server Error."),
2424

25-
DEVICE_TOKEN_LENGTH(401, "DEVICE-400-1", "Device Token Length Error.")
25+
DEVICE_TOKEN_LENGTH(401, "DEVICE-400-1", "Device Token Length Error."),
26+
27+
SETTING_NOT_FOUND(404, "SETTING-404-1", "Setting Not Found.")
2628
}

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/setting/api/SettingApi.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import io.github.v1servicenotification.category.api.response.CategoryListRespons
44
import java.util.UUID
55

66
interface SettingApi {
7-
fun activateCategory(categoryId: UUID, userId: UUID): Int
8-
fun deActivateCategory(categoryId: UUID, userId: UUID): Int
7+
fun activateOrDeActivateCategory(isActivate: Boolean, topic: String, userId: UUID)
98
fun queryActivatedCategory(userId: UUID): CategoryListResponse
109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.v1servicenotification.setting.exception
2+
3+
import io.github.v1servicenotification.error.ErrorCode
4+
import io.github.v1servicenotification.error.NotificationException
5+
6+
class SettingNotFoundException private constructor(): NotificationException(ErrorCode.CATEGORY_NOT_FOUND) {
7+
companion object {
8+
@JvmField
9+
val EXCEPTION = SettingNotFoundException()
10+
}
11+
}

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/setting/service/SettingApiImpl.kt

+7-25
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,23 @@ import io.github.v1servicenotification.category.api.response.CategoryElement
55
import io.github.v1servicenotification.category.api.response.CategoryListResponse
66
import io.github.v1servicenotification.setting.spi.SettingRepositorySpi
77
import io.github.v1servicenotification.setting.api.SettingApi
8+
import io.github.v1servicenotification.setting.exception.SettingNotFoundException
89
import io.github.v1servicenotification.setting.spi.SettingCategorySpi
910
import java.util.UUID
1011

1112
@DomainService
1213
class SettingApiImpl(
1314
private val settingRepositorySpi: SettingRepositorySpi,
1415
private val settingCategorySpi: SettingCategorySpi
15-
): SettingApi {
16+
) : SettingApi {
1617

17-
override fun activateCategory(categoryId: UUID, userId: UUID): Int {
18-
return saveOrUpdateSetting(
19-
categoryId = categoryId,
20-
userId = userId,
21-
isActivate = true
22-
)
23-
}
24-
25-
override fun deActivateCategory(categoryId: UUID, userId: UUID): Int {
26-
return saveOrUpdateSetting(
27-
categoryId = categoryId,
28-
userId = userId,
29-
isActivate = false
30-
)
31-
}
32-
33-
private fun saveOrUpdateSetting(categoryId: UUID, userId: UUID, isActivate: Boolean): Int {
34-
val category = settingCategorySpi.findById(categoryId)
18+
override fun activateOrDeActivateCategory(isActivate: Boolean, topic: String, userId: UUID) {
19+
val category = settingCategorySpi.findByStartingWithTopic(topic)
3520

36-
return if (settingRepositorySpi.settingExist(category, userId)) {
37-
settingRepositorySpi.updateSetting(category, userId, isActivate)
38-
204
39-
} else {
40-
settingRepositorySpi.saveSetting(category, userId, isActivate)
41-
201
21+
if (!settingRepositorySpi.settingExist(category, userId)) {
22+
throw SettingNotFoundException.EXCEPTION
4223
}
24+
settingRepositorySpi.updateAllSetting(category, userId, isActivate)
4325
}
4426

4527
override fun queryActivatedCategory(userId: UUID): CategoryListResponse {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.github.v1servicenotification.setting.spi
22

3-
import io.github.v1servicenotification.category.Category
4-
import java.util.UUID
3+
import java.util.*
54

65
interface SettingCategorySpi {
7-
fun findById(id: UUID): Category
6+
fun findByStartingWithTopic(topic: String): List<UUID>
87
}

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingRepositorySpi.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package io.github.v1servicenotification.setting.spi
22

33
import io.github.v1servicenotification.annotation.Spi
44
import io.github.v1servicenotification.category.Category
5-
import io.github.v1servicenotification.setting.Setting
65
import java.util.UUID
76

87
@Spi
98
interface SettingRepositorySpi {
10-
fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting
11-
fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting
12-
fun settingExist(category: Category, userId: UUID): Boolean
9+
fun updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean)
10+
fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean
1311
fun queryActivatedCategory(userId: UUID): List<Category>
1412
}

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemoryCategoryRepository.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ class InMemoryCategoryRepository(
3434
?: throw CategoryNotFoundException.EXCEPTION
3535
}
3636

37-
override fun findById(id: UUID): Category {
38-
return categoryMap[id]
39-
?: throw CategoryNotFoundException.EXCEPTION
40-
}
41-
4237
override fun findAllByDefaultActivated(defaultActivated: Boolean): List<Category> {
4338
return categoryMap.filter {
4439
it.value.defaultActivated == defaultActivated
4540
}.map { it.value }
4641
}
42+
43+
override fun findByStartingWithTopic(topic: String): List<UUID> {
44+
return categoryMap.filter {
45+
it.value.topic.startsWith(topic)
46+
}.map {
47+
it.key
48+
}
49+
}
4750
}

โ€Žnotification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemorySettingRepository.kt

+13-23
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,16 @@ class InMemorySettingRepository(
1515
categoryMap[category.id] = category
1616
}
1717

18-
fun findSetting(userId: UUID, categoryId: UUID): Setting? {
19-
return settingMap
20-
.filter { it.value.userId == userId && it.value.notificationCategoryId == categoryId }
21-
.map { it.value }.firstOrNull()
22-
}
23-
24-
override fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
25-
val setting = Setting(userId, category.id, isActivated)
26-
settingMap[UUID.randomUUID()] = setting
27-
28-
return setting
29-
}
3018

31-
override fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
32-
return settingMap.filter {
33-
it.value.notificationCategoryId == category.id && it.value.userId == userId
34-
}.map {
35-
it.value.changeIsActivate(isActivated)
36-
it.value
37-
}[0]
19+
override fun updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean) {
20+
categoryIds.forEach {
21+
val setting = findSetting(userId, it)
22+
setting?.changeIsActivate(isActivated)
23+
}
3824
}
3925

40-
override fun settingExist(category: Category, userId: UUID): Boolean {
41-
return settingMap.filter {
42-
it.value.notificationCategoryId == category.id && it.value.userId == userId
43-
}.isNotEmpty()
26+
override fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean {
27+
return categoryIds.map { findSetting(userId, it) }.any { it != null }
4428
}
4529

4630
override fun queryActivatedCategory(userId: UUID): List<Category> {
@@ -50,6 +34,12 @@ class InMemorySettingRepository(
5034
}.map { it.value }
5135
}
5236

37+
private fun findSetting(userId: UUID, categoryId: UUID): Setting? {
38+
return settingMap
39+
.filter { it.value.userId == userId && it.value.notificationCategoryId == categoryId }
40+
.map { it.value }.firstOrNull()
41+
}
42+
5343
override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List<UUID> {
5444
return settingMap.values.filter {
5545
it.isActivated == isActivated && categoryMap[it.notificationCategoryId]?.topic == topic

โ€Žnotification-domain/src/test/kotlin/io/github/v1servicenotification/detail/DetailApiImplTest.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ class DetailApiImplTest {
6767
topic = "ALL"
6868
)
6969

70+
val categoryIds = listOf(category.id)
71+
7072
categorySpi.saveCategory(category)
7173

72-
settingSpi.saveSetting(category, userId, true)
74+
settingSpi.updateAllSetting(categoryIds, userId, true)
7375

7476
detailSpi.save(category)
7577

@@ -102,9 +104,11 @@ class DetailApiImplTest {
102104
topic = "ALL"
103105
)
104106

107+
val categoryIds = listOf(category.id)
108+
105109
categorySpi.saveCategory(category)
106110

107-
settingSpi.saveSetting(category, userId, false)
111+
settingSpi.updateAllSetting(categoryIds, userId, false)
108112

109113
detailSpi.save(category)
110114

@@ -135,9 +139,11 @@ class DetailApiImplTest {
135139
topic = "ALL"
136140
)
137141

142+
val categoryIds = listOf(category.id)
143+
138144
categorySpi.saveCategory(category)
139145

140-
settingSpi.saveSetting(category, userId, false)
146+
settingSpi.updateAllSetting(categoryIds, userId, false)
141147

142148
detailSpi.save(category)
143149

โ€Žnotification-domain/src/test/kotlin/io/github/v1servicenotification/setting/SettingApiImplTest.kt

+5-28
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class SettingApiImplTest {
2626

2727
settingSpi.saveCategory(category)
2828

29-
settingSpi.saveSetting(
30-
category,
29+
val categoryIds = listOf(category.id)
30+
31+
32+
settingSpi.updateAllSetting(
33+
categoryIds,
3134
userId,
3235
true
3336
)
@@ -39,30 +42,4 @@ class SettingApiImplTest {
3942
assertThat(result.destination).isEqualTo(category.destination)
4043

4144
}
42-
43-
@Test
44-
fun deActivatedCategory() {
45-
val userId = UUID.randomUUID()
46-
val categoryId = UUID.randomUUID()
47-
categorySpi.saveCategory(
48-
Category(categoryId, "Test name", "Test destination", false, "ALL")
49-
)
50-
Assertions.assertThat(settingApi.deActivateCategory(categoryId, userId))
51-
.isEqualTo(201)
52-
Assertions.assertThat(settingApi.deActivateCategory(categoryId, userId))
53-
.isEqualTo(204)
54-
}
55-
56-
@Test
57-
fun activateCategory() {
58-
val userId = UUID.randomUUID()
59-
val categoryId = UUID.randomUUID()
60-
categorySpi.saveCategory(
61-
Category(categoryId, "Test name", "Test destination", false, "ALL")
62-
)
63-
assertThat(settingApi.activateCategory(categoryId, userId))
64-
.isEqualTo(201)
65-
assertThat(settingApi.activateCategory(categoryId, userId))
66-
.isEqualTo(204)
67-
}
6845
}

โ€Žnotification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/category/domain/repository/CustomCategoryRepositoryImpl.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.v1servicenotification.domain.category.domain.repository
22

3+
import com.querydsl.jpa.impl.JPAQueryFactory
34
import io.github.v1servicenotification.category.Category
45
import io.github.v1servicenotification.category.spi.QueryCategoryRepositorySpi
56
import io.github.v1servicenotification.category.spi.UpdateCategoryRepositorySpi
7+
import io.github.v1servicenotification.domain.category.domain.QCategoryEntity.categoryEntity
68
import io.github.v1servicenotification.domain.category.exception.CategoryNotFoundException
79
import io.github.v1servicenotification.domain.category.mapper.CategoryMapper
810
import io.github.v1servicenotification.global.extension.findOne
@@ -13,7 +15,8 @@ import java.util.UUID
1315
@Repository
1416
class CustomCategoryRepositoryImpl(
1517
private val categoryMapper: CategoryMapper,
16-
private val categoryRepository: CategoryRepository
18+
private val categoryRepository: CategoryRepository,
19+
private val jpaQueryFactory: JPAQueryFactory,
1720
) : UpdateCategoryRepositorySpi, QueryCategoryRepositorySpi, SettingCategorySpi {
1821

1922
override fun saveCategory(category: Category) {
@@ -40,13 +43,13 @@ class CustomCategoryRepositoryImpl(
4043
return categoryMapper.categoryEntityToDomain(category)
4144
}
4245

43-
override fun findById(id: UUID): Category {
44-
val category = categoryRepository.findById(id)
45-
.orElseThrow {
46-
CategoryNotFoundException.EXCEPTION
47-
}
46+
override fun findByStartingWithTopic(topic: String): List<UUID> {
47+
return jpaQueryFactory
48+
.select(categoryEntity.id)
49+
.from(categoryEntity)
50+
.where(categoryEntity.topic.startsWith(topic))
51+
.fetch()
4852

49-
return categoryMapper.categoryEntityToDomain(category)
5053
}
5154

5255
override fun findAllByDefaultActivated(defaultActivated: Boolean): List<Category> {

โ€Žnotification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/domain/repository/CustomSettingRepositoryImpl.kt

+31-30
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,55 @@ import com.querydsl.jpa.impl.JPAQueryFactory
44
import io.github.v1servicenotification.category.Category
55
import io.github.v1servicenotification.detail.spi.PostDetailSettingRepositorySpi
66
import io.github.v1servicenotification.domain.category.domain.QCategoryEntity.categoryEntity
7+
import io.github.v1servicenotification.domain.category.domain.repository.CategoryRepository
78
import io.github.v1servicenotification.domain.category.mapper.CategoryMapper
89
import io.github.v1servicenotification.domain.setting.domain.QSettingEntity.settingEntity
9-
import io.github.v1servicenotification.domain.setting.domain.SettingEntity
1010
import io.github.v1servicenotification.domain.setting.domain.SettingId
11-
import io.github.v1servicenotification.domain.setting.mapper.SettingMapper
12-
import io.github.v1servicenotification.setting.Setting
1311
import io.github.v1servicenotification.setting.spi.SettingRepositorySpi
1412
import org.springframework.stereotype.Repository
1513
import java.util.UUID
14+
import javax.transaction.Transactional
1615

1716
@Repository
1817
class CustomSettingRepositoryImpl(
1918
private val settingRepository: SettingRepository,
20-
private val settingMapper: SettingMapper,
2119
private val categoryMapper: CategoryMapper,
22-
private val jpaQueryFactory: JPAQueryFactory
20+
private val jpaQueryFactory: JPAQueryFactory,
21+
private val categoryRepository: CategoryRepository,
2322
) : SettingRepositorySpi, PostDetailSettingRepositorySpi {
24-
override fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
25-
return settingMapper.settingEntityToDomain(
26-
settingRepository.save(
27-
SettingEntity(
28-
settingId = getSettingId(category, userId),
29-
isActivated = isActivated
23+
24+
@Transactional
25+
override fun updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean) {
26+
categoryIds.forEach {
27+
jpaQueryFactory
28+
.update(settingEntity)
29+
.set(settingEntity.isActivated, isActivated)
30+
.where(
31+
settingEntity.settingId.categoryEntity.id.eq(it)
32+
.and(settingEntity.settingId.userId.eq(userId))
3033
)
31-
)
32-
)
34+
.execute()
35+
}
3336
}
3437

35-
override fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
36-
return settingMapper.settingEntityToDomain(
37-
settingRepository.save(
38-
SettingEntity(
39-
settingId = getSettingId(category, userId),
40-
isActivated = isActivated
41-
)
38+
override fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean {
39+
return getSettingIdList(categoryIds, userId).map { settingId ->
40+
settingRepository.existsById(settingId)
41+
}.all { it }
42+
}
43+
44+
private fun getSettingIdList(categoryIds: List<UUID>, userId: UUID): List<SettingId> {
45+
return getCategoryById(categoryIds).map { category ->
46+
SettingId(
47+
userId = userId,
48+
categoryEntity = categoryMapper.categoryDomainToEntity(category)
4249
)
43-
)
50+
}
4451
}
4552

46-
override fun settingExist(category: Category, userId: UUID): Boolean {
47-
return settingRepository.existsById(getSettingId(category, userId))
53+
private fun getCategoryById(categoryId: List<UUID>): List<Category> {
54+
return categoryRepository.findAllById(categoryId)
55+
.map { categoryMapper.categoryEntityToDomain(it) }
4856
}
4957

5058
override fun queryActivatedCategory(userId: UUID): List<Category> {
@@ -66,13 +74,6 @@ class CustomSettingRepositoryImpl(
6674
}
6775
}
6876

69-
private fun getSettingId(category: Category, userId: UUID): SettingId {
70-
return SettingId(
71-
userId = userId,
72-
categoryEntity = categoryMapper.categoryDomainToEntity(category)
73-
)
74-
}
75-
7677
override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List<UUID> {
7778
return jpaQueryFactory
7879
.select(settingEntity.settingId.userId)

0 commit comments

Comments
ย (0)