func (c *Category) Get(page, count int) error { session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return err } defer session.Close() err = session.DB(database.ProductDatabase).C(database.CategoryCollectionName).Find(bson.M{"id": c.CategoryID}).One(&c) if err != nil { return err } c.ProductListing = &products.PaginatedProductListing{ Page: page, PerPage: count, Parts: []products.Part{}, } c.ProductListing.TotalItems, err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(bson.M{"id": bson.M{"$in": c.ProductIdentifiers}}).Count() if err != nil { c.ProductListing.TotalItems = 1 } err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(bson.M{"id": bson.M{"$in": c.ProductIdentifiers}}).Sort("id").Skip((page - 1) * count).Limit(count).All(&c.ProductListing.Parts) if err != nil { return err } c.ProductListing.ReturnedCount = len(c.ProductListing.Parts) c.ProductListing.TotalPages = c.ProductListing.TotalItems / c.ProductListing.PerPage return nil }
func GetCategoryParts(catId, page, count int) (PartResponse, error) { var parts PartResponse session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return parts, err } defer session.Close() //get category's children var cat Category err = session.DB(database.ProductDatabase).C(database.CategoryCollectionName).Find(bson.M{"id": catId}).Select(bson.M{"children": 1}).One(&cat) if err != nil { return parts, err } children := []int{catId} for _, child := range cat.Children { children = append(children, child.CategoryID) } parts.Page = page //get parts of category and its children query := bson.M{"categories": bson.M{"$elemMatch": bson.M{"id": bson.M{"$in": children}}}} err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(query).Limit(count).Skip((page - 1) * count).All(&parts.Parts) if err != nil { return parts, err } //get total parts count total_items, err := session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(query).Count() parts.TotalPages = int(math.Ceil(float64(total_items) / float64(count))) return parts, err }
// Identifiers ... func Identifiers(brand int, dtx *apicontext.DataContext) ([]string, error) { var parts []string brands := []int{brand} if brand == 0 { brands = getBrandsFromDTX(dtx) } session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return parts, err } defer session.Close() qry := bson.M{ "brand.id": bson.M{ "$in": brands, }, "status": bson.M{ "$in": []int{800, 900}, }, } err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(qry).Distinct("part_number", &parts) if err != nil { return parts, err } sort.Strings(parts) return parts, nil }
// FromDatabase ... func (p *Part) FromDatabase(brands []int) error { session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return err } defer session.Close() query := bson.M{"id": p.ID, "brand.id": bson.M{"$in": brands}} return session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(query).One(&p) }
func GetCategoryTree(dtx *apicontext.DataContext) ([]Category, error) { var cats []Category session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return cats, err } defer session.Close() query := bson.M{"parent_id": 0, "is_lifestyle": false, "brand.id": bson.M{"$in": dtx.BrandArray}} err = session.DB(database.ProductDatabase).C(database.CategoryCollectionName).Find(query).Sort("sort").All(&cats) return cats, err }
func (p *Part) GetPartByPartNumber() (err error) { session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return err } defer session.Close() pattern := bson.RegEx{ Pattern: "^" + p.PartNumber + "$", Options: "i", } return session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(bson.M{"part_number": pattern}).One(&p) }
func All(page, count int, dtx *apicontext.DataContext) ([]Part, error) { var parts []Part brands := getBrandsFromDTX(dtx) session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return parts, err } defer session.Close() err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(bson.M{"brand.id": bson.M{"$in": brands}}).Sort("id:1").Skip(page * count).Limit(count).All(&parts) return parts, err }
func Latest(count int, dtx *apicontext.DataContext, brand int) ([]Part, error) { var parts []Part brands := getBrandsFromDTX(dtx) if brand > 0 { brands = []int{brand} } session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return parts, err } defer session.Close() err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(bson.M{"brand.id": bson.M{"$in": brands}}).Sort("-date_added").Limit(count).All(&parts) return parts, err }
func (p *Part) GetRelated(dtx *apicontext.DataContext) ([]Part, error) { var parts []Part brands := getBrandsFromDTX(dtx) session, err := mgo.DialWithInfo(database.MongoPartConnectionString()) if err != nil { return parts, err } defer session.Close() query := bson.M{ "id": bson.M{ "$in": p.Related, }, "brand.id": bson.M{ "$in": brands, }, } err = session.DB(database.ProductDatabase).C(database.ProductCollectionName).Find(query).Sort("id:1").All(&parts) return parts, err }