Skip to content

Commit

Permalink
#12 충전 request mongoDB 임시 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
suyeon0 committed Aug 7, 2023
1 parent aa98efd commit 68614d9
Show file tree
Hide file tree
Showing 23 changed files with 447 additions and 6 deletions.
21 changes: 21 additions & 0 deletions api-interface/src/main/kotlin/com/racket/api/cache/CacheService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.racket.api.cache

import com.racket.api.cache.domain.entity.UserChargingWayInfo
import com.racket.api.cache.presentation.reponse.CacheBalanceResponseView
import com.racket.api.cache.presentation.reponse.ChargeResponseView
import java.util.*

interface CacheService {

fun charge(chargeDTO: ChargeDTO): ChargeResponseView

fun getBalanceByUserId(userId: Long): CacheBalanceResponseView

fun getChargingWayById(userId: Long): UserChargingWayInfo

data class ChargeDTO (
val userId: Long,
val amount: Long,
val chargingWayId: Long
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.racket.api.cache

import com.racket.api.cache.domain.CacheBalanceRepository
import com.racket.api.cache.domain.CacheTransactionRepository
import com.racket.api.cache.domain.UserChargingWayInfoRepository
import com.racket.api.cache.domain.entity.CacheBalance
import com.racket.api.cache.domain.entity.CacheTransaction
import com.racket.api.cache.domain.entity.UserChargingWayInfo
import com.racket.api.cache.domain.enums.CacheEventType
import com.racket.api.cache.exception.NotExistSavedChargeWay
import com.racket.api.cache.presentation.reponse.CacheBalanceResponseView
import com.racket.api.cache.presentation.reponse.ChargeResponseView
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.stereotype.Service
import java.time.LocalDateTime

@Service
class CacheServiceImpl(
val cacheTransactionRepository: CacheTransactionRepository,
val cacheBalanceRepository: CacheBalanceRepository,
val cacheUserChargingWayInfoRepository: UserChargingWayInfoRepository,
val mongoTemplate: MongoTemplate

): CacheService {
override fun charge(chargeDTO: CacheService.ChargeDTO): ChargeResponseView {
val transaction = this.cacheTransactionRepository.save(this.createCacheTransactionEntity(chargeDTO))
return ChargeResponseView(
id = transaction.id!!,
amount = transaction.amount,
userId = transaction.userId
)
}

private fun createCacheTransactionEntity(chargeDTO: CacheService.ChargeDTO) =
CacheTransaction (
userId = chargeDTO.userId,
amount = chargeDTO.amount,
chargingWayId = chargeDTO.chargingWayId,
eventType = CacheEventType.CHARGE
)

private fun validateToCharge() {

}

override fun getBalanceByUserId(userId: Long): CacheBalanceResponseView {
val cacheBalance = this.cacheBalanceRepository.findById(userId).orElseGet { CacheBalance(userId = userId, balance = 0) }
return CacheBalanceResponseView(
userId = cacheBalance.userId,
balance = cacheBalance.balance
)
}

override fun getChargingWayById(id: Long): UserChargingWayInfo =
this.cacheUserChargingWayInfoRepository.findById(id).orElseThrow { NotExistSavedChargeWay() }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.racket.api.cache

interface CacheUserService {

fun validateUserId(userId: Long)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.racket.api.cache

import com.racket.api.user.UserService
import org.springframework.stereotype.Service

@Service
class CacheUserServiceImpl(
private val userService: UserService
): CacheUserService {

override fun validateUserId(userId: Long) {
this.userService.getUser(id = userId)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.racket.api.cache.domain

import com.racket.api.cache.domain.entity.CacheBalance
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository

@Repository
interface CacheBalanceRepository: CrudRepository<CacheBalance, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.racket.api.cache.domain

import com.racket.api.cache.domain.entity.CacheTransaction
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface CacheTransactionRepository: MongoRepository<CacheTransaction, Long> {
fun findByUserId(userId: Long): List<CacheTransaction?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.racket.api.cache.domain

import com.racket.api.cache.domain.entity.UserChargingWayInfo
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository


@Repository
interface UserChargingWayInfoRepository: CrudRepository<UserChargingWayInfo, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.racket.api.cache.domain.entity

import javax.persistence.Entity
import javax.persistence.Id

@Entity
class CacheBalance(

@Id
val userId: Long,

val balance: Long

) {
fun validateBalance() {
if (this.balance < 0) {
throw IllegalArgumentException("balance is must be greater than zero.")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.racket.api.cache.domain.entity

import com.racket.api.cache.domain.enums.CacheEventType
import org.bson.types.ObjectId
import org.springframework.data.mongodb.core.mapping.Document
import java.util.*
import javax.persistence.*

@Document("cacheTransaction")
class CacheTransaction(

@Id
@Column(name = "cache_transaction_id", nullable = false)
var id: ObjectId? = null,

@Column(name = "user_id", nullable = false)
val userId: Long,

@Column(name = "amount", nullable = false)
val amount: Long,

@Column(name = "event_type", nullable = false)
val eventType: CacheEventType,

@Column(name = "charging_way", nullable = false)
val chargingWayId: Long

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.racket.api.cache.domain.entity

import com.racket.api.cache.domain.enums.ChargingWayType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated
import javax.persistence.GeneratedValue
import javax.persistence.Id


// 유저가 저장해놓은 충전 계좌
@Entity
class UserChargingWayInfo(

@Id @GeneratedValue
@Column(name = "id", nullable = false)
val id: Long,

@Column(name = "user_id", nullable = false)
val userId: Long,

@Column(name = "seq", nullable = false)
val seq: Long,

@Enumerated(EnumType.STRING)
@Column(name = "charging_way", nullable = false)
val chargingWayType: ChargingWayType,

@Column(name = "bank_id", nullable = false)
val bankId: Long,

@Column(name = "account", nullable = false)
val account: String,

@Column(name = "useYn", nullable = false)
var useYn: Boolean
) {

fun updateUseYn(isUse: Boolean) {
this.useYn = isUse
}

fun isInvalidChargingWay() = !this.useYn

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.racket.api.cache.domain.enums

enum class CacheEventType {
CHARGE, USE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.racket.api.cache.domain.enums

enum class ChargingWayType(desc: String) {

BANK_ACCOUNT("은행 계좌")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.racket.api.cache.exception

class NotExistSavedChargeWay: RuntimeException() {
override val message: String = "The charging way must be saved"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.racket.api.cache.presentation

import com.racket.api.cache.CacheService
import com.racket.api.cache.CacheUserService
import com.racket.api.cache.presentation.reponse.ChargeResponseView
import com.racket.api.cache.presentation.request.CreateChargeCommand
import com.racket.api.shared.annotation.LongTypeIdInputValidator
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/api/v1/racket-cache")
class CacheController(
private val cacheService: CacheService,
private val cacheUserService: CacheUserService
) : CacheSpecification {
override fun postToCharge(chargeCommand: CreateChargeCommand): ResponseEntity<ChargeResponseView> {
this.ValidateToCharge(chargeCommand)

return ResponseEntity.status(HttpStatus.CREATED)
.body(
this.cacheService.charge(
CacheService.ChargeDTO(
userId = chargeCommand.userId,
amount = chargeCommand.amount,
chargingWayId = chargeCommand.userChargingWayId
)
)
)
}

private fun ValidateToCharge(chargeCommand: CreateChargeCommand) {
chargeCommand.validate()
this.cacheUserService.validateUserId(chargeCommand.userId)
}

@LongTypeIdInputValidator
override fun getBalanceByUserId(userId: Long) =
ResponseEntity.ok().body(this.cacheService.getBalanceByUserId(userId))

override fun getTransactionList() {
TODO("Not yet implemented")
}

override fun postToUse() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.racket.api.cache.presentation

import com.racket.api.cache.presentation.reponse.CacheBalanceResponseView
import com.racket.api.cache.presentation.reponse.ChargeResponseView
import com.racket.api.cache.presentation.request.CreateChargeCommand
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody

@Tag(name = "RacketCache-API", description = "캐시 충전 및 사용을 담당")
interface CacheSpecification {

@PostMapping("/charge")
fun postToCharge(@RequestBody chargeCommand: CreateChargeCommand): ResponseEntity<ChargeResponseView>

@GetMapping("/balance/{userId}")
fun getBalanceByUserId(userId: Long): ResponseEntity<CacheBalanceResponseView>

@GetMapping("/transactions")
fun getTransactionList()

@PostMapping("/use")
fun postToUse()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.racket.api.cache.presentation.reponse

data class CacheBalanceResponseView (

val userId: Long,
val balance: Long

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.racket.api.cache.presentation.reponse

import org.bson.types.ObjectId

data class ChargeResponseView(

val id: ObjectId,
val userId: Long,
val amount: Long

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.racket.api.cache.presentation.request

data class CreateChargeCommand(
val userId: Long,
val amount: Long,
val userChargingWayId: Long
) {
fun validate() {
if (this.amount < 100_000) {
throw IllegalArgumentException("Charge cache must be greater than 100,000")
}
}
}
Loading

0 comments on commit 68614d9

Please sign in to comment.