sunsheng 1 year ago
parent
commit
eda837ab38
4 changed files with 57 additions and 55 deletions
  1. 55 5
      src/api/category.go
  2. 0 50
      src/api/learnLog.go
  3. 1 0
      src/api/router.go
  4. 1 0
      src/db/model/category.go

+ 55 - 5
src/api/category.go

@@ -57,13 +57,13 @@ func DeleteCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 }
 
 func CategoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	page, size, query := UtilQueryPageSize(c)
+	_, _, query := UtilQueryPageSize(c)
 	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionCategory,
-		Page:        page,
-		Size:        size,
-		Query:       query,
-		Sort:        bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
+		// Page:        page,
+		// Size:        size,
+		Query: query,
+		Sort:  bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
 	})
 }
 
@@ -87,3 +87,53 @@ func UpdateCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	}
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategory, cate.Id.Hex(), &cate)
 }
+
+// model模块下
+type Category struct {
+	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
+	Pid        string             `bson:"pid,omitempty" json:"pid"` // 分类的上层id // 默认为top
+	Name       string             `bson:"name,omitempty" json:"name"`
+	Sort       *int               `bson:"sort,omitempty" json:"sort"`   // 排序,使用创建时间联合排序。排序按升序排,默认为0
+	Type       string             `bson:"type,omitempty" json:"type"`   // 分类类型,系列/课程/章节/知识点
+	Scope      string             `bson:"scope,omitempty" json:"scope"` // 分类所属的数据库 模块唯一
+	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+	Children   []*Category        `bson:"children,omitempty" json:"children"` // 存储树桩结构
+}
+
+func CategoryParseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	// 查询所有数据
+	cates := []*model.Category{}
+	err := repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionCategory,
+		Query:       repo.Map{},
+		Sort:        bson.M{"sort": -1, "createTime": 1}, // sort降序排列:大的值排在前面,默认为0 || createTime升序排列:小的值排在前面
+	}, cates)
+	if err != nil {
+		return nil, err
+	}
+
+	// 创建一个映射来存储所有的分类
+	cateMap := make(map[string]*model.Category)
+	for _, cate := range cates {
+		cateMap[cate.Id.Hex()] = cate
+	}
+
+	// 结构化处理:返回树状结构
+	for _, cate := range cates {
+		if parent, ok := cateMap[cate.Pid]; ok {
+			parent.Children = append(parent.Children, cate)
+		}
+	}
+
+	// 从映射中获取顶层分类
+	topCates := []*model.Category{}
+	for _, cate := range cateMap {
+		if cate.Pid == "top" {
+			topCates = append(topCates, cate)
+		}
+	}
+
+	return topCates, nil
+
+}

+ 0 - 50
src/api/learnLog.go

@@ -44,53 +44,3 @@ func SyncLearnTime(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	update := bson.M{"$inc": bson.M{"learnTime": 1}, "updateTime": time.Now()}
 	return repo.RepoUpdateDbSetDoc(apictx.CreateRepoCtx(), db, repo.CollectionLearnLog, id, update)
 }
-
-// func DeleteCategory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	// 验证是否为管理员
-// 	isAdmin, err := IsAdmin(c, apictx)
-// 	if err != nil {
-// 		return nil, err
-// 	}
-// 	if !isAdmin {
-// 		return nil, errors.New("没有权限")
-// 	}
-// 	_id := c.Param("id")
-// 	id, _ := primitive.ObjectIDFromHex(_id)
-// 	if id.IsZero() {
-// 		return nil, errors.New("id错误")
-// 	}
-// 	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionCategory, _id)
-// }
-
-// func CategoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	page, size, query := UtilQueryPageSize(c)
-// 	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-// 		CollectName: repo.CollectionCategory,
-// 		Page:        page,
-// 		Size:        size,
-// 		Query:       query,
-// 		Sort:        bson.D{bson.E{Key: "sort", Value: 1}, bson.E{Key: "createTime", Value: 1}},
-// 	})
-// }
-
-// func CategoryDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	_id := c.Param("id")
-// 	id, _ := primitive.ObjectIDFromHex(_id)
-// 	if id.IsZero() {
-// 		return nil, errors.New("id错误")
-// 	}
-// 	cate := &model.Category{}
-// 	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-// 		CollectName: repo.CollectionCategory,
-// 		Query:       repo.Map{"_id": id},
-// 	}, cate)
-// 	if err != nil {
-// 		log.Error(err)
-// 		return nil, err
-// 	}
-
-// 	if !found {
-// 		return nil, errors.New("未找到该数据")
-// 	}
-// 	return cate, nil
-// }

+ 1 - 0
src/api/router.go

@@ -27,6 +27,7 @@ func RegRouters(svc *Service) {
 	root.POSTJWT("/category/create", CreateCategory)
 	root.POSTJWT("/category/delete/:id", DeleteCategory)
 	root.GET("/category/list", CategoryList)
+	root.GET("/category/parse/list", CategoryParseList)
 	root.POSTJWT("/category/update", UpdateCategory)
 
 	// 学习记录

+ 1 - 0
src/db/model/category.go

@@ -16,4 +16,5 @@ type Category struct {
 	Scope      string             `bson:"scope,omitempty" json:"scope"` // 分类所属的数据库 模块唯一
 	CreateTime time.Time          `bson:"createTime,omitempty" json:"createTime"`
 	UpdateTime time.Time          `bson:"updateTime,omitempty" json:"updateTime"`
+	Children   []*Category        `bson:"children,omitempty" json:"children"` // 存储树桩结构
 }