Darko Luketic 5 years ago
commit
6a483096dd
6 changed files with 250 additions and 0 deletions
  1. 7 0
      README.md
  2. 3 0
      main.go
  3. 37 0
      wiki/storage/interface.go
  4. 21 0
      wiki/storage/model.go
  5. 181 0
      wiki/storage/mongo/storage.go
  6. 1 0
      wiki/wiki.go

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+# wiki
+
+wiki software in Go
+
+## status
+
+in development

+ 3 - 0
main.go

@@ -0,0 +1,3 @@
+package main
+
+func main() {}

+ 37 - 0
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)
+}

+ 21 - 0
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
+
+}

+ 181 - 0
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")
+}

+ 1 - 0
wiki/wiki.go

@@ -0,0 +1 @@
+package wiki