commit 6a483096dd0fa8f296c1fbebfb6308f4e078e243 Author: Darko Luketic Date: Fri Feb 10 18:52:07 2017 +0100 initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..0940fd6 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# wiki + +wiki software in Go + +## status + +in development \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..38dd16d --- /dev/null +++ b/main.go @@ -0,0 +1,3 @@ +package main + +func main() {} diff --git a/wiki/storage/interface.go b/wiki/storage/interface.go new file mode 100644 index 0000000..c954136 --- /dev/null +++ b/wiki/storage/interface.go @@ -0,0 +1,37 @@ +package storage + +type Storage interface { + TermStorage + ContentStorage + DiffStorage +} + + +type TermStorage interface { + CreateTerm(term *Term) error + UpdateTerm(term *Term) error + RemoveTerm(term *Term) error + GetTermByName(name string) (*Term, error) + GetTermBySlug(slug string) (*Term, error) + GetTermByID(id string) (*Term, error) + GetTerms() ([]Term, error) + GenerateSlug(name string) string +} + +type ContentStorage interface { + CreateContent(content *Content) error + UpdateContent(content *Content) error + RemoveContent(content *Content) error + GetContentByTermName(termName string) (*Content, error) + GetContentByTermID(termID string) (*Content, error) + GetContentByID(id string) (*Content, error) + GetContents() ([]Content, error) +} + +type DiffStorage interface { + CreateDiff(diff *Diff) error + UpdateDiff(diff *Diff) error + RemoveDiff(diff *Diff) error + GetDiff(term string) (*Diff, error) + GetDiffs() ([]Diff, error) +} \ No newline at end of file diff --git a/wiki/storage/model.go b/wiki/storage/model.go new file mode 100644 index 0000000..a631fa5 --- /dev/null +++ b/wiki/storage/model.go @@ -0,0 +1,21 @@ +package storage + +import "gopkg.in/mgo.v2/bson" + +type Term struct { + ID bson.ObjectId `bson:"_id,omitempty"` + Slug string + Name string +} + +type Content struct { + ID bson.ObjectId `bson:"_id,omitempty"` + TermID string + Text string +} + +type Diff struct { + ID bson.ObjectId `bson:"_id,omitempty"` + ContentID string + +} diff --git a/wiki/storage/mongo/storage.go b/wiki/storage/mongo/storage.go new file mode 100644 index 0000000..4052eb2 --- /dev/null +++ b/wiki/storage/mongo/storage.go @@ -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") +} diff --git a/wiki/wiki.go b/wiki/wiki.go new file mode 100644 index 0000000..3e1ef4c --- /dev/null +++ b/wiki/wiki.go @@ -0,0 +1 @@ +package wiki