示例#1
0
func (cat *Category) GetCategories(r *http.Request) ([]CategoryReturn, error) {
	parentCat := r.Header.Get("Parent-Category")

	//convert header string to int64
	parentId, err := strconv.ParseInt(parentCat, 10, 64)

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

	categoryModel := model.Category{}
	categories, err := categoryModel.GetCategories(r, parentId)

	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
}
示例#2
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
}
示例#3
0
func (cat *Category) CreateCategory(r *http.Request) (bool, error) {
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&cat)

	if err != nil {
		//handle err
		return false, err
	}

	//populate category data
	categoryModel := model.Category{}
	categoryModel.Name = cat.Name
	err = categoryModel.CreateCategory(r, cat.ParentId)

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

	return true, nil
}
示例#4
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
}