forked from agiledragon/ddd-sample-in-golang
-
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
1 parent
a3a1ee2
commit f99758f
Showing
13 changed files
with
106 additions
and
111 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/service" | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/service" | ||
) | ||
|
||
func CreateCargo(cargoId string, afterDays uint) { | ||
service.GetCargoService().Create(cargoId, afterDays) | ||
service.GetCargoService().Create(cargoId, afterDays) | ||
} | ||
|
||
func DelayCargo(cargoId string, days uint) { | ||
service.GetCargoService().Delay(cargoId, days) | ||
service.GetCargoService().Delay(cargoId, days) | ||
} | ||
|
||
func GetCargoAfterDays(cargoId string) uint { | ||
return service.GetCargoService().GetAfterDays(cargoId) | ||
return service.GetCargoService().GetAfterDays(cargoId) | ||
} | ||
|
||
func DestroyCargo(cargoId string) { | ||
service.GetCargoService().DestroyCargo(cargoId) | ||
service.GetCargoService().DestroyCargo(cargoId) | ||
} |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package base | ||
|
||
type AggregateRoot struct { | ||
Entity | ||
Entity | ||
} | ||
|
||
func NewAggregateRoot(id string) AggregateRoot { | ||
return AggregateRoot{NewEntity(id)} | ||
} | ||
return AggregateRoot{NewEntity(id)} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package base | ||
|
||
type Entity struct { | ||
id string | ||
id string | ||
} | ||
|
||
func NewEntity(id string) Entity { | ||
return Entity{id: id} | ||
return Entity{id: id} | ||
} | ||
|
||
func (t *Entity) GetId() string { | ||
return t.id | ||
func (t *Entity) Id() string { | ||
return t.id | ||
} | ||
|
||
func (t *Entity) Equal(other *Entity) bool { | ||
return t.id == other.id | ||
return t.id == other.id | ||
} | ||
|
||
func (t *Entity) NotEqual(other *Entity) bool { | ||
return !t.Equal(other) | ||
} | ||
return !t.Equal(other) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
package base | ||
|
||
type ValueObject struct { | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model/base" | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model/base" | ||
) | ||
|
||
type Cargo struct { | ||
base.AggregateRoot | ||
Delivery Delivery | ||
base.AggregateRoot | ||
Delivery Delivery | ||
} | ||
|
||
func newCargo(cargoId string, delivery Delivery) *Cargo { | ||
return &Cargo{AggregateRoot: base.NewAggregateRoot(cargoId), Delivery: delivery} | ||
return &Cargo{AggregateRoot: base.NewAggregateRoot(cargoId), Delivery: delivery} | ||
} | ||
|
||
func (t *Cargo) Delay(days uint) { | ||
afterDays := t.GetAfterDays() + days | ||
t.Delivery = Delivery{AfterDays: afterDays} | ||
afterDays := t.GetAfterDays() + days | ||
t.Delivery = Delivery{AfterDays: afterDays} | ||
} | ||
|
||
func (t *Cargo) GetAfterDays() uint { | ||
return t.Delivery.AfterDays | ||
} | ||
return t.Delivery.AfterDays | ||
} |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package model | ||
|
||
type CargoFactory struct { | ||
|
||
} | ||
|
||
func (t CargoFactory) Create(cargoId string, afterDays uint) *Cargo { | ||
delivery := Delivery{AfterDays: afterDays} | ||
return newCargo(cargoId, delivery) | ||
} | ||
delivery := Delivery{AfterDays: afterDays} | ||
return newCargo(cargoId, delivery) | ||
} |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package model | ||
|
||
type CargoProvider interface { | ||
Confirm(cargo *Cargo) | ||
Confirm(cargo *Cargo) | ||
} | ||
|
||
var p CargoProvider = nil | ||
|
||
func SetCargoProvider(provider CargoProvider) { | ||
p = provider | ||
p = provider | ||
} | ||
|
||
func GetCargoProvider() CargoProvider { | ||
return p | ||
return p | ||
} |
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package model | ||
|
||
type CargoRepo interface { | ||
Add(cargo *Cargo) | ||
Get(cargoId string) *Cargo | ||
Update(cargo *Cargo) | ||
Remove(cargoId string) | ||
Add(cargo *Cargo) | ||
Get(cargoId string) *Cargo | ||
Update(cargo *Cargo) | ||
Remove(cargoId string) | ||
} | ||
|
||
var r CargoRepo = nil | ||
|
||
func SetCargoRepo(repo CargoRepo) { | ||
r = repo | ||
r = repo | ||
} | ||
|
||
func GetCargoRepo() CargoRepo { | ||
return r | ||
return r | ||
} |
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model/base" | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model/base" | ||
) | ||
|
||
type Delivery struct { | ||
base.ValueObject | ||
AfterDays uint | ||
base.ValueObject | ||
AfterDays uint | ||
} | ||
|
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 |
---|---|---|
@@ -1,49 +1,50 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model" | ||
"sync" | ||
"github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model" | ||
"sync" | ||
) | ||
|
||
type CargoService struct { | ||
repo model.CargoRepo | ||
provider model.CargoProvider | ||
repo model.CargoRepo | ||
provider model.CargoProvider | ||
} | ||
|
||
var cs = &CargoService{} | ||
var once sync.Once | ||
|
||
func GetCargoService() *CargoService { | ||
once.Do(func() { | ||
cs.repo = model.GetCargoRepo() | ||
cs.provider = model.GetCargoProvider() | ||
}) | ||
return cs | ||
once.Do(func() { | ||
cs.repo = model.GetCargoRepo() | ||
cs.provider = model.GetCargoProvider() | ||
}) | ||
return cs | ||
} | ||
|
||
func (t *CargoService) Create(cargoId string, afterDays uint) { | ||
cargo := model.CargoFactory{}.Create(cargoId, afterDays) | ||
t.repo.Add(cargo) | ||
t.provider.Confirm(cargo) | ||
cargo := model.CargoFactory{}.Create(cargoId, afterDays) | ||
t.repo.Add(cargo) | ||
t.provider.Confirm(cargo) | ||
} | ||
|
||
func (t *CargoService) Delay(cargoId string, days uint) { | ||
cargo := t.repo.Get(cargoId) | ||
if cargo == nil { | ||
panic("not found cargo by cargoId") | ||
} | ||
cargo.Delay(days) | ||
t.repo.Update(cargo) | ||
t.provider.Confirm(cargo) | ||
cargo := t.repo.Get(cargoId) | ||
if cargo == nil { | ||
panic("not found cargo by cargoId") | ||
} | ||
cargo.Delay(days) | ||
t.repo.Update(cargo) | ||
t.provider.Confirm(cargo) | ||
} | ||
|
||
func (t *CargoService) GetAfterDays(cargoId string) uint { | ||
cargo := t.repo.Get(cargoId) | ||
if cargo == nil { | ||
panic("not found cargo by cargoId") | ||
} | ||
return cargo.GetAfterDays() | ||
cargo := t.repo.Get(cargoId) | ||
if cargo == nil { | ||
panic("not found cargo by cargoId") | ||
} | ||
return cargo.GetAfterDays() | ||
} | ||
|
||
func (t *CargoService) DestroyCargo(cargoId string) { | ||
t.repo.Remove(cargoId) | ||
t.repo.Remove(cargoId) | ||
} |
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
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
Oops, something went wrong.