Beispiel #1
0
// Get category by slug in database
func (gs *Shop) GetCategory(slug string, products bool) (Category, error) {
	var c Category
	var err error = errors.New("Category not found")

	if len(gs.Categories) > 0 {
		// find category in cached category list
		for _, category := range gs.Categories {
			if category.Slug == slug {
				c = category
				err = nil
				break
			}
		}
	}

	// try to find category in database
	if err != nil {
		err = database.Find("categories", database.M{"slug": slug}).One(&c)
	}

	if products == true && err == nil {
		// look for category products
		database.Find("products", database.M{"category": slug}).All(&c.Products)
	}

	return c, err
}
Beispiel #2
0
// Get product details in database
func (gs *Shop) GetProduct(category string, slug string) (Product, error) {
	var p Product

	// finc product
	err := database.Find("products", database.M{"category": category, "slug": slug}).One(&p)

	if err == nil {
		p.Category, err = gs.GetCategory(category, false)
	}

	return p, err
}