package server import ( "context" "github.com/spf13/cast" "gomicro-base/api/demo" "gomicro-base/service/demo/internal/model" "gomicro-base/service/demo/internal/service" "google.golang.org/protobuf/types/known/timestamppb" ) // 检查接口实现 var _ demo.DemoHandler = &handler{} // handler类似控制器 type handler struct { svc *service.DemoService } func (h *handler) GetDemo(ctx context.Context, req *demo.DemoReq, resp *demo.DemoResp) error { getDemo, err := h.svc.GetDemo(ctx, req) if err != nil { return err } *resp = h.convertUserModel(getDemo) return nil } func (h *handler) ListDemo(ctx context.Context, req *demo.ListDemoReq, resp *demo.ListDemoResp) error { if req.Limit <= 0 { req.Limit = 15 } listDemo, err := h.svc.ListDemo(ctx, req) if err != nil { return err } list := make([]*demo.DemoResp, 0) for _, v := range listDemo { c := h.convertUserModel(v) list = append(list, &c) } resp.List = list resp.Limit = req.Limit return nil } func (h *handler) convertUserModel(users *model.User) (resp demo.DemoResp) { return demo.DemoResp{ Id: cast.ToString(users.Id), Username: users.Username, Email: users.Email, Avatar: users.Avatar, Status: int32(users.Status), CreatedAt: timestamppb.New(users.CreatedAt), UpdatedAt: timestamppb.New(users.UpdatedAt), } }