func AllProducts() (apiProducts []*duoerlapi.Product, err error) { // Find all the products dbProducts, err := products.FindAll(bson.M{}) if err != nil { utils.PrintStackAndError(err) return } // Collect brand/author Ids and find them brandIds, authorIds := products.CollectBrandAndAuthorIds(dbProducts) dbBrands, err := brands.FindByIds(brandIds) if err != nil { utils.PrintStackAndError(err) return } dbAuthors, err := users.FindByIds(authorIds) if err != nil { utils.PrintStackAndError(err) return } // Build the brandMap and authorMap brandMap := brands.BuildBrandMap(dbBrands) authorMap := users.BuildUserMap(dbAuthors) apiProducts = toApiProducts(dbProducts, brandMap, authorMap) return }
func makeApiReviews(reviewz []*reviews.Review) (apiReviews []*duoerlapi.Review, err error) { authorIds, productIds := reviews.CollectAuthorAndProductIds(reviewz) authorz, err := users.FindByIds(authorIds) if err != nil { utils.PrintStackAndError(err) return } productz, err := products.FindByIds(productIds) if err != nil { utils.PrintStackAndError(err) return } productMap := products.BuildProductMap(productz) authorMap := users.BuildUserMap(authorz) apiReviews = toApiReviews(reviewz, productMap, authorMap) return }