gopiwik/mongo/visit.go

52 lines
1019 B
Go
Raw Permalink Normal View History

2017-03-03 22:07:39 +01:00
package mongo
import (
2021-01-06 17:34:05 +01:00
"git.icod.de/dalu/gopiwik/piwik"
2017-03-03 22:07:39 +01:00
"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
}