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