some UI work, editPOST handler
This commit is contained in:
27
storage/interface.go
Normal file
27
storage/interface.go
Normal file
@ -0,0 +1,27 @@
|
||||
package storage
|
||||
|
||||
type Interface interface {
|
||||
TermInterface
|
||||
RevisionInterface
|
||||
}
|
||||
|
||||
type TermInterface 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)
|
||||
}
|
||||
|
||||
type RevisionInterface interface {
|
||||
CreateRevision(revision *Revision) error
|
||||
RemoveRevision(revision *Revision) error
|
||||
RemoveRevisionsByTermID(termID string) error
|
||||
GetRevisionByTermName(termName string) (*Revision, error)
|
||||
GetRevisionByTermSlug(termSlug string) (*Revision, error)
|
||||
GetRevisionByTermID(termID string) (*Revision, error)
|
||||
GetRevisionByID(id string) (*Revision, error)
|
||||
GetRevisions() ([]*Revision, error)
|
||||
}
|
33
storage/model.go
Normal file
33
storage/model.go
Normal file
@ -0,0 +1,33 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type Term struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
Slug string `bson:"slug"`
|
||||
Language string `bson:"lang"`
|
||||
Name string `bson:"name"`
|
||||
CurrentRevisionID string `bson:"revision_id,omitempty"`
|
||||
Redirect string `bson:"redirect,omitempty"`
|
||||
}
|
||||
|
||||
type Revision struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
TermID string `bson:"term_id"`
|
||||
AuthorIP string `bson:"author_ip"`
|
||||
PublishDate time.Time `bson:"date"`
|
||||
Summary string `bson:"summary,omitempty"`
|
||||
Text string `bson:"text"`
|
||||
HTML string `bson:"html,omitempty"`
|
||||
}
|
||||
|
||||
type Talk struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
TermID string `bson:"term_id"`
|
||||
Text string `bson:"text"`
|
||||
HTML string `bson:"html,omitempty"`
|
||||
}
|
206
storage/mongo/storage.go
Normal file
206
storage/mongo/storage.go
Normal file
@ -0,0 +1,206 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"github.com/dalu/wiki/storage"
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
const (
|
||||
databaseDefault = "wiki"
|
||||
termCollectionDefault = "term"
|
||||
revisionCollectionDefault = "revision"
|
||||
)
|
||||
|
||||
type mongoStorage struct {
|
||||
ms *mgo.Session
|
||||
database string
|
||||
termCollection string
|
||||
revisionCollection string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Database string
|
||||
TermCollection string
|
||||
RevisionCollection string
|
||||
}
|
||||
|
||||
func New(ms *mgo.Session, config Config) mongoStorage {
|
||||
if config.Database == "" {
|
||||
config.Database = databaseDefault
|
||||
}
|
||||
if config.TermCollection == "" {
|
||||
config.TermCollection = termCollectionDefault
|
||||
}
|
||||
if config.RevisionCollection == "" {
|
||||
config.RevisionCollection = revisionCollectionDefault
|
||||
}
|
||||
|
||||
return mongoStorage{
|
||||
ms: ms.Clone(),
|
||||
database: config.Database,
|
||||
termCollection: config.TermCollection,
|
||||
revisionCollection: config.RevisionCollection,
|
||||
}
|
||||
}
|
||||
|
||||
// --- 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) GetTermBySlug(slug 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{"slug": slug}).One(term); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return term, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// --- Revision ---
|
||||
|
||||
func (s mongoStorage) CreateRevision(Revision *storage.Revision) error {
|
||||
ms := s.ms.Copy()
|
||||
defer ms.Close()
|
||||
c := ms.DB(s.database).C(s.revisionCollection)
|
||||
return c.Insert(Revision)
|
||||
}
|
||||
|
||||
func (s mongoStorage) RemoveRevision(Revision *storage.Revision) error {
|
||||
ms := s.ms.Copy()
|
||||
defer ms.Close()
|
||||
c := ms.DB(s.database).C(s.revisionCollection)
|
||||
return c.RemoveId(Revision.ID)
|
||||
}
|
||||
|
||||
func (s mongoStorage) RemoveRevisionsByTermID(termID string) error {
|
||||
ms := s.ms.Copy()
|
||||
defer ms.Close()
|
||||
c := ms.DB(s.database).C(s.revisionCollection)
|
||||
_, e := c.RemoveAll(bson.M{"term_id": termID})
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s mongoStorage) GetRevisionByTermName(termName string) (*storage.Revision, error) {
|
||||
ms := s.ms.Copy()
|
||||
defer ms.Close()
|
||||
c := ms.DB(s.database).C(s.revisionCollection)
|
||||
term, e := s.GetTermByName(termName)
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user