123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package api
- import (
- "mesh/db/repo"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "infish.cn/comm"
- )
- func CreateDatabaseCategoryRouter(router *GinRouter) {
-
- router.POSTJWT("/user/category/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- scope := c.Param("scope")
- if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
- return nil, NewError("Id参数非法!")
- }
- body := &comm.DbCategory{}
- c.ShouldBindJSON(body)
- body.UserId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
- body.Scope = scope
- body.CreateTime = time.Now()
- if body.Id.IsZero() {
- body.CreateTime = time.Now()
- } else {
- body.UpdateTime = time.Now()
- }
- curr := &comm.DbCategory{}
- ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionCategories,
- Project: []string{"_id"},
- Query: repo.Map{"userId": body.UserId, "scope": body.Scope},
- }, curr)
- if err != nil {
- return nil, err
- }
- if !ok {
- id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, body)
- if err != nil {
- return nil, err
- }
- body.Id, _ = primitive.ObjectIDFromHex(id)
- return body, nil
- }
- body.Id = primitive.NilObjectID
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategories, curr.Id.Hex(), body)
- })
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- router.GETJWT("/user/category/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- scope := c.Param("scope")
- if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
- return nil, NewError("Id参数非法!")
- }
- uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionCategories,
- Query: repo.Map{"userId": uidObj, "scope": scope},
- })
- if !ok {
- return nil, NewError("no find!")
- }
- return ret, nil
- })
-
- router.POSTJWT("/user/assetcategory/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- scope := c.Param("scope")
- if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
- return nil, NewError("Id参数非法!")
- }
- body := &comm.DbAssetUserCategory{}
- c.ShouldBindJSON(body)
- body.UserId, _ = primitive.ObjectIDFromHex(apictx.User.Parent)
- body.Scope = scope
- body.CreateTime = time.Now()
- curr := &comm.DbAssetUserCategory{}
- ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionDbAssetCategory,
- Project: []string{"_id"},
- Query: repo.Map{"userId": body.UserId, "scope": body.Scope},
- }, curr)
- if err != nil {
- return nil, err
- }
- if !ok {
- body.CreateTime = time.Now()
- id, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDbAssetCategory, body)
- if err != nil {
- return nil, err
- }
- body.Id, _ = primitive.ObjectIDFromHex(id)
- return body, nil
- }
- body.Id = primitive.NilObjectID
- body.UpdateTime = time.Now()
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionDbAssetCategory, curr.Id.Hex(), body)
- })
- router.GETJWT("/user/assetcategory/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- scope := c.Param("scope")
- if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
- return nil, NewError("Id参数非法!")
- }
- uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionDbAssetCategory,
- Query: repo.Map{"userId": uidObj, "scope": scope},
- })
- if !ok {
- return nil, NewError("no find!")
- }
- return ret, nil
- })
- router.GETJWT("/user/categoryconf/:scope", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- scope := c.Param("scope")
- if len(scope) < 1 || !primitive.IsValidObjectID(apictx.User.Parent) {
- return nil, NewError("Id参数非法!")
- }
- uidObj, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
- _, ret1 := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionCategories,
- Query: repo.Map{"userId": uidObj, "scope": scope},
- })
- _, ret2 := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionDbAssetCategory,
- Query: repo.Map{"userId": uidObj, "scope": scope},
- })
- return map[string]interface{}{
- "categories": ret1,
- "assetCategories": ret2,
- }, nil
- })
- router.GET("/public/category/:cateId/:assetCateConfId", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- cateId := c.Param("cateId")
- assetCateConfId := c.Param("assetCateConfId")
- if !primitive.IsValidObjectID(cateId) || !primitive.IsValidObjectID(assetCateConfId) {
- return nil, NewError("Id参数非法!")
- }
- cateIdObj, _ := primitive.ObjectIDFromHex(cateId)
- ok, ret := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionCategories,
- Query: repo.Map{"_id": cateIdObj},
- })
- if !ok {
- return nil, NewError("no find!")
- }
- assetCateIdObj, _ := primitive.ObjectIDFromHex(assetCateConfId)
- _, assetConf := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionDbAssetCategory,
- Query: repo.Map{"_id": assetCateIdObj},
- })
- return map[string]interface{}{
- "categories": ret,
- "assetCategories": assetConf,
- }, nil
- })
- }
|