added sections overview

This commit is contained in:
ston1th 2019-01-22 21:45:32 +01:00
commit 799ff0fb2f
16 changed files with 181 additions and 66 deletions

View file

@ -8,6 +8,7 @@ import (
"git.giftfish.de/ston1th/gowiki/pkg/render"
"git.giftfish.de/ston1th/gowiki/pkg/store"
"sort"
"strings"
)
const pagePrefix = "page/"
@ -21,9 +22,42 @@ var (
errDeleteIndexPage = errors.New("db: index page can not be deleted")
)
func (db *DB) GetAllPages(username string) (pages core.Pages) {
func (db *DB) GetAllSections(username string) (sections core.Sections) {
m := make(map[string]core.UpdatedPages)
db.store.ForEach(func(k string, _ []byte) error {
if trim, ok := hasTrimPrefix(k, pagePrefix); ok {
if trim == core.IndexURI {
return nil
}
p, err := db.getPage(trim, username)
if err == nil {
s := trim[:strings.Index(trim, "/")]
if _, ok := m[s]; !ok {
m[s] = core.UpdatedPages{}
}
m[s] = append(m[s], *p)
}
}
return nil
})
for s, p := range m {
sort.Sort(p)
l := len(p)
if l > 5 {
l = 5
}
sections = append(sections, core.Section{s, p[:l]})
}
sort.Sort(sections)
return
}
func (db *DB) GetAllSectionPages(section, username string) (pages core.Pages, err error) {
db.store.ForEach(func(k string, _ []byte) error {
if trim, ok := hasTrimPrefix(k, pagePrefix); ok {
if !strings.HasPrefix(trim, section+"/") {
return nil
}
if trim == core.IndexURI {
return nil
}
@ -34,6 +68,10 @@ func (db *DB) GetAllPages(username string) (pages core.Pages) {
}
return nil
})
if len(pages) == 0 {
err = errPageNotFound
return
}
sort.Sort(pages)
return
}