diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-05 10:53:02 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-05 10:53:02 -0800 |
commit | 93fbf89235828049eb1aef485bc8fef890ddf8e6 (patch) | |
tree | 06a2c6bc727b54081d3aab46b53bd274f4edb407 /system/db | |
parent | 6ab44d82f825d7013308c57c5ee758c89ef904a0 (diff) |
update db.Query to return total of content count, add implement change throughout references, add pagination to admin post results
Diffstat (limited to 'system/db')
-rw-r--r-- | system/db/content.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/system/db/content.go b/system/db/content.go index 0cfadf4..76d95c5 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -215,8 +215,21 @@ type QueryOptions struct { } // Query retrieves a set of content from the db based on options -func Query(namespace string, opts QueryOptions) [][]byte { +// and returns the total number of content in the namespace and the content +func Query(namespace string, opts QueryOptions) (int, [][]byte) { var posts [][]byte + var total int + + // correct bad input rather than return nil or error + // similar to default case for opts.Order switch below + if opts.Count < 0 { + opts.Count = 0 + } + + if opts.Offset < 0 { + opts.Offset = 0 + } + store.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(namespace)) if b == nil { @@ -225,6 +238,12 @@ func Query(namespace string, opts QueryOptions) [][]byte { c := b.Cursor() n := b.Stats().KeyN + total = n + + // return nil if no content + if n == 0 { + return nil + } var start, end int switch opts.Count { @@ -279,12 +298,29 @@ func Query(namespace string, opts QueryOptions) [][]byte { i++ cur++ } + + default: + // results for DESC order + for k, v := c.First(); k != nil; k, v = c.Next() { + if cur < start { + cur++ + continue + } + + if cur >= end { + break + } + + posts = append(posts, v) + i++ + cur++ + } } return nil }) - return posts + return total, posts } // SortContent sorts all content of the type supplied as the namespace by time, |