Skip to content

Commit

Permalink
๐Ÿ”€ :: (#71) ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™” ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: (#71) ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™” ๋ณ€๊ฒฝ
  • Loading branch information
lyutvs authored May 24, 2023
2 parents 4b16de5 + e00ac46 commit 5744326
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ enum class ErrorCode(

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

DEVICE_TOKEN_LENGTH(401, "DEVICE-400-1", "Device Token Length Error.")
DEVICE_TOKEN_LENGTH(401, "DEVICE-400-1", "Device Token Length Error."),

SETTING_NOT_FOUND(404, "SETTING-404-1", "Setting Not Found.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 activateOrDeActivateCategory(isActivate: Boolean, topic: String, userId: UUID)
fun queryActivatedCategory(userId: UUID): CategoryListResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.v1servicenotification.setting.exception

import io.github.v1servicenotification.error.ErrorCode
import io.github.v1servicenotification.error.NotificationException

class SettingNotFoundException private constructor(): NotificationException(ErrorCode.CATEGORY_NOT_FOUND) {
companion object {
@JvmField
val EXCEPTION = SettingNotFoundException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,23 @@ import io.github.v1servicenotification.category.api.response.CategoryElement
import io.github.v1servicenotification.category.api.response.CategoryListResponse
import io.github.v1servicenotification.setting.spi.SettingRepositorySpi
import io.github.v1servicenotification.setting.api.SettingApi
import io.github.v1servicenotification.setting.exception.SettingNotFoundException
import io.github.v1servicenotification.setting.spi.SettingCategorySpi
import java.util.UUID

@DomainService
class SettingApiImpl(
private val settingRepositorySpi: SettingRepositorySpi,
private val settingCategorySpi: SettingCategorySpi
): SettingApi {
) : SettingApi {

override fun activateCategory(categoryId: UUID, userId: UUID): Int {
return saveOrUpdateSetting(
categoryId = categoryId,
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)
override fun activateOrDeActivateCategory(isActivate: Boolean, topic: String, userId: UUID) {
val category = settingCategorySpi.findByStartingWithTopic(topic)

return if (settingRepositorySpi.settingExist(category, userId)) {
settingRepositorySpi.updateSetting(category, userId, isActivate)
204
} else {
settingRepositorySpi.saveSetting(category, userId, isActivate)
201
if (!settingRepositorySpi.settingExist(category, userId)) {
throw SettingNotFoundException.EXCEPTION
}
settingRepositorySpi.updateAllSetting(category, userId, isActivate)
}

override fun queryActivatedCategory(userId: UUID): CategoryListResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.github.v1servicenotification.setting.spi

import io.github.v1servicenotification.category.Category
import java.util.UUID
import java.util.*

interface SettingCategorySpi {
fun findById(id: UUID): Category
fun findByStartingWithTopic(topic: String): List<UUID>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package io.github.v1servicenotification.setting.spi

import io.github.v1servicenotification.annotation.Spi
import io.github.v1servicenotification.category.Category
import io.github.v1servicenotification.setting.Setting
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 updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean)
fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean
fun queryActivatedCategory(userId: UUID): List<Category>
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ class InMemoryCategoryRepository(
?: throw CategoryNotFoundException.EXCEPTION
}

override fun findById(id: UUID): Category {
return categoryMap[id]
?: throw CategoryNotFoundException.EXCEPTION
}

override fun findAllByDefaultActivated(defaultActivated: Boolean): List<Category> {
return categoryMap.filter {
it.value.defaultActivated == defaultActivated
}.map { it.value }
}

override fun findByStartingWithTopic(topic: String): List<UUID> {
return categoryMap.filter {
it.value.topic.startsWith(topic)
}.map {
it.key
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,16 @@ class InMemorySettingRepository(
categoryMap[category.id] = category
}

fun findSetting(userId: UUID, categoryId: UUID): Setting? {
return settingMap
.filter { it.value.userId == userId && it.value.notificationCategoryId == categoryId }
.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 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 updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean) {
categoryIds.forEach {
val setting = findSetting(userId, it)
setting?.changeIsActivate(isActivated)
}
}

override fun settingExist(category: Category, userId: UUID): Boolean {
return settingMap.filter {
it.value.notificationCategoryId == category.id && it.value.userId == userId
}.isNotEmpty()
override fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean {
return categoryIds.map { findSetting(userId, it) }.any { it != null }
}

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

private fun findSetting(userId: UUID, categoryId: UUID): Setting? {
return settingMap
.filter { it.value.userId == userId && it.value.notificationCategoryId == categoryId }
.map { it.value }.firstOrNull()
}

override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List<UUID> {
return settingMap.values.filter {
it.isActivated == isActivated && categoryMap[it.notificationCategoryId]?.topic == topic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ class DetailApiImplTest {
topic = "ALL"
)

val categoryIds = listOf(category.id)

categorySpi.saveCategory(category)

settingSpi.saveSetting(category, userId, true)
settingSpi.updateAllSetting(categoryIds, userId, true)

detailSpi.save(category)

Expand Down Expand Up @@ -102,9 +104,11 @@ class DetailApiImplTest {
topic = "ALL"
)

val categoryIds = listOf(category.id)

categorySpi.saveCategory(category)

settingSpi.saveSetting(category, userId, false)
settingSpi.updateAllSetting(categoryIds, userId, false)

detailSpi.save(category)

Expand Down Expand Up @@ -135,9 +139,11 @@ class DetailApiImplTest {
topic = "ALL"
)

val categoryIds = listOf(category.id)

categorySpi.saveCategory(category)

settingSpi.saveSetting(category, userId, false)
settingSpi.updateAllSetting(categoryIds, userId, false)

detailSpi.save(category)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class SettingApiImplTest {

settingSpi.saveCategory(category)

settingSpi.saveSetting(
category,
val categoryIds = listOf(category.id)


settingSpi.updateAllSetting(
categoryIds,
userId,
true
)
Expand All @@ -39,30 +42,4 @@ class SettingApiImplTest {
assertThat(result.destination).isEqualTo(category.destination)

}

@Test
fun deActivatedCategory() {
val userId = UUID.randomUUID()
val categoryId = UUID.randomUUID()
categorySpi.saveCategory(
Category(categoryId, "Test name", "Test destination", false, "ALL")
)
Assertions.assertThat(settingApi.deActivateCategory(categoryId, userId))
.isEqualTo(201)
Assertions.assertThat(settingApi.deActivateCategory(categoryId, userId))
.isEqualTo(204)
}

@Test
fun activateCategory() {
val userId = UUID.randomUUID()
val categoryId = UUID.randomUUID()
categorySpi.saveCategory(
Category(categoryId, "Test name", "Test destination", false, "ALL")
)
assertThat(settingApi.activateCategory(categoryId, userId))
.isEqualTo(201)
assertThat(settingApi.activateCategory(categoryId, userId))
.isEqualTo(204)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -40,13 +43,13 @@ 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<UUID> {
return jpaQueryFactory
.select(categoryEntity.id)
.from(categoryEntity)
.where(categoryEntity.topic.startsWith(topic))
.fetch()

return categoryMapper.categoryEntityToDomain(category)
}

override fun findAllByDefaultActivated(defaultActivated: Boolean): List<Category> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,55 @@ import com.querydsl.jpa.impl.JPAQueryFactory
import io.github.v1servicenotification.category.Category
import io.github.v1servicenotification.detail.spi.PostDetailSettingRepositorySpi
import io.github.v1servicenotification.domain.category.domain.QCategoryEntity.categoryEntity
import io.github.v1servicenotification.domain.category.domain.repository.CategoryRepository
import io.github.v1servicenotification.domain.category.mapper.CategoryMapper
import io.github.v1servicenotification.domain.setting.domain.QSettingEntity.settingEntity
import io.github.v1servicenotification.domain.setting.domain.SettingEntity
import io.github.v1servicenotification.domain.setting.domain.SettingId
import io.github.v1servicenotification.domain.setting.mapper.SettingMapper
import io.github.v1servicenotification.setting.Setting
import io.github.v1servicenotification.setting.spi.SettingRepositorySpi
import org.springframework.stereotype.Repository
import java.util.UUID
import javax.transaction.Transactional

@Repository
class CustomSettingRepositoryImpl(
private val settingRepository: SettingRepository,
private val settingMapper: SettingMapper,
private val categoryMapper: CategoryMapper,
private val jpaQueryFactory: JPAQueryFactory
private val jpaQueryFactory: JPAQueryFactory,
private val categoryRepository: CategoryRepository,
) : SettingRepositorySpi, PostDetailSettingRepositorySpi {
override fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
return settingMapper.settingEntityToDomain(
settingRepository.save(
SettingEntity(
settingId = getSettingId(category, userId),
isActivated = isActivated

@Transactional
override fun updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean) {
categoryIds.forEach {
jpaQueryFactory
.update(settingEntity)
.set(settingEntity.isActivated, isActivated)
.where(
settingEntity.settingId.categoryEntity.id.eq(it)
.and(settingEntity.settingId.userId.eq(userId))
)
)
)
.execute()
}
}

override fun updateSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
return settingMapper.settingEntityToDomain(
settingRepository.save(
SettingEntity(
settingId = getSettingId(category, userId),
isActivated = isActivated
)
override fun settingExist(categoryIds: List<UUID>, userId: UUID): Boolean {
return getSettingIdList(categoryIds, userId).map { settingId ->
settingRepository.existsById(settingId)
}.all { it }
}

private fun getSettingIdList(categoryIds: List<UUID>, userId: UUID): List<SettingId> {
return getCategoryById(categoryIds).map { category ->
SettingId(
userId = userId,
categoryEntity = categoryMapper.categoryDomainToEntity(category)
)
)
}
}

override fun settingExist(category: Category, userId: UUID): Boolean {
return settingRepository.existsById(getSettingId(category, userId))
private fun getCategoryById(categoryId: List<UUID>): List<Category> {
return categoryRepository.findAllById(categoryId)
.map { categoryMapper.categoryEntityToDomain(it) }
}

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

private fun getSettingId(category: Category, userId: UUID): SettingId {
return SettingId(
userId = userId,
categoryEntity = categoryMapper.categoryDomainToEntity(category)
)
}

override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List<UUID> {
return jpaQueryFactory
.select(settingEntity.settingId.userId)
Expand Down
Loading

0 comments on commit 5744326

Please sign in to comment.