Skip to content

Commit

Permalink
⚡️ :: 변경사항 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
lyutvs committed May 21, 2023
1 parent 4b16de5 commit c0b3d5e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 90 deletions.
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 activateCategory(isActivate: Boolean, topic: String, userId: UUID): Int
fun queryActivatedCategory(userId: UUID): CategoryListResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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

interface SettingCategorySpi {
fun findById(id: UUID): Category
fun findByStartingWithTopic(topic: String): List<Category>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category>, userId: UUID, isActivated: Boolean): List<Setting>
fun updateSetting(categories: List<Category>, userId: UUID, isActivated: Boolean): List<Setting>
fun settingExist(categories: List<Category>, userId: UUID): Boolean
fun queryActivatedCategory(userId: UUID): List<Category>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category> {
return categoryMap.filter {
it.value.defaultActivated == defaultActivated
}.map { it.value }
}

override fun findByStartingWithTopic(topic: String): List<Category> {
return categoryMap.values.filter {
it.topic.startsWith(topic)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category>, userId: UUID, isActivated: Boolean): List<Setting> {
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<Category>, userId: UUID, isActivated: Boolean): List<Setting> {
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<Category>, userId: UUID): Boolean {
return categories.mapNotNull {
findSetting(userId, it.id)
}.isNotEmpty()
}

Expand Down
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,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<Category> {
return jpaQueryFactory
.selectFrom(categoryEntity)
.where(categoryEntity.topic.startsWith(topic))
.fetch()
.map {
categoryMapper.categoryEntityToDomain(it)
}

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 @@ -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<Category>, userId: UUID, isActivated: Boolean): List<Setting> {
return getSettingIdList(categories, userId).map {
saveSettingId(it, isActivated)
}
}

override fun updateSetting(categories: List<Category>, userId: UUID, isActivated: Boolean): List<Setting> {
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<Category>, userId: UUID): Boolean {
return getSettingIdList(categories, userId).map { settingId ->
settingRepository.existsById(settingId)
}.all { it }
}

override fun queryActivatedCategory(userId: UUID): List<Category> {
Expand All @@ -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<Category>, userId: UUID): List<SettingId> {
return categories.map { category ->
SettingId(
userId = userId,
categoryEntity = categoryMapper.categoryDomainToEntity(category)
)
}
}

override fun findAllUserIdByTopicAndIsActivated(topic: String, isActivated: Boolean): List<UUID> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 입니다.")
Expand All @@ -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<Unit> {
@ApiResponses(
value = [
ApiResponse(responseCode = CustomHttpStatus.CREATED),
ApiResponse(responseCode = CustomHttpStatus.NO_CONTENT)
]
)
@PatchMapping
fun activateNotificationCategory(
@RequestParam("is-activate") isActivate: Boolean,
@RequestParam("topic") topic: String
): ResponseEntity<Unit> {
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<Unit> {
return ResponseEntity(
HttpStatus.valueOf(
settingApi.deActivateCategory(categoryId, getUserId())
)
)
}

}
}

0 comments on commit c0b3d5e

Please sign in to comment.