diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-14 09:57:55 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-14 09:57:55 -0800 |
commit | b3aa9440f62db5a530397c525a42b4fca7b27bab (patch) | |
tree | 8775b01a283c32dce3297dbddad1ddf14a114c33 /system/db/content.go | |
parent | bd96fae14bc6201cb6e81b1cb2c51b00314810b6 (diff) |
adding db method ContentBySlug to lookup the type & id of content by its slug and return it directly
Diffstat (limited to 'system/db/content.go')
-rw-r--r-- | system/db/content.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/system/db/content.go b/system/db/content.go index 0661556..87b3e69 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -229,6 +229,41 @@ func Content(target string) ([]byte, error) { return val.Bytes(), nil } +// ContentBySlug does a lookup in the content index to find the type and id of +// the requested content. Subsequently, issues the lookup in the type bucket and +// returns the data at that ID or nil if nothing exists. +func ContentBySlug(slug string) ([]byte, error) { + val := &bytes.Buffer{} + err := store.View(func(tx *bolt.Tx) error { + var t, id string + b := tx.Bucket([]byte("__contentIndex")) + idx := b.Get([]byte(slug)) + + if idx != nil { + tid := strings.Split(string(idx), ":") + + if len(tid) < 2 { + return fmt.Errorf("Bad data in content index for slug: %s", slug) + } + + t, id = tid[0], tid[1] + } + + c := tx.Bucket([]byte(t)) + _, err := val.Write(c.Get([]byte(id))) + if err != nil { + return err + } + + return nil + }) + if err != nil { + return nil, err + } + + return val.Bytes(), nil +} + // ContentAll retrives all items from the database within the provided namespace func ContentAll(namespace string) [][]byte { var posts [][]byte |