Skip to content

Commit

Permalink
add: 픽 정보 오픈할 때 코인 사용 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
wonlog-g committed Aug 21, 2024
1 parent b456ae2 commit 7900abe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions service/src/main/kotlin/com/mashup/dojo/domain/Coin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ data class CoinUseDetail(
) {
companion object {
const val REASON_PROVIDE_DEFAULT = "provide by default"
const val REASON_USED_FOR_OPEN_PICK = "used for open pick"

fun createEarnedCoinUseDetail(
coinId: CoinId,
Expand Down
25 changes: 25 additions & 0 deletions service/src/main/kotlin/com/mashup/dojo/service/CoinService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ interface CoinService {
fun getCoin(memberId: MemberId): Coin?

fun create(memberId: MemberId): CoinId

fun updateCoin(
useType: CoinUseType,
detail: String,
cost: Int,
coin: Coin,
)
}

@Service
Expand All @@ -36,6 +43,24 @@ class DefaultCoinService(

return coin.id
}

override fun updateCoin(
useType: CoinUseType,
detail: String,
cost: Int,
coin: Coin,
) {
coinRepository.save(coin.toEntity())

val coinUseDetail =
if (CoinUseType.USED == useType) {
CoinUseDetail.createUsedCoinUseDetail(coin.id, cost.toLong(), detail)
} else {
CoinUseDetail.createEarnedCoinUseDetail(coin.id, cost.toLong(), detail)
}

coinUseDetailRepository.save(coinUseDetail.toEntity())
}
}

private fun CoinEntity.toCoin(): Coin {
Expand Down
11 changes: 11 additions & 0 deletions service/src/main/kotlin/com/mashup/dojo/usecase/PickUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package com.mashup.dojo.usecase
import com.mashup.dojo.DojoException
import com.mashup.dojo.DojoExceptionType
import com.mashup.dojo.Status
import com.mashup.dojo.domain.CoinUseDetail
import com.mashup.dojo.domain.CoinUseType
import com.mashup.dojo.domain.MemberId
import com.mashup.dojo.domain.PickId
import com.mashup.dojo.domain.PickOpenItem
import com.mashup.dojo.domain.PickSort
import com.mashup.dojo.domain.QuestionId
import com.mashup.dojo.domain.QuestionSetId
import com.mashup.dojo.domain.QuestionSheetId
import com.mashup.dojo.service.CoinService
import com.mashup.dojo.service.ImageService
import com.mashup.dojo.service.MemberService
import com.mashup.dojo.service.NotificationService
Expand All @@ -19,6 +22,7 @@ import com.mashup.dojo.usecase.PickUseCase.GetReceivedPickPagingCommand
import com.mashup.dojo.usecase.PickUseCase.OpenPickCommand
import com.mashup.dojo.usecase.PickUseCase.PickOpenInfo
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

interface PickUseCase {
Expand Down Expand Up @@ -86,6 +90,7 @@ class DefaultPickUseCase(
private val imageService: ImageService,
private val memberService: MemberService,
private val notificationService: NotificationService,
private val coinService: CoinService,
) : PickUseCase {
override fun getReceivedPickList(command: GetReceivedPickPagingCommand): PickService.GetPickPaging {
return pickService.getReceivedPickPaging(
Expand Down Expand Up @@ -121,7 +126,13 @@ class DefaultPickUseCase(
}
}

@Transactional
override fun openPick(openPickCommand: OpenPickCommand): PickOpenInfo {
val coin = coinService.getCoin(openPickCommand.pickedId) ?: throw DojoException.of(DojoExceptionType.NOT_EXIST, "유저의 코인정보가 없습니다")
val updatedCoin = coin.useCoin(openPickCommand.pickOpenItem.cost.toLong())

coinService.updateCoin(CoinUseType.USED, CoinUseDetail.REASON_USED_FOR_OPEN_PICK, openPickCommand.pickOpenItem.cost, updatedCoin)

return pickService.openPick(
openPickCommand.pickId,
openPickCommand.pickedId,
Expand Down

0 comments on commit 7900abe

Please sign in to comment.