add bookstore example
This commit is contained in:
13
example/bookstore/rpc/add/internal/config/config.go
Executable file
13
example/bookstore/rpc/add/internal/config/config.go
Executable file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/tal-tech/go-zero/core/stores/cache"
|
||||
"github.com/tal-tech/go-zero/rpcx"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rpcx.RpcServerConf
|
||||
DataSource string
|
||||
Table string
|
||||
Cache cache.CacheConf
|
||||
}
|
||||
38
example/bookstore/rpc/add/internal/logic/addlogic.go
Executable file
38
example/bookstore/rpc/add/internal/logic/addlogic.go
Executable file
@@ -0,0 +1,38 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bookstore/rpc/add/internal/svc"
|
||||
add "bookstore/rpc/add/pb"
|
||||
"bookstore/rpc/model"
|
||||
"context"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
|
||||
return &AddLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return &add.AddResp{
|
||||
Ok: true,
|
||||
}, nil
|
||||
}
|
||||
26
example/bookstore/rpc/add/internal/server/adderserver.go
Executable file
26
example/bookstore/rpc/add/internal/server/adderserver.go
Executable file
@@ -0,0 +1,26 @@
|
||||
// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: add.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"bookstore/rpc/add/internal/logic"
|
||||
"bookstore/rpc/add/internal/svc"
|
||||
add "bookstore/rpc/add/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type AdderServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdderServer(svcCtx *svc.ServiceContext) *AdderServer {
|
||||
return &AdderServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AdderServer) Add(ctx context.Context, in *add.AddReq) (*add.AddResp, error) {
|
||||
l := logic.NewAddLogic(ctx, s.svcCtx)
|
||||
return l.Add(in)
|
||||
}
|
||||
20
example/bookstore/rpc/add/internal/svc/servicecontext.go
Executable file
20
example/bookstore/rpc/add/internal/svc/servicecontext.go
Executable file
@@ -0,0 +1,20 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"bookstore/rpc/add/internal/config"
|
||||
"bookstore/rpc/model"
|
||||
|
||||
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user