示例#1
0
//this returns all the categories in a structered json tree
func (cat *Category) GetCategoryTree(r *http.Request, parentId int64) ([]CategoryBranch, error) {
	//get list of all categories
	categoryModel := model.Category{}
	categories, err := categoryModel.GetAllCategories(r)

	if err != nil {
		return []CategoryBranch{}, err
	}

	branches := make([]CategoryBranch, 0, 20)

	for _, cat := range categories {
		y := CategoryBranch{
			Name:     cat.Name,
			Id:       cat.Id,
			ParentId: cat.ParentId,
		}

		branches = append(branches, y)
	}

	categoryTree, err := cat.GetCategoryBranch(branches, parentId)

	if err != nil {
		return []CategoryBranch{}, err
	}

	return categoryTree, nil
}
示例#2
0
func (cat *Category) GetAllCategories(r *http.Request) ([]CategoryReturn, error) {
	categoryModel := model.Category{}
	categories, err := categoryModel.GetAllCategories(r)

	if err != nil {
		log.Println(err)
	}

	results := make([]CategoryReturn, 0, 20)

	for _, r := range categories {
		y := CategoryReturn{
			Name:     r.Name,
			Id:       r.Id,
			ParentId: r.ParentId,
			Products: r.Products,
			Key:      r.Key,
		}

		results = append(results, y)
	}

	return results, nil
}