Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backendsquid.buckpal.account.adapter.`in`.web

import backendsquid.buckpal.account.application.port.`in`.SendMoneyCommand
import backendsquid.buckpal.account.application.port.`in`.SendMoneyUseCase
import backendsquid.buckpal.account.application.service.SendMoneyResult
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand All @@ -21,7 +22,7 @@ class SendMoneyController(
@PathVariable("sourceAccountId") @Positive sourceAccountId: Long,
@PathVariable("targetAccountId") @Positive targetAccountId: Long,
@PathVariable("amount") @Positive amount: Long,
): Boolean {
): SendMoneyResult {
val command = SendMoneyCommand(
sourceAccountId = sourceAccountId,
targetAccountId = targetAccountId,
Expand All @@ -34,7 +35,7 @@ class SendMoneyController(
@PostMapping("/accounts/send")
fun sendMoneyByBody(
@RequestBody @Valid sendMoneyResource: SendMoneyResource
): Boolean {
): SendMoneyResult {
val command = SendMoneyCommand(
sourceAccountId = sendMoneyResource.sourceAccountId,
targetAccountId = sendMoneyResource.targetAccountId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package backendsquid.buckpal.account.application.port.`in`

import backendsquid.buckpal.account.application.service.SendMoneyResult

interface SendMoneyUseCase {
fun sendMoney(command: SendMoneyCommand): Boolean
fun sendMoney(command: SendMoneyCommand): SendMoneyResult
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package backendsquid.buckpal.account.application.service

data class SendMoneyResult(
val boolean : Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SendMoneyService(
private val accountLock: AccountLock,
private val updateAccountStatePort: UpdateAccountStatePort,
): SendMoneyUseCase {
override fun sendMoney(command: SendMoneyCommand): Boolean {
override fun sendMoney(command: SendMoneyCommand): SendMoneyResult {
val baseline = LocalDateTime.now().minusDays(10)
val sourceAccount = loadAccountPort.loadAccount(
accountId = command.sourceAccountId,
Expand All @@ -30,13 +30,13 @@ class SendMoneyService(
accountLock.lockAccount(sourceAccount.id)
if (!sourceAccount.withdraw(money = command.money, targetAccountId = targetAccount.id)) {
accountLock.releaseAccount(sourceAccount.id)
return false
return SendMoneyResult(false)
}

accountLock.lockAccount(targetAccount.id)
if (!targetAccount.deposit(money = command.money, sourceAccountId = sourceAccount.id)) {
accountLock.releaseAccount(targetAccount.id)
return false
return SendMoneyResult(false)
}

updateAccountStatePort.updateActivities(sourceAccount)
Expand All @@ -45,6 +45,6 @@ class SendMoneyService(
accountLock.releaseAccount(sourceAccount.id)
accountLock.releaseAccount(targetAccount.id)

return true
return SendMoneyResult(true)
}
}