package mongo import ( "github.com/dalu/gopiwik/piwik" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) func NewVisitStorage(ms *mgo.Session, db, col string) VisitStorage { return VisitStorage{ ms: ms.Clone(), db: db, col: col, } } type VisitStorage struct { ms *mgo.Session db, col string } func (s VisitStorage) Add(visit *piwik.Visit) error { ms := s.ms.Copy() defer ms.Close() c := ms.DB(s.db).C(s.col) visit.ID = bson.NewObjectId().Hex() return c.Insert(visit) } func (s VisitStorage) Find(query map[string]interface{}) (*piwik.Visit, error) { ms := s.ms.Copy() defer ms.Close() c := ms.DB(s.db).C(s.col) visit := new(piwik.Visit) if e := c.Find(query).One(visit); e != nil { return nil, e } return visit, nil } func (s VisitStorage) FindAll(query map[string]interface{}) ([]*piwik.Visit, error) { ms := s.ms.Copy() defer ms.Close() c := ms.DB(s.db).C(s.col) visits := []*piwik.Visit{} if e := c.Find(query).All(visits); e != nil { return nil, e } return visits, nil }