service-meshes.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package api
  2. import (
  3. "mesh/bus"
  4. "mesh/db/model"
  5. "mesh/db/repo"
  6. "time"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. )
  10. func RegQueenMeshes(router *GinRouter) {
  11. CollectionName := "hubmeshes"
  12. CreateCRUD(router, "/hubmeshes", &CRUDOption{
  13. Collection: CollectionName,
  14. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  15. hub := &model.HubMesh{}
  16. c.ShouldBindJSON(hub)
  17. if len(hub.Name) < 1 {
  18. return nil, NewError("参数不合法!")
  19. }
  20. if len(hub.Thumbnail) < 1 {
  21. return nil, NewError("没有模型封面")
  22. }
  23. hub.CreateTime = time.Now()
  24. hub.UserId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
  25. if hub.UserId == primitive.NilObjectID {
  26. return nil, NewError("参数不合法!")
  27. }
  28. hub.DbName = "queenmeshes"
  29. if len(hub.Thumbnail) < 1 {
  30. hub.Thumbnail = "https://gd1.alicdn.com/imgextra/i2/0/O1CN01BXPA5l1H9VvVhEQQ7_!!0-item_pic.jpg_400x400.jpg"
  31. }
  32. ret, err := bus.TreeAddDefineMeshpack("queenmeshes", apictx.User.Parent, hub.Name)
  33. if err != nil {
  34. return nil, err
  35. }
  36. hub.Host = ret.Host
  37. hub.DefineId = ret.DefineId
  38. hub.Collection = ret.Collection
  39. hub.DbId = ret.DbId
  40. return hub, nil
  41. },
  42. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  43. return &model.HubMesh{}
  44. },
  45. SearchFilter: func(_ *gin.Context, apictx *ApiSession, _ map[string]interface{}) map[string]interface{} {
  46. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  47. return map[string]interface{}{"userId": userId}
  48. },
  49. JWT: true,
  50. SearchProject: []string{"name", "thumbnail", "createTime", "host", "dbid", "defineId"},
  51. DetailProject: []string{"name", "thumbnail", "createTime", "host", "dbid", "defineId"},
  52. Remove: func(_ *gin.Context, apictx *ApiSession, id string) (interface{}, error) {
  53. hub := &model.HubMesh{}
  54. err := repo.RepoSeachDoc2(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: CollectionName, Query: repo.Map{"_id": id}}, hub)
  55. if err != nil {
  56. return nil, err
  57. }
  58. err = bus.TreeRemoveDefineMeshpack(hub.DbId, hub.DefineId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. //todo queentree 添加删除状态
  63. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), CollectionName, id)
  64. },
  65. })
  66. }