diff --git a/internal/biz/greeter.go b/internal/biz/greeter.go new file mode 100644 index 0000000000000000000000000000000000000000..51aeaa68ad3dcc558e7810eabd1a6f7381017e1f --- /dev/null +++ b/internal/biz/greeter.go @@ -0,0 +1,45 @@ +package biz + +import ( + "context" + + v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" + "github.com/go-kratos/kratos/v2/errors" + "github.com/go-kratos/kratos/v2/log" +) + +var ( + // ErrUserNotFound is user not found. + ErrUserNotFound = errors.NotFound(v1.ErrorReason_USER_NOT_FOUND.String(), "user not found") +) + +// Greeter is a Greeter model. +type Greeter struct { + Hello string +} + +// GreeterRepo is a Greater repo. +type GreeterRepo interface { + Save(context.Context, *Greeter) (*Greeter, error) + Update(context.Context, *Greeter) (*Greeter, error) + FindByID(context.Context, int64) (*Greeter, error) + ListByHello(context.Context, string) ([]*Greeter, error) + ListAll(context.Context) ([]*Greeter, error) +} + +// GreeterUsecase is a Greeter usecase. +type GreeterUsecase struct { + repo GreeterRepo + log *log.Helper +} + +// NewGreeterUsecase new a Greeter usecase. +func NewGreeterUsecase(repo GreeterRepo) *GreeterUsecase { + return &GreeterUsecase{repo: repo} +} + +// CreateGreeter creates a Greeter, and returns the new Greeter. +func (uc *GreeterUsecase) CreateGreeter(ctx context.Context, g *Greeter) (*Greeter, error) { + log.Infof("CreateGreeter: %v", g.Hello) + return uc.repo.Save(ctx, g) +} diff --git a/internal/data/greeter.go b/internal/data/greeter.go new file mode 100644 index 0000000000000000000000000000000000000000..7af3138e507fbc9d74b58a524d10c3a760d0e6b5 --- /dev/null +++ b/internal/data/greeter.go @@ -0,0 +1,38 @@ +package data + +import ( + "context" + + "github.com/go-kratos/kratos-layout/internal/biz" +) + +type greeterRepo struct { + data *Data +} + +// NewGreeterRepo . +func NewGreeterRepo(data *Data) biz.GreeterRepo { + return &greeterRepo{ + data: data, + } +} + +func (r *greeterRepo) Save(ctx context.Context, g *biz.Greeter) (*biz.Greeter, error) { + return g, nil +} + +func (r *greeterRepo) Update(ctx context.Context, g *biz.Greeter) (*biz.Greeter, error) { + return g, nil +} + +func (r *greeterRepo) FindByID(context.Context, int64) (*biz.Greeter, error) { + return nil, nil +} + +func (r *greeterRepo) ListByHello(context.Context, string) ([]*biz.Greeter, error) { + return nil, nil +} + +func (r *greeterRepo) ListAll(context.Context) ([]*biz.Greeter, error) { + return nil, nil +} diff --git a/internal/service/greeter.go b/internal/service/greeter.go new file mode 100644 index 0000000000000000000000000000000000000000..a05ee671409ad7d43ef290c0f6350796b58fb422 --- /dev/null +++ b/internal/service/greeter.go @@ -0,0 +1,29 @@ +package service + +import ( + "context" + + v1 "github.com/go-kratos/kratos-layout/api/helloworld/v1" + "github.com/go-kratos/kratos-layout/internal/biz" +) + +// GreeterService is a greeter service. +type GreeterService struct { + v1.UnimplementedGreeterServer + + uc *biz.GreeterUsecase +} + +// NewGreeterService new a greeter service. +func NewGreeterService(uc *biz.GreeterUsecase) *GreeterService { + return &GreeterService{uc: uc} +} + +// SayHello implements helloworld.GreeterServer. +func (s *GreeterService) SayHello(ctx context.Context, in *v1.HelloRequest) (*v1.HelloReply, error) { + g, err := s.uc.CreateGreeter(ctx, &biz.Greeter{Hello: in.Name}) + if err != nil { + return nil, err + } + return &v1.HelloReply{Message: "Hello " + g.Hello}, nil +}