|
@@ -0,0 +1,181 @@
|
|
|
+package mongo
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/dalu/wiki/wiki/storage"
|
|
|
+ "gopkg.in/mgo.v2"
|
|
|
+ "gopkg.in/mgo.v2/bson"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ databaseDefault = "wiki"
|
|
|
+ termCollectionDefault = "term"
|
|
|
+ contentCollectionDefault = "content"
|
|
|
+ diffCollectionDefault = "diff"
|
|
|
+)
|
|
|
+
|
|
|
+type mongoStorage struct {
|
|
|
+ ms *mgo.Session
|
|
|
+ database string
|
|
|
+ termCollection string
|
|
|
+ contentCollection string
|
|
|
+ diffCollection string
|
|
|
+}
|
|
|
+
|
|
|
+type Config struct {
|
|
|
+ Database string
|
|
|
+ TermCollection string
|
|
|
+ ContentCollection string
|
|
|
+ DiffCollection string
|
|
|
+}
|
|
|
+
|
|
|
+func New(ms *mgo.Session, config Config) *mongoStorage {
|
|
|
+ if config.Database == "" {
|
|
|
+ config.Database = databaseDefault
|
|
|
+ }
|
|
|
+ if config.TermCollection == "" {
|
|
|
+ config.TermCollection = termCollectionDefault
|
|
|
+ }
|
|
|
+ if config.ContentCollection == "" {
|
|
|
+ config.ContentCollection = contentCollectionDefault
|
|
|
+ }
|
|
|
+ if config.DiffCollection == "" {
|
|
|
+ config.DiffCollection = diffCollectionDefault
|
|
|
+ }
|
|
|
+ return &mongoStorage{
|
|
|
+ ms: ms.Clone(),
|
|
|
+ database: config.Database,
|
|
|
+ termCollection: config.TermCollection,
|
|
|
+ contentCollection: config.ContentCollection,
|
|
|
+ diffCollection: config.DiffCollection,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Term
|
|
|
+
|
|
|
+func (s *mongoStorage) CreateTerm(term *storage.Term) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.termCollection)
|
|
|
+ return c.Insert(term)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) UpdateTerm(term *storage.Term) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.termCollection)
|
|
|
+ return c.UpdateId(term.ID, term)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) RemoveTerm(term *storage.Term) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.termCollection)
|
|
|
+ return c.RemoveId(term.ID)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetTermByName(name 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.Find(bson.M{"name": name}).One(term); e != nil {
|
|
|
+ return nil, e
|
|
|
+ }
|
|
|
+ return term, nil
|
|
|
+}
|
|
|
+
|
|
|
+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
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetTermByID(id string) (*storage.Term, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetTermBySlug(slug string) (*storage.Term, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GenerateSlug(name string) string {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// Content
|
|
|
+
|
|
|
+func (s *mongoStorage) CreateContent(content *storage.Content) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.contentCollection)
|
|
|
+ return c.Insert(content)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) UpdateContent(content *storage.Content) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.contentCollection)
|
|
|
+ return c.UpdateId(content.ID, content)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) RemoveContent(content *storage.Content) error {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.contentCollection)
|
|
|
+ return c.RemoveId(content.ID)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetContentByTermName(termName string) (*storage.Content, error) {
|
|
|
+ ms := s.ms.Copy()
|
|
|
+ defer ms.Close()
|
|
|
+ c := ms.DB(s.database).C(s.contentCollection)
|
|
|
+ term, e := s.GetTermByName(termName)
|
|
|
+ if e != nil {
|
|
|
+ return nil, e
|
|
|
+ }
|
|
|
+ content := new(storage.Content)
|
|
|
+ if e := c.Find(bson.M{"term_id": term.ID.Hex()}).One(content); e != nil {
|
|
|
+ return nil, e
|
|
|
+ }
|
|
|
+ return content, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetContentByID(id string) (*storage.Content, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetContentByTermID(termID string) (*storage.Content, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetContents() ([]storage.Content, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+// Diff
|
|
|
+
|
|
|
+func (s *mongoStorage) CreateDiff(diff *storage.Diff) error {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) UpdateDiff(diff *storage.Diff) error {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) RemoveDiff(diff *storage.Diff) error {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetDiff(term string) (*storage.Diff, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|
|
|
+
|
|
|
+func (s *mongoStorage) GetDiffs() ([]storage.Diff, error) {
|
|
|
+ panic("implement me")
|
|
|
+}
|