initial
This commit is contained in:
commit
6a483096dd
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# wiki
|
||||
|
||||
wiki software in Go
|
||||
|
||||
## status
|
||||
|
||||
in development
|
37
wiki/storage/interface.go
Normal file
37
wiki/storage/interface.go
Normal file
@ -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
wiki/storage/model.go
Normal file
21
wiki/storage/model.go
Normal file
@ -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
wiki/storage/mongo/storage.go
Normal file
181
wiki/storage/mongo/storage.go
Normal file
@ -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
wiki/wiki.go
Normal file
1
wiki/wiki.go
Normal file
@ -0,0 +1 @@
|
||||
package wiki
|
Loading…
Reference in New Issue
Block a user