api.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package bus
  2. import (
  3. "context"
  4. "fmt"
  5. "mesh/db"
  6. "mesh/db/model"
  7. "mesh/db/repo"
  8. "github.com/nats-io/nats.go"
  9. "infish.cn/comm"
  10. )
  11. type ReqDetail struct {
  12. Id string
  13. }
  14. func RegLibDetail() *comm.NatsMsgReplyer {
  15. return &comm.NatsMsgReplyer{
  16. Subject: "meshlib.item.detail",
  17. Entity: func() interface{} { return &ReqDetail{} },
  18. Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) {
  19. req, ok := entity.(*ReqDetail)
  20. if !ok {
  21. return nil, fmt.Errorf("参数错误不是ReqDetail类型")
  22. }
  23. if len(req.Id) < 1 {
  24. return nil, fmt.Errorf("Id不能为空")
  25. }
  26. var CreateRepoCtx = func() *repo.RepoSession {
  27. return &repo.RepoSession{
  28. Ctx: context.Background(),
  29. Client: db.MongoClient,
  30. }
  31. }
  32. out := &model.HubMesh{}
  33. ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionHubMeshes, Query: repo.Map{"_id": req.Id}}, out)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if !ok {
  38. return nil, fmt.Errorf("查询的模型库不存在")
  39. }
  40. return out, nil
  41. },
  42. }
  43. }