package api import ( "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" "infish.cn/comm" ) func CreateDatabaseRouter(router *GinRouter) { router.POSTJWT("/db/import/defaultdb", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { search := &model.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": "qdb-default"}, Project: []string{"_id"}}, search) if ok { return nil, NewError("默认数据库已存在!") } db := model.CreateDefaultDatabase() return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, db) }) router.POSTJWT("/db/category/save", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { body := &struct { Id string `json:"_id"` Categories []*comm.CategoryNode }{} err := c.ShouldBindJSON(body) if err != nil { return nil, err } search := &model.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"_id": body.Id}, Project: []string{"_id", "categories"}}, search) if ok { //已存在 search.Categories.Categories = body.Categories search.Categories.UpdateTime = time.Now() } else { search.Categories = &comm.DbCategory{ Id: primitive.NewObjectID(), Scope: "default", CreateTime: time.Now(), UpdateTime: time.Now(), Categories: body.Categories, } } return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "database", body.Id, bson.M{"categories": search.Categories}) }) CreateCRUD(router, "/db", &CRUDOption{ Collection: repo.CollectionDatabase, NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) { cat := &model.Database{} c.ShouldBindJSON(cat) if len(cat.Name) < 1 || len(cat.Label) < 1 { return nil, NewError("参数不合法!") } cat.CreateTime = time.Now() cat.Assets = model.CreateDefaultDbAssets() cat.Categories = &comm.DbCategory{} cat.UserId = apictx.User.ID search := &model.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": cat.Name}, Project: []string{"_id"}}, search) if ok { return nil, NewError("数据库编号已存在!") } ok, _ = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"label": cat.Label}, Project: []string{"_id"}}, search) if ok { return nil, NewError("数据库名字已存在!") } cat.Assets = model.CreateDefaultDbAssets() cat.CreateTime = time.Now() return cat, nil }, EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} { return &model.Database{} }, SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} { return map[string]interface{}{"userId": apictx.User.ID} // return map[string]interface{}{} }, JWT: true, SearchProject: []string{"name", "label", "createTime"}, DetailProject: []string{"name", "label", "createTime", "categories", "assetsCategory", "assets"}, Remove: func(c *gin.Context, apictx *ApiSession, id string) (interface{}, error) { // team := &model.ResCategory{} // ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionCategory, Query: repo.Map{"_id": id}, Project: []string{"orderId", "type"}}, team) // if !ok { // return nil, NewError("删除失败!") // } // minOrderId := team.OrderId //后面有几个零 // if minOrderId > 0 { // var maxStep int64 = 1 // currValue := minOrderId // for { // if currValue%10 == 0 { // currValue = currValue / 10 // maxStep = maxStep * 10 // } else { // break // } // } // maxOrderId := minOrderId + maxStep - 1 // return repo.RepoDeleteDocs(apictx.CreateRepoCtx(), repo.CollectionCategory, &bson.M{"orderId": bson.M{"$gte": minOrderId, "$lt": maxOrderId}, "type": team.Type}) // } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, id) }, }) }