123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package api
- import (
- "fmt"
- "mats/db/model"
- "mats/db/repo"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- func CreateDatabaseDesignRouter(router *GinRouter) {
- router.GETJWT("/design/list", DesignList)
- router.POSTJWT("/design/create", DesignCreate)
- router.POSTJWT("/design/update", DesignSave)
- router.POSTJWT("/design/delete/:id", DesignDelete)
- router.GETJWT("/design/detail/:id", DesignDetail)
- router.POSTJWT("/design/save", DesignSave)
- }
- func DesignList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- if query == nil {
- query = map[string]interface{}{}
- }
- query["userId"], _ = primitive.ObjectIDFromHex(apictx.User.ID)
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
- CollectName: repo.CollectionDesigns,
- Page: page,
- Size: size,
- Query: query,
- Project: []string{"name", "thumbnail", "createTime"},
- Sort: bson.M{"createTime": -1},
- })
- }
- func DesignCreate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- body := &struct {
- Name string
- From string
- Id string
- }{}
- err := c.ShouldBindJSON(body)
- if err != nil {
- return nil, NewError("参数解析错误")
- }
- if len(body.Name) < 1 {
- body.Name = "未定义"
- }
-
- d3d := &model.Design3d{
- Name: body.Name,
- CreateTime: time.Now(),
- Products: []*model.ProductHeader{},
- Scenes: []*model.DesignScene{},
- }
- d3d.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
- return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDesigns, d3d)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- func DesignSave(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- body := &model.Design3d{}
- err := c.ShouldBindJSON(body)
- if err != nil {
- fmt.Println(err)
- return nil, NewError("参数解析错误" + err.Error())
- }
- if len(body.Id) < 1 {
- return nil, NewError("ID不能为空")
- }
- id := body.Id.Hex()
- body.Id = primitive.NilObjectID
- body.UserId = primitive.NilObjectID
- body.CreateTime = time.Time{}
- body.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionDesigns, id, body)
- }
- func DesignDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- id := c.Param("id")
- if len(id) < 1 {
- return nil, NewError("参数不能为空")
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionDesigns, id)
- }
- func DesignDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- id := c.Param("id")
- if len(id) < 1 {
- return nil, NewError("参数id不能为空")
- }
- prj := &model.Design3d{}
- ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDesigns, Query: repo.Map{"_id": id}}, prj)
- if err != nil {
- return nil, err
- }
- if !ok {
- return nil, NewError("没有数据!")
- }
- return prj, nil
- }
|