라이브 방 메뉴 처리 및 저장
- Redis -> Valkey로 이전되도록 수정
This commit is contained in:
		| @@ -2,17 +2,48 @@ package kr.co.vividnext.sodalive.live.room.menu | ||||
|  | ||||
| import kr.co.vividnext.sodalive.common.SodaException | ||||
| import kr.co.vividnext.sodalive.live.roulette.RedisIdGenerator | ||||
| import kr.co.vividnext.sodalive.v2.LiveRoomMenuRepositoryV2 | ||||
| import org.springframework.stereotype.Service | ||||
|  | ||||
| @Service | ||||
| class LiveRoomMenuService( | ||||
|     private val idGenerator: RedisIdGenerator, | ||||
|     private val repository: LiveRoomMenuRepository | ||||
|     private val repository: LiveRoomMenuRepository, | ||||
|     private val repositoryV2: LiveRoomMenuRepositoryV2 | ||||
| ) { | ||||
|     private fun findByCreatorId(creatorId: Long): List<LiveRoomMenu> { | ||||
|         var liveRoomMenuList = repositoryV2.findByCreatorId(creatorId) | ||||
|  | ||||
|         if (liveRoomMenuList.isEmpty()) { | ||||
|             liveRoomMenuList = repository.findByCreatorId(creatorId) | ||||
|  | ||||
|             if (liveRoomMenuList.isNotEmpty()) { | ||||
|                 val newLiveRoomMenuList = liveRoomMenuList.map { liveRoomMenu -> | ||||
|                     val newLiveRoomMenu = LiveRoomMenu( | ||||
|                         id = idGenerator.generateId(SEQUENCE_NAME), | ||||
|                         creatorId = liveRoomMenu.creatorId, | ||||
|                         isActive = liveRoomMenu.isActive, | ||||
|                         menu = liveRoomMenu.menu | ||||
|                     ) | ||||
|                     repositoryV2.save(newLiveRoomMenu) | ||||
|                     repository.delete(liveRoomMenu) | ||||
|  | ||||
|                     newLiveRoomMenu | ||||
|                 } | ||||
|  | ||||
|                 return newLiveRoomMenuList | ||||
|             } else { | ||||
|                 return emptyList() | ||||
|             } | ||||
|         } else { | ||||
|             return liveRoomMenuList | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun getAllLiveMenu(creatorId: Long, memberId: Long): List<GetMenuPresetResponse> { | ||||
|         if (creatorId != memberId) throw SodaException("잘못된 요청입니다.") | ||||
|  | ||||
|         return repository.findByCreatorId(creatorId) | ||||
|         return findByCreatorId(creatorId) | ||||
|             .sortedBy { it.id } | ||||
|             .asSequence() | ||||
|             .map { GetMenuPresetResponse(id = it.id, menu = it.menu, isActive = it.isActive) } | ||||
| @@ -22,7 +53,7 @@ class LiveRoomMenuService( | ||||
|     fun createLiveMenu(memberId: Long, request: CreateLiveMenuRequest): Boolean { | ||||
|         liveMenuValidate(menu = request.menu) | ||||
|  | ||||
|         val menuList = repository.findByCreatorId(creatorId = memberId) | ||||
|         val menuList = findByCreatorId(creatorId = memberId) | ||||
|  | ||||
|         if (menuList.size >= 3) { | ||||
|             throw SodaException("메뉴판의 최대개수는 3개입니다.") | ||||
| @@ -31,7 +62,7 @@ class LiveRoomMenuService( | ||||
|         if (request.isActive) { | ||||
|             menuList.forEach { | ||||
|                 it.isActive = false | ||||
|                 repository.save(it) | ||||
|                 repositoryV2.save(it) | ||||
|             } | ||||
|         } | ||||
|  | ||||
| @@ -42,14 +73,14 @@ class LiveRoomMenuService( | ||||
|             menu = request.menu | ||||
|         ) | ||||
|  | ||||
|         repository.save(menu) | ||||
|         repositoryV2.save(menu) | ||||
|         return request.isActive | ||||
|     } | ||||
|  | ||||
|     fun updateLiveMenu(memberId: Long, request: UpdateLiveMenuRequest) { | ||||
|         liveMenuValidate(menu = request.menu) | ||||
|  | ||||
|         val menuList = repository.findByCreatorId(creatorId = memberId) | ||||
|         val menuList = findByCreatorId(creatorId = memberId) | ||||
|         if (menuList.isEmpty()) { | ||||
|             throw SodaException("잘못된 요청입니다.") | ||||
|         } | ||||
| @@ -58,16 +89,16 @@ class LiveRoomMenuService( | ||||
|             if (it.id == request.id) { | ||||
|                 it.menu = request.menu | ||||
|                 it.isActive = request.isActive | ||||
|                 repository.save(it) | ||||
|                 repositoryV2.save(it) | ||||
|             } else if (request.isActive) { | ||||
|                 it.isActive = false | ||||
|                 repository.save(it) | ||||
|                 repositoryV2.save(it) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun getLiveMenu(creatorId: Long): GetMenuPresetResponse? { | ||||
|         val menuList = repository.findByCreatorId(creatorId = creatorId) | ||||
|         val menuList = findByCreatorId(creatorId = creatorId) | ||||
|  | ||||
|         var activeMenu: LiveRoomMenu? = null | ||||
|         for (menu in menuList) { | ||||
| @@ -85,11 +116,11 @@ class LiveRoomMenuService( | ||||
|     } | ||||
|  | ||||
|     fun deactivateAll(memberId: Long) { | ||||
|         val menuList = repository.findByCreatorId(creatorId = memberId) | ||||
|         val menuList = findByCreatorId(creatorId = memberId) | ||||
|  | ||||
|         menuList.forEach { | ||||
|             it.isActive = false | ||||
|             repository.save(it) | ||||
|             repositoryV2.save(it) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -6,7 +6,7 @@ import org.springframework.stereotype.Service | ||||
|  | ||||
| @Service | ||||
| class RedisIdGenerator( | ||||
|     @Qualifier("redisStringRedisTemplate") private val stringRedisTemplate: StringRedisTemplate | ||||
|     @Qualifier("valkeyStringRedisTemplate") private val stringRedisTemplate: StringRedisTemplate | ||||
| ) { | ||||
|     fun generateId(key: String): Long { | ||||
|         return stringRedisTemplate.opsForValue().increment(key, 1) ?: 1L | ||||
|   | ||||
| @@ -0,0 +1,8 @@ | ||||
| package kr.co.vividnext.sodalive.v2 | ||||
|  | ||||
| import kr.co.vividnext.sodalive.live.room.menu.LiveRoomMenu | ||||
| import org.springframework.data.repository.CrudRepository | ||||
|  | ||||
| interface LiveRoomMenuRepositoryV2 : CrudRepository<LiveRoomMenu, Long> { | ||||
|     fun findByCreatorId(creatorId: Long): List<LiveRoomMenu> | ||||
| } | ||||
		Reference in New Issue
	
	Block a user