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 GetBrandFollowers(brandIdHex string) (apiUsers []*duoerlapi.User, err error) { brandId, err := utils.ToObjectId(brandIdHex) if err != nil { utils.PrintStackAndError(err) return } followbrandz, err := followbrands.FindByBrandId(brandId) if err != nil { utils.PrintStackAndError(err) return } maxNum := len(followbrandz) // Get random number users if maxNum > configs.BRAND_SHOW_FOLLOWER_NUM { randIndex, err := randutil.IntRange(0, maxNum) if err != nil { utils.PrintStackAndError(err) randIndex = 0 } leftIndex := randIndex - configs.BRAND_SHOW_FOLLOWER_NUM if leftIndex < 0 { followbrandz = followbrandz[0:configs.BRAND_SHOW_FOLLOWER_NUM] } else { followbrandz = followbrandz[leftIndex:randIndex] } } followerIds := []bson.ObjectId{} for _, followBrand := range followbrandz { followerIds = append(followerIds, followBrand.UserId) } followers, err := users.FindByIds(followerIds) if err != nil { utils.PrintStackAndError(err) return } apiUsers = toApiUsers(followers) 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 }