service-database-design-scene.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package api
  2. import (
  3. "assetcenter/db/model"
  4. "assetcenter/db/repo"
  5. "time"
  6. "github.com/gin-gonic/gin"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. // 注册设计相关接口
  10. func CreateDatabaseDesignSceneRouter(router *GinRouter) {
  11. //项目场景列表管理
  12. CreateArrayCRUD(router, "/design/scene", &ArrayCRUDOption{
  13. Collection: repo.CollectionDesigns,
  14. NeedsUpdate: true,
  15. NeedsRemove: true,
  16. NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  17. prod := model.ProductHeader{}
  18. c.ShouldBindJSON(&prod)
  19. sceneProd := &model.SceneProduct{
  20. Id: prod.Id,
  21. // Name: prod.Name,
  22. // Type: "product",
  23. Locked: false,
  24. Visible: true,
  25. Transform: &model.Transform{
  26. Pos: model.Vect3{0, 0, 0},
  27. Scale: model.Vect3{1, 1, 1},
  28. Rot: model.Vect3{0, 0, 0},
  29. },
  30. }
  31. scene := &model.DesignScene{
  32. Id: primitive.NewObjectID().Hex(),
  33. Name: prod.Name,
  34. Thumbnail: prod.Thumbnail,
  35. CreateTime: time.Now(),
  36. Product: []*model.SceneProduct{sceneProd},
  37. }
  38. return scene, nil
  39. },
  40. EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} {
  41. return &model.DesignScene{}
  42. },
  43. ArrayFieldPath: "scenes",
  44. SearchProject: []string{"name", "id", "thumbnail", "createTime"},
  45. })
  46. router.POSTJWT("/design/create/scene/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  47. projId := c.Param("id")
  48. scene := &model.DesignScene{}
  49. c.ShouldBindJSON(scene)
  50. if len(scene.Name) < 1 {
  51. return nil, NewError("场景名称不能为空!")
  52. }
  53. scene.Id = primitive.NewObjectID().Hex()
  54. scene.CreateTime = time.Now()
  55. scene.Product = []*model.SceneProduct{}
  56. ret, _ := repo.RepoDocArrayAppend(apictx.CreateRepoCtx(), repo.CollectionDesigns, projId, "scenes", scene)
  57. if ret.ModifiedCount == 1 {
  58. return scene, nil
  59. }
  60. return nil, NewError("创建失败!")
  61. })
  62. }