summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-12-17 06:39:43 -0800
committerSteve Manuel <nilslice@gmail.com>2016-12-17 06:39:43 -0800
commit547c1bb8d9240ff5a531890bda193ce0a17edff1 (patch)
treec7cd108f04a2607a6e44766d1736d4c189534c56
parent8b9d19d5a7212d3e0d493ad6a0570434f9049b43 (diff)
editing server and reference
-rw-r--r--addons/reference/reference.go42
-rw-r--r--system/api/server.go6
2 files changed, 19 insertions, 29 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go
index 6a46e75..48a9748 100644
--- a/addons/reference/reference.go
+++ b/addons/reference/reference.go
@@ -1,11 +1,11 @@
package api
import (
+ "bytes"
"encoding/json"
"fmt"
- "log"
+ "text/template"
- "github.com/bosssauce/ponzu/content"
"github.com/bosssauce/ponzu/management/editor"
"github.com/bosssauce/ponzu/system/api"
)
@@ -20,38 +20,28 @@ type Referenceable interface {
// IMPORTANT:
// The `fieldName` argument will cause a panic if it is not exactly the string
// form of the struct field that this editor input is representing
-func Select(fieldName string, p interface{}, attrs map[string]string, contentType string) []byte {
- ct, ok := content.Types[contentType]
- if !ok {
- log.Println("Cannot reference an invalid content type:", contentType)
- return nil
- }
-
- // get a handle to the underlying interface type for decoding
- t := ct()
-
+func Select(fieldName string, p interface{}, attrs map[string]string, contentType string, tmpl template.Template) []byte {
// decode all content type from db into options map
// map["?type=<contentType>&id=<id>"]t.String()
options := make(map[string]string)
- // jj := db.ContentAll(contentType + "__sorted") // make this an API call
- jj := api.ContentAll(contentType)
- for i := range jj {
- err := json.Unmarshal(jj[i], t)
- if err != nil {
- log.Println("Error decoding into reference handle:", contentType, err)
- }
+ var data []map[string]interface{}
+ j := api.ContentAll(contentType)
- // make sure it is a content.Identifiable
- item, ok := t.(content.Identifiable)
- if !ok {
- log.Println("Cannot use type", contentType, "as a reference since it does not implement content.Identifiable")
+ err := json.Unmarshal(j, data)
+ if err != nil {
+ return nil
+ }
+
+ for i := range data {
+ k := fmt.Sprintf("?type=%s&id=%s", contentType, data[i]["id"].(string))
+ v := &bytes.Buffer{}
+ err := tmpl.Execute(v, data[i])
+ if err != nil {
return nil
}
- k := fmt.Sprintf("?type=%s&id=%d", contentType, item.ItemID())
- v := item.String()
- options[k] = v
+ options[k] = v.String()
}
return editor.Select(fieldName, p, attrs, options)
diff --git a/system/api/server.go b/system/api/server.go
index c6d29bf..d663fe4 100644
--- a/system/api/server.go
+++ b/system/api/server.go
@@ -19,7 +19,7 @@ func Run() {
}
// ContentAll retrives all items from the HTTP API within the provided namespace
-func ContentAll(namespace string) [][]byte {
+func ContentAll(namespace string) []byte {
endpoint := "http://0.0.0.0:8080/api/contents?type="
buf := []byte{}
r := bytes.NewReader(buf)
@@ -31,9 +31,9 @@ func ContentAll(namespace string) [][]byte {
c := http.Client{}
res, err := c.Do(req)
+ defer res.Body.Close()
fmt.Println(res, string(buf))
- ret := [][]byte{}
- return append(ret, buf)
+ return buf
}