룰렛 데이터 생성/수정 API 추가
This commit is contained in:
parent
1a5b4b364a
commit
e96b3d9bd4
|
@ -0,0 +1,7 @@
|
|||
package kr.co.vividnext.sodalive.live.roulette
|
||||
|
||||
data class CreateOrUpdateRouletteRequest(
|
||||
val can: Int,
|
||||
val isActive: Boolean,
|
||||
val items: List<RouletteItem>
|
||||
)
|
|
@ -0,0 +1,18 @@
|
|||
package kr.co.vividnext.sodalive.live.roulette
|
||||
|
||||
import org.springframework.data.annotation.Id
|
||||
import org.springframework.data.redis.core.RedisHash
|
||||
|
||||
@RedisHash("roulette")
|
||||
data class Roulette(
|
||||
@Id
|
||||
val creatorId: Long,
|
||||
var can: Int,
|
||||
var isActive: Boolean,
|
||||
var items: List<RouletteItem>
|
||||
)
|
||||
|
||||
data class RouletteItem(
|
||||
val title: String,
|
||||
val percentage: Int
|
||||
)
|
|
@ -0,0 +1,27 @@
|
|||
package kr.co.vividnext.sodalive.live.roulette
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/roulette")
|
||||
class RouletteController(private val service: RouletteService) {
|
||||
@PostMapping
|
||||
fun createOrUpdateRoulette(
|
||||
@RequestBody request: CreateOrUpdateRouletteRequest,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
if (member == null || member.role != MemberRole.CREATOR) {
|
||||
throw SodaException("로그인 정보를 확인해주세요.")
|
||||
}
|
||||
|
||||
ApiResponse.ok(service.createOrUpdateRoulette(memberId = member.id!!, request = request))
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package kr.co.vividnext.sodalive.live.roulette
|
||||
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface RouletteRepository : CrudRepository<Roulette, Long>
|
|
@ -0,0 +1,39 @@
|
|||
package kr.co.vividnext.sodalive.live.roulette
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class RouletteService(private val repository: RouletteRepository) {
|
||||
fun createOrUpdateRoulette(memberId: Long, request: CreateOrUpdateRouletteRequest) {
|
||||
rouletteValidate(request)
|
||||
|
||||
val roulette = repository.findByIdOrNull(id = memberId) ?: Roulette(
|
||||
creatorId = memberId,
|
||||
can = request.can,
|
||||
isActive = request.isActive,
|
||||
items = request.items
|
||||
)
|
||||
|
||||
repository.save(roulette)
|
||||
}
|
||||
|
||||
private fun rouletteValidate(request: CreateOrUpdateRouletteRequest) {
|
||||
if (request.can < 5) {
|
||||
throw SodaException("룰렛 금액은 최소 5캔 입니다.")
|
||||
}
|
||||
|
||||
if (request.items.size < 2 || request.items.size > 6) {
|
||||
throw SodaException("룰렛 옵션은 최소 2개, 최대 6개까지 설정할 수 있습니다.")
|
||||
}
|
||||
|
||||
val percentage = request.items.asSequence()
|
||||
.map { it.percentage }
|
||||
.reduce { acc, percentage -> acc + percentage }
|
||||
|
||||
if (percentage != 100) {
|
||||
throw SodaException("옵션 확률의 합이 100%가 아닙니다.")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue