Skip to content

Commit

Permalink
transfer monkey
Browse files Browse the repository at this point in the history
  • Loading branch information
agiledragon committed Nov 28, 2018
1 parent 281e250 commit e9cd36c
Show file tree
Hide file tree
Showing 17 changed files with 387 additions and 0 deletions.
21 changes: 21 additions & 0 deletions transfer-money/app/service/account_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package service

import (
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/service"
)

func CreateAccount(accountId string, amount uint, phoneNumber string) {
service.GetAccountService().Create(accountId, amount, phoneNumber)
}

func TransferMoney(srcAccountId, dstAccountId string, amount uint) {
service.GetAccountService().TransferMoney(srcAccountId, dstAccountId, amount)
}

func GetAmount(accountId string) (uint, error) {
return service.GetAccountService().GetAmount(accountId)
}

func DestroyAccount(accountId string) {
service.GetAccountService().Destroy(accountId)
}
22 changes: 22 additions & 0 deletions transfer-money/domain/model/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package model

import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base"

type Account struct {
base.AggregateRoot
balance *balance
phone *phone
MoneySource *moneySource
MoneyDestination *moneyDestination
}

func newAccount(accountId string, balance *balance, phone *phone) *Account {
account := &Account{
AggregateRoot: base.NewAggregateRoot(accountId),
balance: balance,
phone: phone,
}
account.MoneySource = newMoneySource(accountId, balance, phone)
account.MoneyDestination = newMoneyDestination(accountId, balance, phone)
return account
}
10 changes: 10 additions & 0 deletions transfer-money/domain/model/account_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package model

type AccountFactory struct {
}

func (this AccountFactory) Create(accountId string, amount uint, phoneNumber string) *Account {
b := newBalance(amount)
p := newPhone(accountId, phoneNumber)
return newAccount(accountId, b, p)
}
17 changes: 17 additions & 0 deletions transfer-money/domain/model/account_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

var repoInst AccountRepo

type AccountRepo interface {
Add(account *Account)
Get(accountId string) *Account
Remove(accountId string)
}

func SetAccountRepo(repo AccountRepo) {
repoInst = repo
}

func GetAccountRepo() AccountRepo {
return repoInst
}
24 changes: 24 additions & 0 deletions transfer-money/domain/model/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package model

import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base"

type balance struct {
base.ValueObject
amount uint
}

func newBalance(amount uint) *balance {
return &balance{amount: amount}
}

func (this *balance) increase(amount uint) {
this.amount += amount
}

func (this *balance) decrease(amount uint) {
this.amount -= amount
}

func (this *balance) get() uint {
return this.amount
}
9 changes: 9 additions & 0 deletions transfer-money/domain/model/base/aggregate_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package base

type AggregateRoot struct {
Entity
}

func NewAggregateRoot(id string) AggregateRoot {
return AggregateRoot{NewEntity(id)}
}
21 changes: 21 additions & 0 deletions transfer-money/domain/model/base/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package base

type Entity struct {
id string
}

func NewEntity(id string) Entity {
return Entity{id: id}
}

func (t *Entity) Id() string {
return t.id
}

func (t *Entity) Equal(other *Entity) bool {
return t.id == other.id
}

func (t *Entity) NotEqual(other *Entity) bool {
return !t.Equal(other)
}
4 changes: 4 additions & 0 deletions transfer-money/domain/model/base/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package base

type Role struct {
}
4 changes: 4 additions & 0 deletions transfer-money/domain/model/base/value_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package base

type ValueObject struct {
}
25 changes: 25 additions & 0 deletions transfer-money/domain/model/money_destination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model

import (
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base"
)

type moneyDestination struct {
base.Role
accountId string
balance *balance
phone *phone
}

func newMoneyDestination(accountId string, balance *balance, phone *phone) *moneyDestination {
return &moneyDestination{accountId: accountId, balance: balance, phone: phone}
}

func (this *moneyDestination) getAccountId() string {
return this.accountId
}

