added user independent sections and started db migration work
This commit is contained in:
parent
6a2dc8146c
commit
16ea95bf09
24 changed files with 622 additions and 122 deletions
103
pkg/db/section.go
Normal file
103
pkg/db/section.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
||||
"git.giftfish.de/ston1th/gowiki/pkg/log"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const sectionPrefix = "section/"
|
||||
|
||||
var (
|
||||
errSectionExists = errors.New("db: section already exist")
|
||||
)
|
||||
|
||||
func (db *DB) GetSections() (secs core.Sections, err error) {
|
||||
err = db.store.ForEach(func(k string, _ []byte) error {
|
||||
if trim, ok := hasTrimPrefix(k, sectionPrefix); ok {
|
||||
s, err := db.GetSection(trim)
|
||||
if err == nil {
|
||||
secs = append(secs, s)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
sort.Sort(secs)
|
||||
return
|
||||
}
|
||||
|
||||
func (db *DB) GetSection(section string) (s core.Section, err error) {
|
||||
err = db.store.Get(sectionPrefix+section, &s)
|
||||
return
|
||||
}
|
||||
|
||||
func (db *DB) SectionExists(section string) error {
|
||||
return db.store.Get(sectionPrefix+section, nil)
|
||||
}
|
||||
|
||||
func (db *DB) CreateSection(section string, members []string, user bool) error {
|
||||
section = userRe.ReplaceAllString(section, "")
|
||||
if err := db.SectionExists(section); err == nil {
|
||||
return errSectionExists
|
||||
}
|
||||
return db.UpdateSection(section, members, user)
|
||||
}
|
||||
|
||||
func (db *DB) UpdateSection(section string, members []string, user bool) error {
|
||||
section = userRe.ReplaceAllString(section, "")
|
||||
if _, ok := core.Contains(section, reservedNames); ok {
|
||||
return errNameReserved
|
||||
}
|
||||
var m []string
|
||||
if user {
|
||||
m = append(m, section)
|
||||
} else {
|
||||
for _, v := range members {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if err := db.UserExists(v); err == nil {
|
||||
m = append(m, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
return db.store.Set(sectionPrefix+section, &core.Section{section, user, m})
|
||||
}
|
||||
|
||||
func (db *DB) DeleteSection(section string, user bool) error {
|
||||
if section == core.WikiSection {
|
||||
return errors.New("section '" + core.WikiSection + "' cannot be deleted")
|
||||
}
|
||||
s, err := db.GetSection(section)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.store.Delete(sectionPrefix + section)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !s.User && !user {
|
||||
return nil
|
||||
}
|
||||
section += "/"
|
||||
var pages []string
|
||||
db.store.ForEach(func(k string, _ []byte) error {
|
||||
if trim, ok := hasTrimPrefix(k, pagePrefix); ok {
|
||||
if strings.HasPrefix(trim, section) {
|
||||
pages = append(pages, trim)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
for _, p := range pages {
|
||||
err := db.deletePage(p)
|
||||
if err != nil {
|
||||
log.Debugf("DeleteSection %s, %s", section, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue