Skip to content

Commit

Permalink
๐Ÿ”€ :: (#103) ํ™œ์„ฑํ™”๋œ ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก api ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: (#103) ํ™œ์„ฑํ™”๋œ ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก api ๋ณ€๊ฒฝ
  • Loading branch information
lyutvs authored May 25, 2023
2 parents 5744326 + a3db1bb commit 90afde2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.v1servicenotification.setting.api

import io.github.v1servicenotification.category.api.response.CategoryListResponse
import io.github.v1servicenotification.setting.api.response.SettingListResponse
import java.util.UUID

interface SettingApi {
fun queryUserCategoryStatus(userId: UUID): SettingListResponse
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,6 @@
package io.github.v1servicenotification.setting.api.response

data class SettingElement(
val topic: String,
val isActivate: Boolean,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.v1servicenotification.setting.api.response

data class SettingListResponse(
val settings: List<SettingElement>
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.v1servicenotification.setting.service

import io.github.v1servicenotification.annotation.DomainService
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.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
Expand All @@ -24,18 +24,20 @@ class SettingApiImpl(
settingRepositorySpi.updateAllSetting(category, userId, isActivate)
}

override fun queryActivatedCategory(userId: UUID): CategoryListResponse {
return CategoryListResponse(
settingRepositorySpi.queryActivatedCategory(userId)
.map {
CategoryElement(
id = it.id,
title = it.title,
destination = it.destination,
topic = it.topic
)
}
.toList()
)
override fun queryUserCategoryStatus(userId: UUID): SettingListResponse {
val settingList = settingRepositorySpi.queryUserIdSetting(userId)
val categoryList = settingRepositorySpi.queryUserCategory(userId)

val categoryMap = categoryList.associateBy { it.id }

val activatedCategories = settingList.mapNotNull { setting ->
val category = categoryMap[setting.notificationCategoryId]
category?.let {
val topicSubStringByUnderscore = it.topic.substringBefore("_")
SettingElement(topicSubStringByUnderscore, setting.isActivated)
}
}.distinctBy { it.topic }

return SettingListResponse(activatedCategories)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ 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 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
@@ -1,6 +1,7 @@
package io.github.v1servicenotification.stubs

import io.github.v1servicenotification.category.Category
import io.github.v1servicenotification.category.exception.CategoryNotFoundException
import io.github.v1servicenotification.detail.spi.PostDetailSettingRepositorySpi
import io.github.v1servicenotification.setting.Setting
import io.github.v1servicenotification.setting.spi.SettingRepositorySpi
Expand All @@ -11,6 +12,14 @@ class InMemorySettingRepository(
private val settingMap: HashMap<UUID, Setting> = hashMapOf()
) : SettingRepositorySpi, PostDetailSettingRepositorySpi {


fun saveSetting(category: Category, userId: UUID, isActivated: Boolean): Setting {
val setting = Setting(userId, category.id, isActivated)
settingMap[UUID.randomUUID()] = setting

return setting
}

fun saveCategory(category: Category) {
categoryMap[category.id] = category
}
Expand All @@ -27,11 +36,8 @@ class InMemorySettingRepository(
return categoryIds.map { findSetting(userId, it) }.any { it != null }
}

override fun queryActivatedCategory(userId: UUID): List<Category> {
return categoryMap.filter {
val setting = findSetting(userId, it.value.id)
setting?.isActivated ?: it.value.defaultActivated
}.map { it.value }
override fun queryUserIdSetting(userId: UUID): List<Setting> {
return settingMap.values.filter { it.userId == userId }
}

private fun findSetting(userId: UUID, categoryId: UUID): Setting? {
Expand All @@ -45,4 +51,10 @@ class InMemorySettingRepository(
it.isActivated == isActivated && categoryMap[it.notificationCategoryId]?.topic == topic
}.map { it.userId }
}

override fun queryUserCategory(userId: UUID): List<Category> {
return settingMap.filter { it.value.userId == userId }.map {
categoryMap[it.value.notificationCategoryId] ?: throw CategoryNotFoundException.EXCEPTION
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,24 @@ class SettingApiImplTest {
private val settingApi = SettingApiImpl(settingSpi, categorySpi)

@Test
fun queryActivatedCategory() {
fun queryUserCategoryStatus() {
val userId = UUID.randomUUID()
val categoryId = UUID.randomUUID()

val category = Category(categoryId, "Test name", "Test destination", false, "ALL")
val setting = Setting(categoryId, userId, true)

settingSpi.saveCategory(category)

val categoryIds = listOf(category.id)


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

val result = settingApi.queryActivatedCategory(userId).categories[0]

assertThat(result.id).isEqualTo(categoryId)
assertThat(result.title).isEqualTo(category.title)
assertThat(result.destination).isEqualTo(category.destination)
val result = settingApi.queryUserCategoryStatus(userId).settings.first()

assertThat(result.topic).isEqualTo(category.topic)
assertThat(result.isActivate).isEqualTo(setting.isActivated)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package io.github.v1servicenotification.domain.setting.domain.repository
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.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.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
Expand All @@ -19,6 +22,7 @@ class CustomSettingRepositoryImpl(
private val categoryMapper: CategoryMapper,
private val jpaQueryFactory: JPAQueryFactory,
private val categoryRepository: CategoryRepository,
private val settingMapper: SettingMapper
) : SettingRepositorySpi, PostDetailSettingRepositorySpi {

@Transactional
Expand Down Expand Up @@ -55,19 +59,22 @@ class CustomSettingRepositoryImpl(
.map { categoryMapper.categoryEntityToDomain(it) }
}

override fun queryActivatedCategory(userId: UUID): List<Category> {

override fun queryUserIdSetting(userId: UUID): List<Setting> {
return jpaQueryFactory
.select(categoryEntity)
.from(categoryEntity)
.selectFrom(settingEntity)
.where(settingEntity.settingId.userId.eq(userId))
.fetch()
.map {
settingMapper.settingEntityToDomain(it)
}
}

override fun queryUserCategory(userId: UUID): List<Category> {
return jpaQueryFactory
.selectFrom(categoryEntity)
.leftJoin(categoryEntity.settingList, settingEntity)
.on(settingEntity.settingId.userId.eq(userId))
.where(
settingEntity.isActivated.isTrue
.or(
categoryEntity.defaultActivated.isTrue
.and(settingEntity.isActivated.isNull)
)
)
.where(settingEntity.settingId.userId.eq(userId))
.fetch()
.map {
categoryMapper.categoryEntityToDomain(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.v1servicenotification.domain.setting.presentation

import io.github.v1servicenotification.category.api.response.CategoryListResponse
import io.github.v1servicenotification.global.extension.getUserId
import io.github.v1servicenotification.setting.api.SettingApi
import io.github.v1servicenotification.setting.api.response.SettingListResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag

Expand All @@ -21,10 +21,10 @@ class SettingController(
private val settingApi: SettingApi
) {

@Operation(summary = "ํ™œ์„ฑํ™”๋œ ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก")
@Operation(summary = "์œ ์ € ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ์ƒํƒœ ๋ชฉ๋ก")
@GetMapping
fun queryActivatedCategory(): CategoryListResponse {
return settingApi.queryActivatedCategory(getUserId())
fun queryUserCategoryStatus(): SettingListResponse {
return settingApi.queryUserCategoryStatus(getUserId())
}

@Operation(summary = "์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™”")
Expand Down

0 comments on commit 90afde2

Please sign in to comment.