Skip to content

Commit

Permalink
Merge pull request #132 from mash-up-kr/jm/add-reword-complete-pick
Browse files Browse the repository at this point in the history
add reword complete pick
  • Loading branch information
xonmin authored Sep 16, 2024
2 parents 9573d55 + 7acf737 commit 1f13382
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
22 changes: 22 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 @@ -4,20 +4,25 @@ import com.mashup.dojo.CoinEntity
import com.mashup.dojo.CoinRepository
import com.mashup.dojo.CoinUseDetailEntity
import com.mashup.dojo.CoinUseDetailRepository
import com.mashup.dojo.DojoException
import com.mashup.dojo.DojoExceptionType
import com.mashup.dojo.UsageStatus
import com.mashup.dojo.domain.Coin
import com.mashup.dojo.domain.CoinId
import com.mashup.dojo.domain.CoinUseDetail
import com.mashup.dojo.domain.CoinUseDetailId
import com.mashup.dojo.domain.CoinUseType
import com.mashup.dojo.domain.MemberId
import jakarta.transaction.Transactional
import org.springframework.stereotype.Service

interface CoinService {
fun getCoin(memberId: MemberId): Coin?

fun create(memberId: MemberId): CoinId

fun rewardCoinForCompletePick(memberId: MemberId): CoinUseDetailId

fun updateCoin(
useType: CoinUseType,
detail: String,
Expand Down Expand Up @@ -45,6 +50,19 @@ class DefaultCoinService(
return coin.id
}

@Transactional
override fun rewardCoinForCompletePick(memberId: MemberId): CoinUseDetailId {
val coin = getCoin(memberId) ?: throw DojoException.of(DojoExceptionType.NOT_EXIST, "유저의 코인정보가 없습니다")
val updatedCoin = coin.earnCoin(COMPLETE_PICK_COIN_AMOUNT.toLong())

return updateCoin(
useType = CoinUseType.EARNED,
detail = CoinUseDetail.REASON_COMPLETE_PICK,
cost = COMPLETE_PICK_COIN_AMOUNT,
coin = updatedCoin
)
}

override fun updateCoin(
useType: CoinUseType,
detail: String,
Expand All @@ -63,6 +81,10 @@ class DefaultCoinService(
val coinUseDetailEntity = coinUseDetailRepository.save(coinUseDetail.toEntity())
return CoinUseDetailId(coinUseDetailEntity.id)
}

companion object {
private const val COMPLETE_PICK_COIN_AMOUNT = 200
}
}

private fun CoinEntity.toCoin(): Coin {
Expand Down
43 changes: 31 additions & 12 deletions service/src/main/kotlin/com/mashup/dojo/usecase/PickUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ import com.mashup.dojo.service.QuestionService
import com.mashup.dojo.usecase.PickUseCase.GetReceivedPickPagingCommand
import com.mashup.dojo.usecase.PickUseCase.OpenPickCommand
import com.mashup.dojo.usecase.PickUseCase.PickOpenInfo
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

private val log = KotlinLogging.logger {}

interface PickUseCase {
data class GetReceivedPickPagingCommand(
val memberId: MemberId,
Expand Down Expand Up @@ -86,6 +90,8 @@ interface PickUseCase {

@Component
class DefaultPickUseCase(
@Value("\${dojo.questionSet.size}")
private val questionSetSize: Int,
private val pickService: PickService,
private val questionService: QuestionService,
private val imageService: ImageService,
Expand All @@ -112,19 +118,32 @@ class DefaultPickUseCase(

val questionSet = questionService.getQuestionSetById(command.questionSetId) ?: throw DojoException.of(DojoExceptionType.QUESTION_SET_NOT_EXIST)

return pickService.create(
questionId = question.id,
questionSetId = questionSet.id,
questionSheetId = command.questionSheetId,
pickerMemberId = command.pickerId,
pickedMemberId = pickedMember.id
).apply {
notificationService.notifyPicked(
pickId = this,
target = pickedMember,
questionId = question.id
)
val pickId =
pickService.create(
questionId = question.id,
questionSetId = questionSet.id,
questionSheetId = command.questionSheetId,
pickerMemberId = command.pickerId,
pickedMemberId = pickedMember.id
).apply {
notificationService.notifyPicked(
pickId = this,
target = pickedMember,
questionId = question.id
)
}

// QSet 에 대한 모든 픽을 완료한 경우, 보상으로 코인 제공
if (pickService.getSolvedPickList(command.pickerId, questionSet.id).size == questionSetSize) {
coinService.rewardCoinForCompletePick(command.pickerId)
.also { coinUseDetailId ->
log.info {
"reward for Complete pick. memberId: [${command.pickerId}], QSetId: [${questionSet.id}], coinUseDetailId: $coinUseDetailId"
}
}
}

return pickId
}

@Transactional
Expand Down

0 comments on commit 1f13382

Please sign in to comment.