룰렛 저장 API, 룰렛 가져오기 API 수정
This commit is contained in:
parent
382364b5df
commit
aeed1dbd06
|
@ -9,7 +9,7 @@ data class Roulette(
|
|||
val creatorId: Long,
|
||||
var can: Int,
|
||||
var isActive: Boolean,
|
||||
var items: List<RouletteItem>
|
||||
var items: List<RouletteItem> = mutableListOf()
|
||||
)
|
||||
|
||||
data class RouletteItem(
|
||||
|
|
|
@ -34,12 +34,7 @@ class RouletteController(private val service: RouletteService) {
|
|||
): ApiResponse<GetRouletteResponse> {
|
||||
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
|
||||
|
||||
val response = service.getRoulette(creatorId = creatorId)
|
||||
if (response == null && creatorId != member.id!!) {
|
||||
throw SodaException("룰렛을 사용할 수 없습니다.")
|
||||
}
|
||||
|
||||
return ApiResponse.ok(response)
|
||||
return ApiResponse.ok(service.getRoulette(creatorId = creatorId, memberId = member.id!!))
|
||||
}
|
||||
|
||||
@PostMapping("/spin")
|
||||
|
|
|
@ -8,8 +8,6 @@ import kr.co.vividnext.sodalive.member.MemberRole
|
|||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import kotlin.concurrent.write
|
||||
|
||||
@Service
|
||||
class RouletteService(
|
||||
|
@ -17,22 +15,26 @@ class RouletteService(
|
|||
private val repository: RouletteRepository,
|
||||
private val roomRepository: LiveRoomRepository
|
||||
) {
|
||||
private val tokenLocks: MutableMap<Long, ReentrantReadWriteLock> = mutableMapOf()
|
||||
|
||||
fun createOrUpdateRoulette(memberId: Long, request: CreateOrUpdateRouletteRequest) {
|
||||
fun createOrUpdateRoulette(memberId: Long, request: CreateOrUpdateRouletteRequest): Boolean {
|
||||
rouletteValidate(request)
|
||||
|
||||
val roulette = repository.findByIdOrNull(id = memberId) ?: Roulette(
|
||||
creatorId = memberId,
|
||||
can = request.can,
|
||||
isActive = request.isActive,
|
||||
items = request.items
|
||||
)
|
||||
|
||||
val lock = getOrCreateLock(memberId = memberId)
|
||||
lock.write {
|
||||
repository.save(roulette)
|
||||
var roulette = repository.findByIdOrNull(id = memberId)
|
||||
if (roulette != null) {
|
||||
roulette.can = request.can
|
||||
roulette.isActive = request.isActive
|
||||
roulette.items = request.items
|
||||
} else {
|
||||
roulette = Roulette(
|
||||
creatorId = memberId,
|
||||
can = request.can,
|
||||
isActive = request.isActive,
|
||||
items = request.items
|
||||
)
|
||||
}
|
||||
|
||||
repository.save(roulette)
|
||||
|
||||
return request.isActive
|
||||
}
|
||||
|
||||
private fun rouletteValidate(request: CreateOrUpdateRouletteRequest) {
|
||||
|
@ -45,14 +47,18 @@ class RouletteService(
|
|||
}
|
||||
}
|
||||
|
||||
fun getRoulette(creatorId: Long): GetRouletteResponse? {
|
||||
fun getRoulette(creatorId: Long, memberId: Long): GetRouletteResponse? {
|
||||
val roulette = repository.findByIdOrNull(id = creatorId)
|
||||
|
||||
if (roulette == null || !roulette.isActive || roulette.items.isEmpty()) {
|
||||
return null
|
||||
if (creatorId != memberId && (roulette == null || !roulette.isActive || roulette.items.isEmpty())) {
|
||||
throw SodaException("룰렛을 사용할 수 없습니다.")
|
||||
}
|
||||
|
||||
return GetRouletteResponse(can = roulette.can, isActive = roulette.isActive, items = roulette.items)
|
||||
return GetRouletteResponse(
|
||||
can = roulette?.can ?: 5,
|
||||
isActive = roulette?.isActive ?: false,
|
||||
items = roulette?.items ?: listOf()
|
||||
)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -85,8 +91,4 @@ class RouletteService(
|
|||
|
||||
return GetRouletteResponse(can = roulette.can, isActive = roulette.isActive, items = roulette.items)
|
||||
}
|
||||
|
||||
private fun getOrCreateLock(memberId: Long): ReentrantReadWriteLock {
|
||||
return tokenLocks.computeIfAbsent(memberId) { ReentrantReadWriteLock() }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue