summaryrefslogtreecommitdiff
path: root/system/db
diff options
context:
space:
mode:
authorSteve <nilslice@gmail.com>2016-12-19 11:19:53 -0800
committerGitHub <noreply@github.com>2016-12-19 11:19:53 -0800
commit3791fadda7b761ffba38c567da29e2e71acd1dfb (patch)
tree79d810f9aafa1868ee0760983937470d0eea3db8 /system/db
parentb20c5bdee38682edc851e646d815a34689c3c923 (diff)
[addons] Creating foundation for plugin-like system "Addons" (#24)
* adding addons dir and sample addon which enables the use of a new input element in forms for referencing other content. "addons" is a conceptual plugin-like feature, similar to wordpress "plugins" dir, but not as sophisticated
Diffstat (limited to 'system/db')
-rw-r--r--system/db/cache.go35
-rw-r--r--system/db/config.go44
-rw-r--r--system/db/content.go20
-rw-r--r--system/db/init.go6
4 files changed, 57 insertions, 48 deletions
diff --git a/system/db/cache.go b/system/db/cache.go
index 5f2dd03..30ecf5a 100644
--- a/system/db/cache.go
+++ b/system/db/cache.go
@@ -2,10 +2,8 @@ package db
import (
"encoding/base64"
- "encoding/json"
"fmt"
"net/http"
- "net/url"
"strings"
"time"
)
@@ -39,41 +37,10 @@ func NewEtag() string {
// InvalidateCache sets a new Etag for http responses
func InvalidateCache() error {
- kv := make(map[string]interface{})
-
- c, err := ConfigAll()
+ err := PutConfig("etag", NewEtag())
if err != nil {
return err
}
- err = json.Unmarshal(c, &kv)
- if err != nil {
- return err
- }
-
- kv["etag"] = NewEtag()
-
- data := make(url.Values)
- for k, v := range kv {
- switch v.(type) {
- case string:
- data.Set(k, v.(string))
- case []string:
- vv := v.([]string)
- for i := range vv {
- if i == 0 {
- data.Set(k, vv[i])
- } else {
- data.Add(k, vv[i])
- }
- }
- }
- }
-
- err = SetConfig(data)
- if err != nil {
-
- }
-
return nil
}
diff --git a/system/db/config.go b/system/db/config.go
index b5a07e4..4bbf29b 100644
--- a/system/db/config.go
+++ b/system/db/config.go
@@ -3,6 +3,7 @@ package db
import (
"bytes"
"encoding/json"
+ "fmt"
"net/url"
"strings"
@@ -117,7 +118,50 @@ func ConfigAll() ([]byte, error) {
return val.Bytes(), nil
}
+// PutConfig updates a single k/v in the config
+func PutConfig(key string, value interface{}) error {
+ kv := make(map[string]interface{})
+
+ c, err := ConfigAll()
+ if err != nil {
+ return err
+ }
+
+ err = json.Unmarshal(c, &kv)
+ if err != nil {
+ return err
+ }
+
+ // set k/v from params to decoded map
+ kv[key] = value
+
+ data := make(url.Values)
+ for k, v := range kv {
+ switch v.(type) {
+ case string:
+ data.Set(k, v.(string))
+
+ case []string:
+ vv := v.([]string)
+ for i := range vv {
+ data.Add(k, vv[i])
+ }
+
+ default:
+ data.Set(k, fmt.Sprintf("%v", v))
+ }
+ }
+
+ err = SetConfig(data)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
// ConfigCache is a in-memory cache of the Configs for quicker lookups
+// 'key' is the JSON tag associated with the config field
func ConfigCache(key string) string {
return configCache.Get(key)
}
diff --git a/system/db/content.go b/system/db/content.go
index 87b3e69..535b601 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -10,9 +10,7 @@ import (
"strconv"
"strings"
- "github.com/bosssauce/ponzu/content"
- "github.com/bosssauce/ponzu/management/editor"
- "github.com/bosssauce/ponzu/management/manager"
+ "github.com/bosssauce/ponzu/system/item"
"github.com/boltdb/bolt"
"github.com/gorilla/schema"
@@ -305,7 +303,7 @@ func Query(namespace string, opts QueryOptions) (int, [][]byte) {
// 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
+ opts.Count = -1
}
if opts.Offset < 0 {
@@ -420,7 +418,7 @@ func SortContent(namespace string) {
// decode each (json) into type to then sort
for i := range all {
j := all[i]
- post := content.Types[namespace]()
+ post := item.Types[namespace]()
err := json.Unmarshal(j, &post)
if err != nil {
@@ -428,7 +426,7 @@ func SortContent(namespace string) {
return
}
- posts = append(posts, post.(editor.Sortable))
+ posts = append(posts, post.(item.Sortable))
}
// sort posts
@@ -469,7 +467,7 @@ func SortContent(namespace string) {
}
-type sortableContent []editor.Sortable
+type sortableContent []item.Sortable
func (s sortableContent) Len() int {
return len(s)
@@ -485,9 +483,9 @@ func (s sortableContent) Swap(i, j int) {
func postToJSON(ns string, data url.Values) ([]byte, error) {
// find the content type and decode values into it
- t, ok := content.Types[ns]
+ t, ok := item.Types[ns]
if !ok {
- return nil, fmt.Errorf(content.ErrTypeNotRegistered, ns)
+ return nil, fmt.Errorf(item.ErrTypeNotRegistered, ns)
}
post := t()
@@ -502,7 +500,7 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {
// if the content has no slug, and has no specifier, create a slug, check it
// for duplicates, and add it to our values
if data.Get("slug") == "" && data.Get("__specifier") == "" {
- slug, err := manager.Slug(post.(content.Identifiable))
+ slug, err := item.Slug(post.(item.Identifiable))
if err != nil {
return nil, err
}
@@ -512,7 +510,7 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {
return nil, err
}
- post.(content.Sluggable).SetSlug(slug)
+ post.(item.Sluggable).SetSlug(slug)
data.Set("slug", slug)
}
diff --git a/system/db/init.go b/system/db/init.go
index fa2c42e..9a0c3e5 100644
--- a/system/db/init.go
+++ b/system/db/init.go
@@ -4,8 +4,8 @@ import (
"encoding/json"
"log"
- "github.com/bosssauce/ponzu/content"
"github.com/bosssauce/ponzu/system/admin/config"
+ "github.com/bosssauce/ponzu/system/item"
"github.com/boltdb/bolt"
"github.com/nilslice/jwt"
@@ -32,7 +32,7 @@ func Init() {
err = store.Update(func(tx *bolt.Tx) error {
// initialize db with all content type buckets & sorted bucket for type
- for t := range content.Types {
+ for t := range item.Types {
_, err := tx.CreateBucketIfNotExists([]byte(t))
if err != nil {
return err
@@ -86,7 +86,7 @@ func Init() {
}
go func() {
- for t := range content.Types {
+ for t := range item.Types {
SortContent(t)
}
}()