Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
agiledragon committed Jun 3, 2018
0 parents commit cb4ec30
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# temp
tmp/
output/
build/

# mac
.DS_Store

# python
*.pyc

# clion
.idea/
*.xml

# eclipse
.settings/
.project
.cproject
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ddd-sample-in-golang

ddd sample
17 changes: 17 additions & 0 deletions app/service/cargo_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service

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

func CreateCargo(cargoId string, afterDays uint) {
service.GetCargoServiceInstance().Create(cargoId, afterDays)
}

func DelayCargo(cargoId string, days uint) {
service.GetCargoServiceInstance().Delay(cargoId, days)
}

func GetCargoAfterDays(cargoId string) uint {
return service.GetCargoServiceInstance().GetAfterDays(cargoId)
}
9 changes: 9 additions & 0 deletions 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 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) GetId() 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)
}
5 changes: 5 additions & 0 deletions domain/model/base/value_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package base

type ValueObject struct {

}
23 changes: 23 additions & 0 deletions domain/model/cargo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package model

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

type Cargo struct {
base.AggregateRoot
Delivery Delivery
}

func newCargo(cargoId string, delivery Delivery) *Cargo {
return &Cargo{AggregateRoot: base.NewAggregateRoot(cargoId), Delivery: delivery}
}

func (t *Cargo) Delay(days uint) {
afterDays := t.GetAfterDays() + days
t.Delivery = Delivery{AfterDays: afterDays}
}

func (t *Cargo) GetAfterDays() uint {
return t.Delivery.AfterDays
}
10 changes: 10 additions & 0 deletions domain/model/cargo_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package model

type CargoFactory struct {

}

func (t CargoFactory) Create(cargoId string, afterDays uint) *Cargo {
delivery := Delivery{AfterDays: afterDays}
return newCargo(cargoId, delivery)
}
15 changes: 15 additions & 0 deletions domain/model/cargo_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

type CargoProvider interface {
Confirm(cargo *Cargo)
}

var p CargoProvider = nil

func SetCargoProvider(provider CargoProvider) {
p = provider
}

func GetCargoProvider() CargoProvider {
return p
}
18 changes: 18 additions & 0 deletions domain/model/cargo_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package model

type CargoRepo interface {
Add(cargo *Cargo)
Get(cargoId string) *Cargo
Update(cargo *Cargo)
Remove(cargoId string)
}

var r CargoRepo = nil

func SetCargoRepo(repo CargoRepo) {
r = repo
}

func GetCargoRepo() CargoRepo {
return r
}
11 changes: 11 additions & 0 deletions domain/model/delivery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

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

type Delivery struct {
base.ValueObject
AfterDays uint
}

44 changes: 44 additions & 0 deletions domain/service/cargo_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package service

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

type CargoService struct {
repo model.CargoRepo
provider model.CargoProvider
}

var cs = &CargoService{}
var once sync.Once
func GetCargoServiceInstance() *CargoService {
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)
}

func (t *CargoService) Delay(cargoId string, days uint) {
cargo := t.repo.Get(cargoId)
if cargo != nil {
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 {
return cargo.GetAfterDays()
}
panic("invalid cargoId")
}
68 changes: 68 additions & 0 deletions ft/cargo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ft

import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/agiledragon/ddd-sample-in-golang/domain/model"
"github.com/agiledragon/ddd-sample-in-golang/app/service"
)

type SpyCargoProvider struct {
cargoId string
afterDays uint
}

func (t *SpyCargoProvider) Confirm(cargo *model.Cargo) {
t.cargoId = cargo.GetId()
t.afterDays = cargo.GetAfterDays()
}

type FakeCargoRepo struct {
cargoes map[string]*model.Cargo
}

func (t *FakeCargoRepo) Add(cargo *model.Cargo) {
t.cargoes[cargo.GetId()] = cargo
}

func (t *FakeCargoRepo) Get(cargoId string) *model.Cargo {
return t.cargoes[cargoId]
}

func (t *FakeCargoRepo) Update(cargo *model.Cargo) {
t.cargoes[cargo.GetId()] = cargo
}

func (t *FakeCargoRepo) Remove(cargoId string) {
delete(t.cargoes, cargoId)
}

func TestCargo(t *testing.T) {
provider := &SpyCargoProvider{}
model.SetCargoProvider(provider)
repo := &FakeCargoRepo{make(map[string]*model.Cargo)}
model.SetCargoRepo(repo)
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)
})

Convey("delay cargo", func() {
const afterDays = 20
const days = 5
service.CreateCargo(cargoId, afterDays)
service.DelayCargo(cargoId, days)
So(provider.cargoId, ShouldEqual, cargoId)
So(provider.afterDays, ShouldEqual, afterDays + days)
So(service.GetCargoAfterDays(cargoId), ShouldEqual, afterDays + days)

})
})
}

11 changes: 11 additions & 0 deletions infra/cargo_provider_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package infra

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

type CargoProviderImpl struct {

}

func (t *CargoProviderImpl) Confirm(cargo *model.Cargo) {

}
23 changes: 23 additions & 0 deletions infra/cargo_repo_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package infra

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

type CargoRepoImpl struct {

}

func (t *CargoRepoImpl) Add(cargo *model.Cargo) {

}

func (t *CargoRepoImpl) Get(cargoId string) *model.Cargo {
return nil
}

func (t *CargoRepoImpl) Update(cargo *model.Cargo) {

}

func (t *CargoRepoImpl) Remove(CargoId string) {

}

0 comments on commit cb4ec30

Please sign in to comment.