From c0b3d5e6b153637cbd07c784c1b34810e7f3d107 Mon Sep 17 00:00:00 2001 From: lyutvs Date: Sun, 21 May 2023 21:02:39 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20::=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=82=AC=ED=95=AD=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../setting/api/SettingApi.kt | 3 +- .../setting/service/SettingApiImpl.kt | 22 ++++------ .../setting/spi/SettingCategorySpi.kt | 3 +- .../setting/spi/SettingRepositorySpi.kt | 6 +-- .../stubs/InMemoryCategoryRepository.kt | 11 ++--- .../stubs/InMemorySettingRepository.kt | 34 ++++++++------- .../CustomCategoryRepositoryImpl.kt | 18 ++++---- .../repository/CustomSettingRepositoryImpl.kt | 41 +++++++++++-------- .../setting/presentation/SettingController.kt | 37 +++++++---------- 9 files changed, 85 insertions(+), 90 deletions(-) diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/api/SettingApi.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/api/SettingApi.kt index de979c14..3268d3aa 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/api/SettingApi.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/api/SettingApi.kt @@ -4,7 +4,6 @@ import io.github.v1servicenotification.category.api.response.CategoryListRespons import java.util.UUID interface SettingApi { - fun activateCategory(categoryId: UUID, userId: UUID): Int - fun deActivateCategory(categoryId: UUID, userId: UUID): Int + fun activateCategory(isActivate: Boolean, topic: String, userId: UUID): Int fun queryActivatedCategory(userId: UUID): CategoryListResponse } diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/service/SettingApiImpl.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/service/SettingApiImpl.kt index c1bd1f5c..d35ce852 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/service/SettingApiImpl.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/service/SettingApiImpl.kt @@ -12,26 +12,18 @@ import java.util.UUID class SettingApiImpl( private val settingRepositorySpi: SettingRepositorySpi, private val settingCategorySpi: SettingCategorySpi -): SettingApi { +) : SettingApi { - override fun activateCategory(categoryId: UUID, userId: UUID): Int { + override fun activateCategory(isActivate: Boolean, topic: String, userId: UUID): Int { return saveOrUpdateSetting( - categoryId = categoryId, + isActivate = isActivate, + topic = topic, userId = userId, - isActivate = true - ) - } - - override fun deActivateCategory(categoryId: UUID, userId: UUID): Int { - return saveOrUpdateSetting( - categoryId = categoryId, - userId = userId, - isActivate = false - ) + ) } - private fun saveOrUpdateSetting(categoryId: UUID, userId: UUID, isActivate: Boolean): Int { - val category = settingCategorySpi.findById(categoryId) + private fun saveOrUpdateSetting(isActivate: Boolean, topic: String, userId: UUID): Int { + val category = settingCategorySpi.findByStartingWithTopic(topic) return if (settingRepositorySpi.settingExist(category, userId)) { settingRepositorySpi.updateSetting(category, userId, isActivate) diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingCategorySpi.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingCategorySpi.kt index 3340078f..6c7e05ba 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingCategorySpi.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingCategorySpi.kt @@ -1,8 +1,7 @@ package io.github.v1servicenotification.setting.spi import io.github.v1servicenotification.category.Category -import java.util.UUID interface SettingCategorySpi { - fun findById(id: UUID): Category + fun findByStartingWithTopic(topic: String): List } diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingRepositorySpi.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingRepositorySpi.kt index fda7c572..d29827f7 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingRepositorySpi.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/setting/spi/SettingRepositorySpi.kt @@ -7,8 +7,8 @@ import java.util.UUID @Spi interface SettingRepositorySpi { - fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting - fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting - fun settingExist(category: Category, userId: UUID): Boolean + fun saveSetting(categories: List, userId: UUID, isActivated: Boolean): List + fun updateSetting(categories: List, userId: UUID, isActivated: Boolean): List + fun settingExist(categories: List, userId: UUID): Boolean fun queryActivatedCategory(userId: UUID): List } diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemoryCategoryRepository.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemoryCategoryRepository.kt index f03bcbf2..66f6531d 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemoryCategoryRepository.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemoryCategoryRepository.kt @@ -34,14 +34,15 @@ class InMemoryCategoryRepository( ?: throw CategoryNotFoundException.EXCEPTION } - override fun findById(id: UUID): Category { - return categoryMap[id] - ?: throw CategoryNotFoundException.EXCEPTION - } - override fun findAllByDefaultActivated(defaultActivated: Boolean): List { return categoryMap.filter { it.value.defaultActivated == defaultActivated }.map { it.value } } + + override fun findByStartingWithTopic(topic: String): List { + return categoryMap.values.filter { + it.topic.startsWith(topic) + } + } } diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemorySettingRepository.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemorySettingRepository.kt index 12384cc1..c11737e9 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemorySettingRepository.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/stubs/InMemorySettingRepository.kt @@ -21,25 +21,29 @@ class InMemorySettingRepository( .map { it.value }.firstOrNull() } - override fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting { - val setting = Setting(userId, category.id, isActivated) - settingMap[UUID.randomUUID()] = setting - - return setting + override fun saveSetting(categories: List, userId: UUID, isActivated: Boolean): List { + return categories.map { + val setting = Setting( + userId = userId, + notificationCategoryId = it.id, + isActivated = isActivated + ) + settingMap[setting.notificationCategoryId] = setting + setting + } } - override fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting { - return settingMap.filter { - it.value.notificationCategoryId == category.id && it.value.userId == userId - }.map { - it.value.changeIsActivate(isActivated) - it.value - }[0] + override fun updateSetting(categories: List, userId: UUID, isActivated: Boolean): List { + return categories.map { + val setting = findSetting(userId, it.id) + setting?.changeIsActivate(isActivated) + setting + }.filterNotNull() } - override fun settingExist(category: Category, userId: UUID): Boolean { - return settingMap.filter { - it.value.notificationCategoryId == category.id && it.value.userId == userId + override fun settingExist(categories: List, userId: UUID): Boolean { + return categories.mapNotNull { + findSetting(userId, it.id) }.isNotEmpty() } diff --git a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/category/domain/repository/CustomCategoryRepositoryImpl.kt b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/category/domain/repository/CustomCategoryRepositoryImpl.kt index 3fb65fdb..5cf6187e 100644 --- a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/category/domain/repository/CustomCategoryRepositoryImpl.kt +++ b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/category/domain/repository/CustomCategoryRepositoryImpl.kt @@ -1,8 +1,10 @@ package io.github.v1servicenotification.domain.category.domain.repository +import com.querydsl.jpa.impl.JPAQueryFactory import io.github.v1servicenotification.category.Category import io.github.v1servicenotification.category.spi.QueryCategoryRepositorySpi import io.github.v1servicenotification.category.spi.UpdateCategoryRepositorySpi +import io.github.v1servicenotification.domain.category.domain.QCategoryEntity.categoryEntity import io.github.v1servicenotification.domain.category.exception.CategoryNotFoundException import io.github.v1servicenotification.domain.category.mapper.CategoryMapper import io.github.v1servicenotification.global.extension.findOne @@ -13,7 +15,8 @@ import java.util.UUID @Repository class CustomCategoryRepositoryImpl( private val categoryMapper: CategoryMapper, - private val categoryRepository: CategoryRepository + private val categoryRepository: CategoryRepository, + private val jpaQueryFactory: JPAQueryFactory, ) : UpdateCategoryRepositorySpi, QueryCategoryRepositorySpi, SettingCategorySpi { override fun saveCategory(category: Category) { @@ -40,13 +43,14 @@ class CustomCategoryRepositoryImpl( return categoryMapper.categoryEntityToDomain(category) } - override fun findById(id: UUID): Category { - val category = categoryRepository.findById(id) - .orElseThrow { - CategoryNotFoundException.EXCEPTION + override fun findByStartingWithTopic(topic: String): List { + return jpaQueryFactory + .selectFrom(categoryEntity) + .where(categoryEntity.topic.startsWith(topic)) + .fetch() + .map { + categoryMapper.categoryEntityToDomain(it) } - - return categoryMapper.categoryEntityToDomain(category) } override fun findAllByDefaultActivated(defaultActivated: Boolean): List { diff --git a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/domain/repository/CustomSettingRepositoryImpl.kt b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/domain/repository/CustomSettingRepositoryImpl.kt index 1a6e62e4..846ebf63 100644 --- a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/domain/repository/CustomSettingRepositoryImpl.kt +++ b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/domain/repository/CustomSettingRepositoryImpl.kt @@ -21,30 +21,33 @@ class CustomSettingRepositoryImpl( private val categoryMapper: CategoryMapper, private val jpaQueryFactory: JPAQueryFactory ) : SettingRepositorySpi, PostDetailSettingRepositorySpi { - override fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting { - return settingMapper.settingEntityToDomain( - settingRepository.save( - SettingEntity( - settingId = getSettingId(category, userId), - isActivated = isActivated - ) - ) - ) + override fun saveSetting(categories: List, userId: UUID, isActivated: Boolean): List { + return getSettingIdList(categories, userId).map { + saveSettingId(it, isActivated) + } + } + + override fun updateSetting(categories: List, userId: UUID, isActivated: Boolean): List { + return getSettingIdList(categories, userId).map { + saveSettingId(it, isActivated) + } } - override fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting { + private fun saveSettingId(settingId: SettingId, isActivated: Boolean): Setting { return settingMapper.settingEntityToDomain( settingRepository.save( SettingEntity( - settingId = getSettingId(category, userId), + settingId = settingId, isActivated = isActivated ) ) ) } - override fun settingExist(category: Category, userId: UUID): Boolean { - return settingRepository.existsById(getSettingId(category, userId)) + override fun settingExist(categories: List, userId: UUID): Boolean { + return getSettingIdList(categories, userId).map { settingId -> + settingRepository.existsById(settingId) + }.all { it } } override fun queryActivatedCategory(userId: UUID): List { @@ -66,11 +69,13 @@ class CustomSettingRepositoryImpl( } } - private fun getSettingId(category: Category, userId: UUID): SettingId { - return SettingId( - userId = userId, - categoryEntity = categoryMapper.categoryDomainToEntity(category) - ) + private fun getSettingIdList(categories: List, userId: UUID): List { + return categories.map { category -> + SettingId( + userId = userId, + categoryEntity = categoryMapper.categoryDomainToEntity(category) + ) + } } override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List { diff --git a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/presentation/SettingController.kt b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/presentation/SettingController.kt index fa0d773c..efe438c1 100644 --- a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/presentation/SettingController.kt +++ b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/domain/setting/presentation/SettingController.kt @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.RequestParam import java.util.UUID @Tag(name = "알림 카테고리 활성화 설정", description = "알림 카테고리 활성화 관련 Api 입니다.") @@ -33,31 +34,21 @@ class SettingController( } @Operation(summary = "알림 카테고리 활성화") - @ApiResponses(value = [ - ApiResponse(responseCode = CustomHttpStatus.CREATED), - ApiResponse(responseCode = CustomHttpStatus.NO_CONTENT) - ]) - @PatchMapping("/{category-uuid}") - fun activateNotificationCategory(@PathVariable("category-uuid") categoryId: UUID): ResponseEntity { + @ApiResponses( + value = [ + ApiResponse(responseCode = CustomHttpStatus.CREATED), + ApiResponse(responseCode = CustomHttpStatus.NO_CONTENT) + ] + ) + @PatchMapping + fun activateNotificationCategory( + @RequestParam("is-activate") isActivate: Boolean, + @RequestParam("topic") topic: String + ): ResponseEntity { return ResponseEntity( HttpStatus.valueOf( - settingApi.activateCategory(categoryId, getUserId()) + settingApi.activateCategory(isActivate, topic, getUserId()) ) ) } - - @Operation(summary = "알림 카테고리 비활성회") - @ApiResponses(value = [ - ApiResponse(responseCode = CustomHttpStatus.CREATED), - ApiResponse(responseCode = CustomHttpStatus.NO_CONTENT) - ]) - @DeleteMapping("/{category-uuid}") - fun deActivateNotificationCategory(@PathVariable("category-uuid") categoryId: UUID): ResponseEntity { - return ResponseEntity( - HttpStatus.valueOf( - settingApi.deActivateCategory(categoryId, getUserId()) - ) - ) - } - -} \ No newline at end of file +}