Skip to content

Commit

Permalink
Merge branch 'main' into COR-103-topic-category-list-refector
Browse files Browse the repository at this point in the history
  • Loading branch information
lyutvs authored May 24, 2023
2 parents fc0a4ce + 5744326 commit 31baf59
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 160 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 @@ -7,4 +7,5 @@ interface SettingApi {
fun activateCategory(categoryId: UUID, userId: UUID): Int
fun deActivateCategory(categoryId: UUID, userId: UUID): Int
fun queryUserCategoryStatus(userId: UUID): SettingListResponse
fun activateOrDeActivateCategory(isActivate: Boolean, topic: String, userId: UUID)
}
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.setting.spi.SettingRepositorySpi
import io.github.v1servicenotification.setting.api.SettingApi
import io.github.v1servicenotification.setting.api.response.SettingElement
import io.github.v1servicenotification.setting.api.response.SettingListResponse
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 {

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
)
}
) : SettingApi {

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 queryUserCategoryStatus(userId: UUID): SettingListResponse {
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,7 +2,6 @@ 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
Expand All @@ -12,4 +11,7 @@ interface SettingRepositorySpi {
fun settingExist(category: Category, userId: UUID): Boolean
fun queryUserIdSetting(userId: UUID): List<Setting>
fun queryUserCategory(userId: UUID): List<Category>
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 @@ -16,34 +16,24 @@ 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 updateAllSetting(categoryIds: List<UUID>, userId: UUID, isActivated: Boolean) {
categoryIds.forEach {
val setting = findSetting(userId, it)
setting?.changeIsActivate(isActivated)
}
}

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 settingExist(categoryIds: List<UUID>, userId: UUID): Boolean {
return categoryIds.map { findSetting(userId, it) }.any { it != null }
}

override fun settingExist(category: Category, userId: UUID): Boolean {
return settingMap.filter {
it.value.notificationCategoryId == category.id && it.value.userId == userId
}.isNotEmpty()
}

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 {
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 @@ -27,8 +27,11 @@ class SettingApiImplTest {

settingSpi.saveCategory(category)

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


settingSpi.updateAllSetting(
categoryIds,
userId,
true
)
Expand All @@ -38,30 +41,4 @@ class SettingApiImplTest {
assertThat(result.topic).isEqualTo(category.topic)
assertThat(result.isActivate).isEqualTo(setting.isActivated)
}

@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 @@ -5,47 +5,55 @@ import io.github.v1servicenotification.category.Category
import io.github.v1servicenotification.detail.spi.PostDetailSettingRepositorySpi
import io.github.v1servicenotification.domain.category.domain.CategoryEntity
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) }
}


Expand Down Expand Up @@ -79,13 +87,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 31baf59

Please sign in to comment.