diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:05:07 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:05:07 -0800 |
commit | d4b94e4b37bb4b28dae2a91bf4a9872e0a801c1a (patch) | |
tree | 7b00da4e19fd39293fd2c5dfd4e0e5818e6bcfed | |
parent | f680588370706affb2ef98ba2a93d93fb5284683 (diff) |
moving reference into content package and testing API based approach
-rw-r--r-- | cmd/ponzu/options.go | 2 | ||||
-rw-r--r-- | content/reference.go | 57 | ||||
-rw-r--r-- | management/reference/reference.go | 35 | ||||
-rw-r--r-- | system/api/server.go | 22 |
4 files changed, 80 insertions, 36 deletions
diff --git a/cmd/ponzu/options.go b/cmd/ponzu/options.go index 8b62b1a..6384b47 100644 --- a/cmd/ponzu/options.go +++ b/cmd/ponzu/options.go @@ -248,7 +248,7 @@ func buildPonzuServer(args []string) error { // copy all ./content files to internal vendor directory src := "content" dst := filepath.Join("cmd", "ponzu", "vendor", "github.com", "bosssauce", "ponzu", "content") - err = copyFilesWarnConflicts(src, dst, []string{"item.go", "types.go"}) + err = copyFilesWarnConflicts(src, dst, []string{"item.go", "types.go", "references.go"}) if err != nil { return err } diff --git a/content/reference.go b/content/reference.go new file mode 100644 index 0000000..0853b3d --- /dev/null +++ b/content/reference.go @@ -0,0 +1,57 @@ +package content + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/bosssauce/ponzu/management/editor" + "github.com/bosssauce/ponzu/system/api" +) + +// Referenceable enures there is a way to reference the implenting type from +// within another type's editor and from type-scoped API calls +type Referenceable interface { + Referenced() []byte +} + +// Select returns the []byte of a <select> HTML element plus internal <options> with a label. +// 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 := 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() + + // 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) + } + + // make sure it is an Identifiable + item, ok := t.(Identifiable) + if !ok { + log.Println("Cannot use type", contentType, "as a reference since it does not implement Identifiable") + return nil + } + + k := fmt.Sprintf("?type=%s&id=%d", contentType, item.ItemID()) + v := item.String() + options[k] = v + } + + return editor.Select(fieldName, p, attrs, options) +} diff --git a/management/reference/reference.go b/management/reference/reference.go deleted file mode 100644 index e6d7edd..0000000 --- a/management/reference/reference.go +++ /dev/null @@ -1,35 +0,0 @@ -package reference - -import ( - "encoding/json" - "fmt" - "log" - - "github.com/bosssauce/ponzu/management/editor" - "github.com/bosssauce/ponzu/system/db" -) - -// Select returns the []byte of a <select> HTML element plus internal <options> with a label. -// 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, display string) []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") - - data := make(map[string]interface{}) - for i := range jj { - err := json.Unmarshal(jj[i], data) - if err != nil { - log.Println("Error decoding into reference handle:", contentType, err) - } - - k := fmt.Sprintf("?type=%s&id=%d", contentType, data["id"].(int)) - v := data[display].(string) - options[k] = v - } - - return editor.Select(fieldName, p, attrs, options) -} diff --git a/system/api/server.go b/system/api/server.go index 823ec16..7254a7b 100644 --- a/system/api/server.go +++ b/system/api/server.go @@ -1,6 +1,9 @@ package api import ( + "bytes" + "fmt" + "log" "net/http" ) @@ -14,3 +17,22 @@ func Run() { http.HandleFunc("/api/content/external", CORS(Record(externalContentHandler))) } + +// ContentAll retrives all items from the HTTP API within the provided namespace +func ContentAll(namespace string) [][]bytes { + endpoint := "http://0.0.0.0:8080/api/contents?type=" + buf := []byte{} + r := bytes.NewReader(buf) + req, err := http.NewRequest(http.MethodGet, endpoint+namespace, r) + if err != nil { + log.Println("Error creating request for reference from:", contentType) + return nil + } + + c := http.Client{} + res, err := c.Do(req) + + fmt.Println(res, string(buf)) + + return []byte{buf} +} |