From 8bfe495a264027a4a0168c1c6b2255de56fc9e96 Mon Sep 17 00:00:00 2001 From: agiledragon Date: Sun, 1 Jul 2018 14:37:19 +0800 Subject: [PATCH] destroy cargo --- cargo/app/service/cargo_api.go | 12 ++++++++---- cargo/domain/model/base/entity.go | 8 ++++---- cargo/domain/service/cargo_service.go | 8 ++++++-- cargo/test/cargo_test.go | 5 ++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cargo/app/service/cargo_api.go b/cargo/app/service/cargo_api.go index 624ab7b..94c4111 100644 --- a/cargo/app/service/cargo_api.go +++ b/cargo/app/service/cargo_api.go @@ -5,13 +5,17 @@ import ( ) func CreateCargo(cargoId string, afterDays uint) { - service.GetCargoServiceInstance().Create(cargoId, afterDays) + service.GetCargoService().Create(cargoId, afterDays) } func DelayCargo(cargoId string, days uint) { - service.GetCargoServiceInstance().Delay(cargoId, days) + service.GetCargoService().Delay(cargoId, days) } func GetCargoAfterDays(cargoId string) uint { - return service.GetCargoServiceInstance().GetAfterDays(cargoId) -} \ No newline at end of file + return service.GetCargoService().GetAfterDays(cargoId) +} + +func DestroyCargo(cargoId string) { + service.GetCargoService().DestroyCargo(cargoId) +} diff --git a/cargo/domain/model/base/entity.go b/cargo/domain/model/base/entity.go index 183a0de..c895a6c 100644 --- a/cargo/domain/model/base/entity.go +++ b/cargo/domain/model/base/entity.go @@ -1,19 +1,19 @@ 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 + 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 { diff --git a/cargo/domain/service/cargo_service.go b/cargo/domain/service/cargo_service.go index 78e75c6..f5a63fe 100644 --- a/cargo/domain/service/cargo_service.go +++ b/cargo/domain/service/cargo_service.go @@ -12,7 +12,7 @@ type CargoService struct { var cs = &CargoService{} var once sync.Once -func GetCargoServiceInstance() *CargoService { +func GetCargoService() *CargoService { once.Do(func() { cs.repo = model.GetCargoRepo() cs.provider = model.GetCargoProvider() @@ -42,4 +42,8 @@ func (t *CargoService) GetAfterDays(cargoId string) uint { panic("not found cargo by cargoId") } return cargo.GetAfterDays() -} \ No newline at end of file +} + +func (t *CargoService) DestroyCargo(cargoId string) { + t.repo.Remove(cargoId) +} diff --git a/cargo/test/cargo_test.go b/cargo/test/cargo_test.go index 36f834b..4ec9e13 100644 --- a/cargo/test/cargo_test.go +++ b/cargo/test/cargo_test.go @@ -5,6 +5,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "github.com/agiledragon/ddd-sample-in-golang/cargo/domain/model" "github.com/agiledragon/ddd-sample-in-golang/cargo/app/service" + "fmt" ) type SpyCargoProvider struct { @@ -45,12 +46,14 @@ func TestCargo(t *testing.T) { const cargoId = "1" Convey("TestCargo", t, func() { + Convey("create cargo", func() { const afterDays = 10 service.CreateCargo(cargoId, afterDays) So(provider.cargoId, ShouldEqual, cargoId) So(provider.afterDays, ShouldEqual, afterDays) So(service.GetCargoAfterDays(cargoId), ShouldEqual, afterDays) + service.DestroyCargo(cargoId) }) Convey("delay cargo", func() { @@ -61,7 +64,7 @@ func TestCargo(t *testing.T) { So(provider.cargoId, ShouldEqual, cargoId) So(provider.afterDays, ShouldEqual, afterDays + days) So(service.GetCargoAfterDays(cargoId), ShouldEqual, afterDays + days) - + service.DestroyCargo(cargoId) }) }) }