func (this *moneyDestination) transferMoneyFrom(srcAccountId string, amount uint) {
this.balance.increase(amount)
this.phone.sendTransferFromMsg(srcAccountId, amount)
}
29 changes: 29 additions & 0 deletions transfer-money/domain/model/money_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package model

import (
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base"
)

type moneySource struct {
base.Role
accountId string
balance *balance
phone *phone
}

func newMoneySource(accountId string, balance *balance, phone *phone) *moneySource {
return &moneySource{accountId: accountId, balance: balance, phone: phone}
}

func (this *moneySource) TransferMoneyTo(dst *moneyDestination, amount uint) {
if this.balance.get() < amount {
panic("insufficient money!")
}
dst.transferMoneyFrom(this.accountId, amount)
this.balance.decrease(amount)
this.phone.sendTransferToMsg(dst.getAccountId(), amount)
}

func (this *moneySource) GetAmount() uint {
return this.balance.get()
}
26 changes: 26 additions & 0 deletions transfer-money/domain/model/phone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model

import (
"fmt"
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base"
)

type phone struct {
base.ValueObject
accountId string
phoneNumber string
}

func newPhone(accountId, phoneNumber string) *phone {
return &phone{accountId: accountId, phoneNumber: phoneNumber}
}

func (this *phone) sendTransferToMsg(dstAccountId string, amount uint) {
msg := fmt.Sprintf("%s send %d money to %s", this.accountId, amount, dstAccountId)
GetPhoneProvider().Send(this.phoneNumber, msg)
}

func (this *phone) sendTransferFromMsg(srcAccountId string, amount uint) {
msg := fmt.Sprintf("%s receive %d money from %s", this.accountId, amount, srcAccountId)
GetPhoneProvider().Send(this.phoneNumber, msg)
}
15 changes: 15 additions & 0 deletions transfer-money/domain/model/phone_privder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

type PhoneProvider interface {
Send(phoneNumber, msg string)
}

var providerInst PhoneProvider

func SetPhoneProvider(provider PhoneProvider) {
providerInst = provider
}

func GetPhoneProvider() PhoneProvider {
return providerInst
}
44 changes: 44 additions & 0 deletions transfer-money/domain/service/account_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package service

import (
"errors"
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model"
"sync"
)

type AccountService struct {
repo model.AccountRepo
}

func (this *AccountService) Create(accountId string, amount uint, phoneNumber string) {
account := model.AccountFactory{}.Create(accountId, amount, phoneNumber)
this.repo.Add(account)
}

func (this *AccountService) TransferMoney(srcAccountId, dstAccountId string, amount uint) {
from := this.repo.Get(srcAccountId)
to := this.repo.Get(dstAccountId)
from.MoneySource.TransferMoneyTo(to.MoneyDestination, amount)
}

func (this *AccountService) GetAmount(accountId string) (uint, error) {
if account := this.repo.Get(accountId); account != nil {
return account.MoneySource.GetAmount(), nil
}
return 0, errors.New("accountId is not existed")

}

func (this *AccountService) Destroy(accountId string) {
this.repo.Remove(accountId)
}

var once sync.Once
var serviceInst *AccountService = &AccountService{}

func GetAccountService() *AccountService {
once.Do(func() {
serviceInst.repo = model.GetAccountRepo()
})
return serviceInst
}
18 changes: 18 additions & 0 deletions transfer-money/infra/account_repo_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package infra

import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model"

type AccountRepoImpl struct {
}

func (this *AccountRepoImpl) Add(account *model.Account) {

}

func (this *AccountRepoImpl) Get(accountId string) *model.Account {
return nil
}

func (this *AccountRepoImpl) Remove(accountId string) {

}
8 changes: 8 additions & 0 deletions transfer-money/infra/phone_provider_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package infra

type PhoneProviderImpl struct {
}

func (this PhoneProviderImpl) Send(phoneNumber, msg string) {

}
Loading

0 comments on commit e9cd36c

Please sign in to comment.