From c1f10a224a2a45ac9dbfa77eee05e8e0dab4596b Mon Sep 17 00:00:00 2001 From: agiledragon Date: Mon, 22 Jul 2019 22:40:41 +0800 Subject: [PATCH] delete transfer-money --- transfer-money/app/service/account_api.go | 21 ----- transfer-money/domain/model/account.go | 22 ----- .../domain/model/account_factory.go | 10 --- transfer-money/domain/model/account_repo.go | 17 ---- transfer-money/domain/model/balance.go | 24 ----- .../domain/model/base/aggregate_root.go | 9 -- transfer-money/domain/model/base/entity.go | 21 ----- transfer-money/domain/model/base/role.go | 4 - .../domain/model/base/value_object.go | 4 - .../domain/model/money_destination.go | 25 ------ transfer-money/domain/model/money_source.go | 29 ------ transfer-money/domain/model/phone.go | 26 ------ transfer-money/domain/model/phone_privder.go | 15 ---- .../domain/service/account_service.go | 44 --------- transfer-money/infra/account_repo_impl.go | 18 ---- transfer-money/infra/phone_provider_impl.go | 8 -- transfer-money/test/account_test.go | 90 ------------------- 17 files changed, 387 deletions(-) delete mode 100644 transfer-money/app/service/account_api.go delete mode 100644 transfer-money/domain/model/account.go delete mode 100644 transfer-money/domain/model/account_factory.go delete mode 100644 transfer-money/domain/model/account_repo.go delete mode 100644 transfer-money/domain/model/balance.go delete mode 100644 transfer-money/domain/model/base/aggregate_root.go delete mode 100644 transfer-money/domain/model/base/entity.go delete mode 100644 transfer-money/domain/model/base/role.go delete mode 100644 transfer-money/domain/model/base/value_object.go delete mode 100644 transfer-money/domain/model/money_destination.go delete mode 100644 transfer-money/domain/model/money_source.go delete mode 100644 transfer-money/domain/model/phone.go delete mode 100644 transfer-money/domain/model/phone_privder.go delete mode 100644 transfer-money/domain/service/account_service.go delete mode 100644 transfer-money/infra/account_repo_impl.go delete mode 100644 transfer-money/infra/phone_provider_impl.go delete mode 100644 transfer-money/test/account_test.go diff --git a/transfer-money/app/service/account_api.go b/transfer-money/app/service/account_api.go deleted file mode 100644 index 4decd71..0000000 --- a/transfer-money/app/service/account_api.go +++ /dev/null @@ -1,21 +0,0 @@ -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) -} diff --git a/transfer-money/domain/model/account.go b/transfer-money/domain/model/account.go deleted file mode 100644 index 62a5b14..0000000 --- a/transfer-money/domain/model/account.go +++ /dev/null @@ -1,22 +0,0 @@ -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 -} diff --git a/transfer-money/domain/model/account_factory.go b/transfer-money/domain/model/account_factory.go deleted file mode 100644 index 9b88e53..0000000 --- a/transfer-money/domain/model/account_factory.go +++ /dev/null @@ -1,10 +0,0 @@ -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) -} diff --git a/transfer-money/domain/model/account_repo.go b/transfer-money/domain/model/account_repo.go deleted file mode 100644 index a0af1de..0000000 --- a/transfer-money/domain/model/account_repo.go +++ /dev/null @@ -1,17 +0,0 @@ -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 -} diff --git a/transfer-money/domain/model/balance.go b/transfer-money/domain/model/balance.go deleted file mode 100644 index 24a39d4..0000000 --- a/transfer-money/domain/model/balance.go +++ /dev/null @@ -1,24 +0,0 @@ -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 -} diff --git a/transfer-money/domain/model/base/aggregate_root.go b/transfer-money/domain/model/base/aggregate_root.go deleted file mode 100644 index 28cf145..0000000 --- a/transfer-money/domain/model/base/aggregate_root.go +++ /dev/null @@ -1,9 +0,0 @@ -package base - -type AggregateRoot struct { - Entity -} - -func NewAggregateRoot(id string) AggregateRoot { - return AggregateRoot{NewEntity(id)} -} diff --git a/transfer-money/domain/model/base/entity.go b/transfer-money/domain/model/base/entity.go deleted file mode 100644 index ca699f9..0000000 --- a/transfer-money/domain/model/base/entity.go +++ /dev/null @@ -1,21 +0,0 @@ -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) -} diff --git a/transfer-money/domain/model/base/role.go b/transfer-money/domain/model/base/role.go deleted file mode 100644 index 11408a8..0000000 --- a/transfer-money/domain/model/base/role.go +++ /dev/null @@ -1,4 +0,0 @@ -package base - -type Role struct { -} diff --git a/transfer-money/domain/model/base/value_object.go b/transfer-money/domain/model/base/value_object.go deleted file mode 100644 index f7fbcb4..0000000 --- a/transfer-money/domain/model/base/value_object.go +++ /dev/null @@ -1,4 +0,0 @@ -package base - -type ValueObject struct { -} diff --git a/transfer-money/domain/model/money_destination.go b/transfer-money/domain/model/money_destination.go deleted file mode 100644 index aff77a0..0000000 --- a/transfer-money/domain/model/money_destination.go +++ /dev/null @@ -1,25 +0,0 @@ -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) -} diff --git a/transfer-money/domain/model/money_source.go b/transfer-money/domain/model/money_source.go deleted file mode 100644 index 2630a4d..0000000 --- a/transfer-money/domain/model/money_source.go +++ /dev/null @@ -1,29 +0,0 @@ -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() -} diff --git a/transfer-money/domain/model/phone.go b/transfer-money/domain/model/phone.go deleted file mode 100644 index 93e329c..0000000 --- a/transfer-money/domain/model/phone.go +++ /dev/null @@ -1,26 +0,0 @@ -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) -} diff --git a/transfer-money/domain/model/phone_privder.go b/transfer-money/domain/model/phone_privder.go deleted file mode 100644 index 064f928..0000000 --- a/transfer-money/domain/model/phone_privder.go +++ /dev/null @@ -1,15 +0,0 @@ -package model - -type PhoneProvider interface { - Send(phoneNumber, msg string) -} - -var providerInst PhoneProvider - -func SetPhoneProvider(provider PhoneProvider) { - providerInst = provider -} - -func GetPhoneProvider() PhoneProvider { - return providerInst -} diff --git a/transfer-money/domain/service/account_service.go b/transfer-money/domain/service/account_service.go deleted file mode 100644 index 053fd0c..0000000 --- a/transfer-money/domain/service/account_service.go +++ /dev/null @@ -1,44 +0,0 @@ -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 -} diff --git a/transfer-money/infra/account_repo_impl.go b/transfer-money/infra/account_repo_impl.go deleted file mode 100644 index f1e8f52..0000000 --- a/transfer-money/infra/account_repo_impl.go +++ /dev/null @@ -1,18 +0,0 @@ -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) { - -} diff --git a/transfer-money/infra/phone_provider_impl.go b/transfer-money/infra/phone_provider_impl.go deleted file mode 100644 index 3746d45..0000000 --- a/transfer-money/infra/phone_provider_impl.go +++ /dev/null @@ -1,8 +0,0 @@ -package infra - -type PhoneProviderImpl struct { -} - -func (this PhoneProviderImpl) Send(phoneNumber, msg string) { - -} diff --git a/transfer-money/test/account_test.go b/transfer-money/test/account_test.go deleted file mode 100644 index c151080..0000000 --- a/transfer-money/test/account_test.go +++ /dev/null @@ -1,90 +0,0 @@ -package test - -import ( - "fmt" - "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/app/service" - "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model" - . "github.com/smartystreets/goconvey/convey" - "testing" -) - -type SpyPhoneProvider struct { - phoneNumbers []string - msgs []string -} - -func (this *SpyPhoneProvider) Send(phoneNumber, msg string) { - this.phoneNumbers = append(this.phoneNumbers, phoneNumber) - this.msgs = append(this.msgs, msg) -} - -type FakeAccountRepo struct { - accounts map[string]*model.Account -} - -func (a *FakeAccountRepo) Add(account *model.Account) { - a.accounts[account.Id()] = account -} - -func (a *FakeAccountRepo) Get(accountId string) *model.Account { - return a.accounts[accountId] -} - -func (a *FakeAccountRepo) Remove(accountId string) { - delete(a.accounts, accountId) -} - -func TestAccount(t *testing.T) { - provider := &SpyPhoneProvider{ - phoneNumbers: make([]string, 0), - msgs: make([]string, 0), - } - model.SetPhoneProvider(provider) - repo := &FakeAccountRepo{make(map[string]*model.Account)} - model.SetAccountRepo(repo) - - Convey("TestAccount", t, func() { - - Convey("create account", func() { - accountId := "zhangsan123" - initialAmount := uint(1000) - phoneNumber := "13310998098" - service.CreateAccount(accountId, initialAmount, phoneNumber) - actualAmount, err := service.GetAmount(accountId) - So(err, ShouldBeNil) - So(actualAmount, ShouldEqual, initialAmount) - service.DestroyAccount(accountId) - }) - - Convey("transfer money", func() { - srcAccountId := "zhangsan123" - srcInitialAmount := uint(1000) - srcPhoneNumber := "13310998098" - service.CreateAccount(srcAccountId, srcInitialAmount, srcPhoneNumber) - - dstAccountId := "lisi456" - dstInitialAmount := uint(200) - dstPhoneNumber := "19916588070" - service.CreateAccount(dstAccountId, dstInitialAmount, dstPhoneNumber) - - amount := uint(300) - service.TransferMoney(srcAccountId, dstAccountId, amount) - srcActualAmount, err := service.GetAmount(srcAccountId) - So(err, ShouldBeNil) - So(srcActualAmount, ShouldEqual, srcInitialAmount-amount) - dstActualAmount, err := service.GetAmount(dstAccountId) - So(err, ShouldBeNil) - So(dstActualAmount, ShouldEqual, dstInitialAmount+amount) - - srcMsg := fmt.Sprintf("%s send %d money to %s", srcAccountId, amount, dstAccountId) - dstMsg := fmt.Sprintf("%s receive %d money from %s", dstAccountId, amount, srcAccountId) - So(provider.phoneNumbers[0], ShouldEqual, dstPhoneNumber) - So(provider.msgs[0], ShouldEqual, dstMsg) - So(provider.phoneNumbers[1], ShouldEqual, srcPhoneNumber) - So(provider.msgs[1], ShouldEqual, srcMsg) - - service.DestroyAccount(srcAccountId) - service.DestroyAccount(dstAccountId) - }) - }) -}