Skip to content

Commit

Permalink
fix bookstore example
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Sep 3, 2020
1 parent 81a9ada commit a74d73f
Showing 1 changed file with 89 additions and 89 deletions.
178 changes: 89 additions & 89 deletions doc/bookstore.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@

```go
type (
addReq struct {
book string `form:"book"`
price int `form:"price"`
}
addReq struct {
book string `form:"book"`
price int64 `form:"price"`
}

addResp struct {
ok bool `json:"ok"`
}
addResp struct {
ok bool `json:"ok"`
}
)

type (
checkReq struct {
book string `form:"book"`
}
checkReq struct {
book string `form:"book"`
}

checkResp struct {
found bool `json:"found"`
price int `json:"price"`
}
checkResp struct {
found bool `json:"found"`
price int64 `json:"price"`
}
)

service bookstore-api {
Expand Down Expand Up @@ -270,24 +270,24 @@

```go
type Config struct {
rest.RestConf
Add rpcx.RpcClientConf // 手动代码
rest.RestConf
Add rpcx.RpcClientConf // 手动代码
}
```

* 修改`internal/svc/servicecontext.go`,如下:

```go
type ServiceContext struct {
Config config.Config
Adder rpcx.Client // 手动代码
Config config.Config
Adder adder.Adder // 手动代码
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Adder: rpcx.MustNewClient(c.Add), // 手动代码
}
return &ServiceContext{
Config: c,
Adder: adder.NewAdder(rpcx.MustNewClient(c.Add)), // 手动代码
}
}
```

Expand All @@ -297,19 +297,19 @@

```go
func (l *AddLogic) Add(req types.AddReq) (*types.AddResp, error) {
// 手动代码开始
resp, err := adder.NewAdder(l.svcCtx.Adder).Add(l.ctx, &adder.AddReq{
Book: req.Book,
Price: req.Price,
})
if err != nil {
return nil, err
}
// 手动代码开始
resp, err := l.svcCtx.Adder.Add(l.ctx, &adder.AddReq{
Book: req.Book,
Price: req.Price,
})
if err != nil {
return nil, err
}
return &types.AddResp{
Ok: resp.Ok,
}, nil
// 手动代码结束
return &types.AddResp{
Ok: resp.Ok,
}, nil
// 手动代码结束
}
```

Expand Down Expand Up @@ -404,27 +404,27 @@

```go
type Config struct {
rest.RestConf
Add rpcx.RpcClientConf // 手动代码
Check rpcx.RpcClientConf // 手动代码
rest.RestConf
Add rpcx.RpcClientConf // 手动代码
Check rpcx.RpcClientConf // 手动代码
}
```

* 修改`internal/svc/servicecontext.go`,如下:

```go
type ServiceContext struct {
Config config.Config
Adder rpcx.Client // 手动代码
Checker rpcx.Client // 手动代码
Config config.Config
Adder adder.Adder // 手动代码
Checker checker.Checker // 手动代码
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Adder: rpcx.MustNewClient(c.Add), // 手动代码
Checker: rpcx.MustNewClient(c.Check), // 手动代码
}
return &ServiceContext{
Config: c,
Adder: adder.NewAdder(rpcx.MustNewClient(c.Add)), // 手动代码
Checker: checker.NewChecker(rpcx.MustNewClient(c.Check)), // 手动代码
}
}
```

Expand All @@ -434,19 +434,19 @@

```go
func (l *CheckLogic) Check(req types.CheckReq) (*types.CheckResp, error) {
// 手动代码开始
resp, err := checker.NewChecker(l.svcCtx.Checker).Check(l.ctx, &checker.CheckReq{
Book: req.Book,
})
if err != nil {
return nil, err
}
// 手动代码开始
resp, err := l.svcCtx.Checker.Check(l.ctx, &checker.CheckReq{
Book: req.Book,
})
if err != nil {
return nil, err
}
return &types.CheckResp{
Found: resp.Found,
Price: resp.Price,
}, nil
// 手动代码结束
return &types.CheckResp{
Found: resp.Found,
Price: resp.Price,
}, nil
// 手动代码结束
}
```

Expand Down Expand Up @@ -511,10 +511,10 @@

```go
type Config struct {
rpcx.RpcServerConf
DataSource string // 手动代码
Table string // 手动代码
Cache cache.CacheConf // 手动代码
rpcx.RpcServerConf
DataSource string // 手动代码
Table string // 手动代码
Cache cache.CacheConf // 手动代码
}
```

Expand All @@ -524,53 +524,53 @@

```go
type ServiceContext struct {
c config.Config
Model *model.BookModel // 手动代码
c config.Config
Model *model.BookModel // 手动代码
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
c: c,
Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), // 手动代码
}
return &ServiceContext{
c: c,
Model: model.NewBookModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), // 手动代码
}
}
```

* 修改`rpc/add/internal/logic/addlogic.go`,如下:

```go
func (l *AddLogic) Add(in *add.AddReq) (*add.AddResp, error) {
// 手动代码开始
_, err := l.svcCtx.Model.Insert(model.Book{
Book: in.Book,
Price: in.Price,
})
if err != nil {
return nil, err
}
// 手动代码开始
_, err := l.svcCtx.Model.Insert(model.Book{
Book: in.Book,
Price: in.Price,
})
if err != nil {
return nil, err
}
return &add.AddResp{
Ok: true,
}, nil
// 手动代码结束
return &add.AddResp{
Ok: true,
}, nil
// 手动代码结束
}
```

* 修改`rpc/check/internal/logic/checklogic.go`,如下:

```go
func (l *CheckLogic) Check(in *check.CheckReq) (*check.CheckResp, error) {
// 手动代码开始
resp, err := l.svcCtx.Model.FindOne(in.Book)
if err != nil {
return nil, err
}
// 手动代码开始
resp, err := l.svcCtx.Model.FindOne(in.Book)
if err != nil {
return nil, err
}
return &check.CheckResp{
Found: true,
Price: resp.Price,
}, nil
// 手动代码结束
return &check.CheckResp{
Found: true,
Price: resp.Price,
}, nil
// 手动代码结束
}
```

Expand Down

0 comments on commit a74d73f

Please sign in to comment.