2017-02-10 18:52:07 +01:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
2017-02-13 22:21:57 +01:00
|
|
|
"github.com/dalu/wiki/storage"
|
2017-02-10 18:52:07 +01:00
|
|
|
"gopkg.in/mgo.v2"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
databaseDefault = "wiki"
|
|
|
|
termCollectionDefault = "term"
|
2017-02-12 17:13:08 +01:00
|
|
|
revisionCollectionDefault = "revision"
|
2017-02-10 18:52:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mongoStorage struct {
|
|
|
|
ms *mgo.Session
|
|
|
|
database string
|
|
|
|
termCollection string
|
2017-02-10 22:00:26 +01:00
|
|
|
revisionCollection string
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Database string
|
|
|
|
TermCollection string
|
2017-02-10 22:00:26 +01:00
|
|
|
RevisionCollection string
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func New(ms *mgo.Session, config Config) mongoStorage {
|
2017-02-10 18:52:07 +01:00
|
|
|
if config.Database == "" {
|
|
|
|
config.Database = databaseDefault
|
|
|
|
}
|
|
|
|
if config.TermCollection == "" {
|
|
|
|
config.TermCollection = termCollectionDefault
|
|
|
|
}
|
2017-02-10 22:00:26 +01:00
|
|
|
if config.RevisionCollection == "" {
|
2017-02-12 17:13:08 +01:00
|
|
|
config.RevisionCollection = revisionCollectionDefault
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
2017-02-10 22:00:26 +01:00
|
|
|
|
|
|
|
return mongoStorage{
|
2017-02-10 18:52:07 +01:00
|
|
|
ms: ms.Clone(),
|
|
|
|
database: config.Database,
|
|
|
|
termCollection: config.TermCollection,
|
2017-02-10 22:00:26 +01:00
|
|
|
revisionCollection: config.RevisionCollection,
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
// --- Term ---
|
2017-02-10 18:52:07 +01:00
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) CreateTerm(term *storage.Term) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
return c.Insert(term)
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) UpdateTerm(term *storage.Term) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
return c.UpdateId(term.ID, term)
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) RemoveTerm(term *storage.Term) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
return c.RemoveId(term.ID)
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetTermByName(name string) (*storage.Term, error) {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
term := new(storage.Term)
|
|
|
|
if e := c.Find(bson.M{"name": name}).One(term); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return term, nil
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetTermBySlug(slug string) (*storage.Term, error) {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
2017-02-10 22:00:26 +01:00
|
|
|
term := new(storage.Term)
|
|
|
|
if e := c.Find(bson.M{"slug": slug}).One(term); e != nil {
|
|
|
|
return nil, e
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
2017-02-10 22:00:26 +01:00
|
|
|
return term, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetTermByID(id string) (*storage.Term, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
term := new(storage.Term)
|
|
|
|
if e := c.FindId(bson.ObjectIdHex(id)).One(term); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return term, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetTerms() ([]*storage.Term, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.termCollection)
|
|
|
|
terms := []*storage.Term{}
|
|
|
|
if e := c.Find(nil).All(&terms); e != nil {
|
|
|
|
return terms, e
|
|
|
|
}
|
|
|
|
return terms, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
// --- Revision ---
|
2017-02-10 18:52:07 +01:00
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) CreateRevision(Revision *storage.Revision) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
2017-02-10 22:00:26 +01:00
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
return c.Insert(Revision)
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) RemoveRevision(Revision *storage.Revision) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
2017-02-10 22:00:26 +01:00
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
return c.RemoveId(Revision.ID)
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) RemoveRevisionsByTermID(termID string) error {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
2017-02-10 22:00:26 +01:00
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
_, e := c.RemoveAll(bson.M{"term_id": termID})
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetRevisionByTermName(termName string) (*storage.Revision, error) {
|
2017-02-10 18:52:07 +01:00
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
2017-02-10 22:00:26 +01:00
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
2017-02-10 18:52:07 +01:00
|
|
|
term, e := s.GetTermByName(termName)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2017-02-10 22:00:26 +01:00
|
|
|
Revision := new(storage.Revision)
|
|
|
|
if e := c.Find(bson.M{"term_id": term.ID.Hex()}).One(Revision); e != nil {
|
2017-02-10 18:52:07 +01:00
|
|
|
return nil, e
|
|
|
|
}
|
2017-02-10 22:00:26 +01:00
|
|
|
return Revision, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetRevisionByTermSlug(termSlug string) (*storage.Revision, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
term, e := s.GetTermByName(termSlug)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
Revision := new(storage.Revision)
|
|
|
|
if e := c.Find(bson.M{"term_id": term.ID.Hex()}).One(Revision); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return Revision, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetRevisionByID(id string) (*storage.Revision, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
Revision := new(storage.Revision)
|
|
|
|
if e := c.FindId(bson.ObjectIdHex(id)).One(Revision); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return Revision, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetRevisionByTermID(termID string) (*storage.Revision, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
term, e := s.GetTermByID(termID)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
Revision := new(storage.Revision)
|
|
|
|
if e := c.Find(bson.M{"term_id": term.ID.Hex()}).One(Revision); e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return Revision, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:00:26 +01:00
|
|
|
func (s mongoStorage) GetRevisions() ([]*storage.Revision, error) {
|
|
|
|
ms := s.ms.Copy()
|
|
|
|
defer ms.Close()
|
|
|
|
c := ms.DB(s.database).C(s.revisionCollection)
|
|
|
|
terms := []*storage.Revision{}
|
|
|
|
if e := c.Find(nil).All(&terms); e != nil {
|
|
|
|
return terms, e
|
|
|
|
}
|
|
|
|
return terms, nil
|
2017-02-10 18:52:07 +01:00
|
|
|
}
|