work in progress

This commit is contained in:
ston1th 2018-09-04 23:15:47 +02:00
commit 07ff5ccffb
23 changed files with 228 additions and 240 deletions

View file

@ -1,44 +1,22 @@
package db
import (
"git.giftfish.de/ston1th/gowiki/pkg/cache"
"git.giftfish.de/ston1th/gowiki/pkg/permission"
"git.giftfish.de/ston1th/gowiki/pkg/reneder"
"git.giftfish.de/ston1th/gowiki/pkg/core"
"git.giftfish.de/ston1th/gowiki/pkg/render"
"git.giftfish.de/ston1th/gowiki/pkg/util"
"github.com/boltdb/bolt"
)
type Article struct {
Title string `json:"title"`
LinkTitle string `json:"-"`
Search string `json:"search"`
Index template.HTML `json:"index"`
Text template.HTML `json:"text"`
MD string `json:"md"`
Created string `json:"created"`
Updated string `json:"updated"`
Public bool `json:"public"`
}
type Page struct {
Title string `json:"title"`
Markdown string `json:"markdown"`
Index template.HTML `json:"index"`
Text template.HTML `json:"text"`
Created string `json:"created"`
Updated string `json:"updated"`
Owner string `json:"owner"`
Perm permission.Permission `json:"perm"`
}
var (
errPageNotFound = dbErr("page not found")
errInvalidPermission = dbErr("invalid permission")
errPrivateWikiPage = dbErr("private wiki pages are not allowed")
)
func (bs *BoltStore) GetPage(title, username string) (p *Page, err error) {
func (bs *BoltStore) GetPage(title, username string) (p *core.Page, err error) {
p = bs.cache.Get(title)
if p == nil {
p = new(Page)
p = new(core.Page)
err = bs.db.View(func(tx *bolt.Tx) error {
v := tx.Bucket([]byte(pageTable)).Get([]byte(title))
if v == nil {
@ -47,43 +25,38 @@ func (bs *BoltStore) GetPage(title, username string) (p *Page, err error) {
return bs.Unmarshal(v, p)
})
if err != nil {
return err
return
}
bs.cache.Add(title, p)
}
if !permission.Read(username, p) {
if !core.ReadPerm(username, p) {
err = errPageNotFound
}
return
}
func (bs *BoltStore) pageExists(title, username string) (page *Page, err error) {
page, err = bs.GetPage(title)
if err != nil {
return
}
if !permission.Verify(username, page) {
err = errPageNotFound
}
// TODO access db not cache
func (bs *BoltStore) pageExists(title, username string) (p *core.Page, err error) {
p, err = bs.GetPage(title, username)
return
}
func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm int) (page *Page, err error) {
p := permission.Parse(perm)
if p == permission.Invalid {
func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm int) (page *core.Page, err error) {
p := core.ParsePerm(perm)
if p == core.Invalid {
err = errInvalidPermission
return
}
if section == wikiSection && p == permission.Private {
if section == wikiSection && p == core.Private {
err = errPrivateWikiPage
return
}
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
if _, err = bs.pageExists(st, username); err != nil {
return
}
page = &Page{
page = &core.Page{
Title: title,
Markdown: markdown,
Created: created(username),
@ -103,25 +76,25 @@ func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm
return
}
err = index.Add(title, search)
err = bs.index.Add(title, st, search)
if err != nil {
return
}
cache.Add(st, page)
bs.cache.Add(st, page)
return
}
func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm int) (page *Page, err error) {
p := permission.Parse(perm)
if p == permission.Invalid {
func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm int) (page *core.Page, err error) {
p := core.ParsePerm(perm)
if p == core.Invalid {
err = errInvalidPermission
return
}
if section == wikiSection && p == permission.Private {
if section == wikiSection && p == core.Private {
err = errPrivateWikiPage
return
}
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
page, err = bs.pageExists(st, username)
if err != nil {
return
@ -143,20 +116,23 @@ func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm
return
}
err = index.Add(title, search)
err = bs.index.Add(title, st, search)
if err != nil {
return
}
cache.Add(st, page)
bs.cache.Add(st, page)
return
}
func (bs *BoltStore) DeletePage(title, section, username string) error {
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
if _, err := bs.pageExists(st, username); err != nil {
return err
}
return bs.db.Update(func(tx *bolt.Tx) error {
if err := bs.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte(pageTable)).Delete([]byte(st))
})
}); err != nil {
return err
}
return bs.index.Delete(st)
}