func (d *MongoDBDriver) List(q apipb.CatalogListRequest) ([]*models.Catalog, error) { c := d.Session.DB("").C(CatalogCollection) var result []*models.Catalog query := bson.M{} if q.Limit == 0 { q.Limit = 100 } switch q.Status { case apipb.CatalogListRequest_ONLY_DRAFT: query["status"] = apipb.CatalogStatus_DRAFT case apipb.CatalogListRequest_ONLY_APPROVED: query["status"] = apipb.CatalogStatus_APPROVED } if q.Time > 0 { query["visible_at"] = bson.M{"$lte": q.Time} if q.HideExpired { m := int64(0) now := models.MillisecondsNow() if q.Time > now { m = q.Time } else { m = now } query["expires_at"] = bson.M{"$gt": m} } else { query["expires_at"] = bson.M{"$gt": q.Time} } } else if q.HideExpired { query["expires_at"] = bson.M{"$gt": models.MillisecondsNow()} } iter := c.Find(query).Limit(int(q.Limit)).Sort("-visible_at").Iter() err := iter.All(&result) if err != nil { return nil, err } return result, nil }
func (s *Server) Current() (*pb.Catalog, error) { query := pb.CatalogListRequest{ Limit: 1, Status: pb.CatalogListRequest_ONLY_APPROVED, Time: models.MillisecondsNow(), } res, err := s.Storage.List(query) if err != nil { return nil, err } if len(res) == 0 { return nil, models.ErrorNotFound } return res[0].ToProto(), nil }