메뉴판 프리셋 API

This commit is contained in:
Klaus 2024-03-05 15:40:24 +09:00
parent 157936acef
commit 945fd5d5a3
7 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,3 @@
package kr.co.vividnext.sodalive.live.room.menu
data class CreateLiveMenuRequest(val menu: String, val isActive: Boolean)

View File

@ -0,0 +1,6 @@
package kr.co.vividnext.sodalive.live.room.menu
data class GetMenuResponse(
val id: Long,
val menu: String
)

View File

@ -0,0 +1,15 @@
package kr.co.vividnext.sodalive.live.room.menu
import org.springframework.data.redis.core.RedisHash
import org.springframework.data.redis.core.index.Indexed
import javax.persistence.Id
@RedisHash("LiveRoomMenu")
data class LiveRoomMenu(
@Id
val id: Long,
@Indexed
val creatorId: Long,
var isActive: Boolean,
var menu: String
)

View File

@ -0,0 +1,66 @@
package kr.co.vividnext.sodalive.live.room.menu
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.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/live/room/menu")
class LiveRoomMenuController(private val service: LiveRoomMenuService) {
@GetMapping("/creator")
@PreAuthorize("hasRole('CREATOR')")
fun getAllLiveMenu(
@RequestParam creatorId: Long,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(service.getAllLiveMenu(creatorId = creatorId, memberId = member.id!!))
}
@PostMapping
@PreAuthorize("hasRole('CREATOR')")
fun createLiveMenu(
@RequestBody request: CreateLiveMenuRequest,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null || member.role != MemberRole.CREATOR) {
throw SodaException("로그인 정보를 확인해주세요.")
}
ApiResponse.ok(service.createLiveMenu(memberId = member.id!!, request = request))
}
@PutMapping
@PreAuthorize("hasRole('CREATOR')")
fun updateLiveMenu(
@RequestBody request: UpdateLiveMenuRequest,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null || member.role != MemberRole.CREATOR) {
throw SodaException("로그인 정보를 확인해주세요.")
}
ApiResponse.ok(service.updateLiveMenu(memberId = member.id!!, request = request))
}
@GetMapping
fun getMenu(
@RequestParam creatorId: Long,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(service.getLiveMenu(creatorId = creatorId))
}
}

View File

@ -0,0 +1,7 @@
package kr.co.vividnext.sodalive.live.room.menu
import org.springframework.data.repository.CrudRepository
interface LiveRoomMenuRepository : CrudRepository<LiveRoomMenu, Long> {
fun findByCreatorId(creatorId: Long): List<LiveRoomMenu>
}

View File

@ -0,0 +1,102 @@
package kr.co.vividnext.sodalive.live.room.menu
import kr.co.vividnext.sodalive.common.SodaException
import kr.co.vividnext.sodalive.live.roulette.RedisIdGenerator
import org.springframework.stereotype.Service
@Service
class LiveRoomMenuService(
private val idGenerator: RedisIdGenerator,
private val repository: LiveRoomMenuRepository
) {
fun getAllLiveMenu(creatorId: Long, memberId: Long): List<GetMenuResponse> {
if (creatorId != memberId) throw SodaException("잘못된 요청입니다.")
return repository.findByCreatorId(creatorId)
.sortedBy { it.id }
.asSequence()
.map { GetMenuResponse(id = it.id, menu = it.menu) }
.toList()
}
fun createLiveMenu(memberId: Long, request: CreateLiveMenuRequest): Boolean {
liveMenuValidate(menu = request.menu)
if (request.isActive) {
val menuList = repository.findByCreatorId(creatorId = memberId)
menuList.forEach {
it.isActive = false
repository.save(it)
}
}
val menu = LiveRoomMenu(
id = idGenerator.generateId(SEQUENCE_NAME),
creatorId = memberId,
isActive = request.isActive,
menu = request.menu
)
repository.save(menu)
return request.isActive
}
fun updateLiveMenu(memberId: Long, request: UpdateLiveMenuRequest): Boolean {
liveMenuValidate(menu = request.menu)
val menuList = repository.findByCreatorId(creatorId = memberId)
if (menuList.isEmpty()) {
throw SodaException("잘못된 요청입니다.")
}
var isActive = false
menuList.forEach {
if (request.isActive || it.isActive) {
isActive = true
}
if (it.id == request.id) {
it.menu = request.menu
it.isActive = request.isActive
repository.save(it)
} else if (request.isActive) {
it.isActive = false
repository.save(it)
}
}
return isActive
}
fun getLiveMenu(creatorId: Long): GetMenuResponse {
val menuList = repository.findByCreatorId(creatorId = creatorId)
if (menuList.isEmpty()) {
throw SodaException("설정된 메뉴판이 없습니다.")
}
var activeMenu: LiveRoomMenu? = null
for (menu in menuList) {
if (menu.isActive) {
activeMenu = menu
break
}
}
if (activeMenu == null || activeMenu.menu.isEmpty()) {
throw SodaException("설정된 메뉴판이 없습니다.")
}
return GetMenuResponse(id = activeMenu.id, menu = activeMenu.menu)
}
private fun liveMenuValidate(menu: String) {
if (menu.isBlank()) {
throw SodaException("메뉴판은 빈칸일 수 없습니다.")
}
}
companion object {
const val SEQUENCE_NAME = "LiveRoomMenu:sequence"
}
}

View File

@ -0,0 +1,3 @@
package kr.co.vividnext.sodalive.live.room.menu
data class UpdateLiveMenuRequest(val id: Long, val menu: String, val isActive: Boolean)