-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
447 additions
and
6 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
api-interface/src/main/kotlin/com/racket/api/cache/CacheService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
57 changes: 57 additions & 0 deletions
57
api-interface/src/main/kotlin/com/racket/api/cache/CacheServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
api-interface/src/main/kotlin/com/racket/api/cache/CacheUserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
api-interface/src/main/kotlin/com/racket/api/cache/CacheUserServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
api-interface/src/main/kotlin/com/racket/api/cache/domain/CacheBalanceRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
10 changes: 10 additions & 0 deletions
10
api-interface/src/main/kotlin/com/racket/api/cache/domain/CacheTransactionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>? | ||
} |
9 changes: 9 additions & 0 deletions
9
api-interface/src/main/kotlin/com/racket/api/cache/domain/UserChargingWayInfoRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
20 changes: 20 additions & 0 deletions
20
api-interface/src/main/kotlin/com/racket/api/cache/domain/entity/CacheBalance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api-interface/src/main/kotlin/com/racket/api/cache/domain/entity/CacheTransaction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
) |
46 changes: 46 additions & 0 deletions
46
api-interface/src/main/kotlin/com/racket/api/cache/domain/entity/UserChargingWayInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
api-interface/src/main/kotlin/com/racket/api/cache/domain/enums/CacheEventType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
6 changes: 6 additions & 0 deletions
6
api-interface/src/main/kotlin/com/racket/api/cache/domain/enums/ChargingWayType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("은행 계좌") | ||
} |
5 changes: 5 additions & 0 deletions
5
api-interface/src/main/kotlin/com/racket/api/cache/exception/NotExistSavedChargeWay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
51 changes: 51 additions & 0 deletions
51
api-interface/src/main/kotlin/com/racket/api/cache/presentation/CacheController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
api-interface/src/main/kotlin/com/racket/api/cache/presentation/CacheSpecification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ace/src/main/kotlin/com/racket/api/cache/presentation/reponse/CacheBalanceResponseView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
) |
11 changes: 11 additions & 0 deletions
11
...interface/src/main/kotlin/com/racket/api/cache/presentation/reponse/ChargeResponseView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
) |
13 changes: 13 additions & 0 deletions
13
...nterface/src/main/kotlin/com/racket/api/cache/presentation/request/CreateChargeCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
Oops, something went wrong.