Skip to content

Commit 5169a0e

Browse files
committed
add external api
1 parent fe992ae commit 5169a0e

File tree

8 files changed

+108
-8
lines changed

8 files changed

+108
-8
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"gopls": {
3-
"build.buildFlags": ["-tags=unit,integration,e2e"]
3+
"build.buildFlags": ["-tags=unit,integration,e2e,wireinject"]
44
},
55
"go.testTags": "-tags=unit,integration,e2e",
66
}

Taskfile.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@ tasks:
6060
--database "mysql://$MYSQL_USER:$MYSQL_PASS@tcp($MYSQL_HOST:$MYSQL_PORT)/$MYSQL_DBNAME"
6161
-verbose force {{.CLI_ARGS}}
6262
desc: 'Execute force migration version.Migration version must be specified as an argument.ex)task migrate:force -- 2'
63+
generate:
64+
cmds:
65+
- go generate ./...
66+
desc: 'execute `go generate` command. generate mock by `gomock` and di by `wire`.'
67+
generate:di:
68+
cmds:
69+
- wire gen
70+
desc: 'execute `wire gen` command.'

controller/gacha.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ import (
44
"context"
55

66
"github.com/JY8752/go-unittest-architecture/domain"
7+
"github.com/JY8752/go-unittest-architecture/infrastructure/api"
78
"github.com/JY8752/go-unittest-architecture/infrastructure/repository"
89
)
910

1011
type Gacha struct {
1112
gachaRep *repository.Gacha
1213
itemRep *repository.Item
1314
seedGenerator domain.SeedGenerator
15+
payment api.Payment
1416
}
1517

16-
func NewGacha(gachaRep *repository.Gacha, itemRep *repository.Item, seedGenerator domain.SeedGenerator) *Gacha {
17-
return &Gacha{gachaRep, itemRep, seedGenerator}
18+
func NewGacha(
19+
gachaRep *repository.Gacha,
20+
itemRep *repository.Item,
21+
seedGenerator domain.SeedGenerator,
22+
payment api.Payment,
23+
) *Gacha {
24+
return &Gacha{gachaRep, itemRep, seedGenerator, payment}
1825
}
1926

2027
func (g *Gacha) Draw(ctx context.Context, gachaId domain.GachaId) (*domain.Item, error) {
@@ -39,7 +46,10 @@ func (g *Gacha) Draw(ctx context.Context, gachaId domain.GachaId) (*domain.Item,
3946
return nil, err
4047
}
4148

42-
// 管理下にないプロセス外依存 決済する
49+
// 管理下にないプロセス外依存 決済する 手抜きでハードコード
50+
if err = g.payment.Buy(100); err != nil {
51+
return nil, err
52+
}
4353

4454
return item, err
4555
}

controller/gacha_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/JY8752/go-unittest-architecture/controller"
1313
"github.com/JY8752/go-unittest-architecture/domain"
1414
"github.com/JY8752/go-unittest-architecture/infrastructure/repository"
15+
mock_api "github.com/JY8752/go-unittest-architecture/mocks/api"
1516
mock_domain "github.com/JY8752/go-unittest-architecture/mocks/domain"
1617
"github.com/JY8752/go-unittest-architecture/test"
1718
_ "github.com/golang-migrate/migrate/v4/source/file"
@@ -50,17 +51,19 @@ func TestDraw(t *testing.T) {
5051
sg := mock_domain.NewMockSeedGenerator(ctrl)
5152
sg.EXPECT().New().Return(int64(1000))
5253

53-
controller := controller.NewGacha(gachaRep, itemRep, sg)
54+
p := mock_api.NewMockPayment(ctrl)
55+
p.EXPECT().Buy(100).Return(nil).Times(1)
56+
57+
controller := controller.NewGacha(gachaRep, itemRep, sg, p)
5458

55-
gachaId := domain.NewGachaId(1)
5659
expected := domain.NewItem(
5760
domain.NewItemId(9),
5861
"item9",
5962
domain.R,
6063
)
6164

6265
// Act
63-
act, err := controller.Draw(context.Background(), gachaId)
66+
act, err := controller.Draw(context.Background(), domain.NewGachaId(1))
6467
if err != nil {
6568
t.Fatal(err)
6669
}

infrastructure/api/payment.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package api
2+
3+
//go:generate mockgen -source=$GOFILE -destination=../../mocks/api/mock_$GOFILE -package=mock_api
4+
5+
import "errors"
6+
7+
type Payment interface {
8+
Buy(int) error
9+
}
10+
11+
type payment struct{}
12+
13+
func NewPayment() Payment {
14+
return &payment{}
15+
}
16+
17+
// dummy実装 本当は外部の決済APIとかを叩く想定
18+
func (p *payment) Buy(price int) error {
19+
if price == 100 {
20+
return nil
21+
}
22+
return errors.New("invalid price")
23+
}

mocks/api/mock_payment.go

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wire.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/JY8752/go-unittest-architecture/controller"
99
"github.com/JY8752/go-unittest-architecture/domain"
10+
"github.com/JY8752/go-unittest-architecture/infrastructure/api"
1011
"github.com/JY8752/go-unittest-architecture/infrastructure/handler"
1112
"github.com/JY8752/go-unittest-architecture/infrastructure/repository"
1213
"github.com/google/wire"
@@ -18,6 +19,7 @@ func InitializeRootHandler(db *sql.DB, e *echo.Echo) *handler.Root {
1819
repository.NewGacha,
1920
repository.NewItem,
2021
domain.NewSeedGenerator,
22+
api.NewPayment,
2123
controller.NewGacha,
2224
handler.NewGacha,
2325
handler.NewRoot,

wire_gen.go

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)