summaryrefslogtreecommitdiff
path: root/post.go
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-09-16 20:24:11 -0700
committerSteve Manuel <nilslice@gmail.com>2016-09-16 20:24:11 -0700
commitd62f31d1932125db59c4cf813c54d95a4a0200ee (patch)
tree038450d2401cf2475eca6383b15967752f85cc30 /post.go
added basic rendering of editor view for `Editable` data
Diffstat (limited to 'post.go')
-rw-r--r--post.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/post.go b/post.go
new file mode 100644
index 0000000..3ae0b92
--- /dev/null
+++ b/post.go
@@ -0,0 +1,93 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "net/http"
+
+ "github.com/nilslice/cms/editor"
+)
+
+// Post is the generic content struct
+type Post struct {
+ editor editor.Editor
+
+ Title []byte `json:"title"`
+ Content []byte `json:"content"`
+ Author []byte `json:"author"`
+ Timestamp []byte `json:"timestamp"`
+}
+
+// Editor partially implements editor.Editable
+func (p *Post) Editor() *editor.Editor {
+ return &p.editor
+}
+
+// NewViewBuffer partially implements editor.Editable
+func (p *Post) NewViewBuffer() {
+ p.editor.ViewBuf = &bytes.Buffer{}
+}
+
+// Render partially implements editor.Editable
+func (p *Post) Render() []byte {
+ return p.editor.ViewBuf.Bytes()
+}
+
+// EditView writes a buffer of html to edit a Post
+func (p Post) EditView() ([]byte, error) {
+ view, err := editor.New(&p,
+ editor.Field{
+ View: editor.Input("Title", &p, map[string]string{
+ "label": "Post Title",
+ "type": "text",
+ "placeholder": "Enter your Post Title here",
+ }),
+ },
+ editor.Field{
+ View: editor.Textarea("Content", &p, map[string]string{
+ "label": "Content",
+ "placeholder": "Add the content of your post here",
+ }),
+ },
+ editor.Field{
+ View: editor.Input("Author", &p, map[string]string{
+ "label": "Author",
+ "type": "text",
+ "placeholder": "Enter the author name here",
+ }),
+ },
+ editor.Field{
+ View: editor.Input("Timestamp", &p, map[string]string{
+ "label": "Publish Date",
+ "type": "date",
+ }),
+ },
+ )
+
+ if err != nil {
+ return nil, fmt.Errorf("Failed to render Post editor view: %s", err.Error())
+ }
+
+ return view, nil
+}
+
+func (p Post) ServeHTTP(res http.ResponseWriter, req *http.Request) {
+ res.Header().Set("Content-type", "text/html")
+ resp, err := p.EditView()
+ if err != nil {
+ fmt.Println(err)
+ }
+ res.Write(resp)
+}
+
+func main() {
+ p := Post{
+ Content: []byte("<h3>H</h3>ello. My name is <em>Steve</em>."),
+ Title: []byte("Profound introduction"),
+ Author: []byte("Steve Manuel"),
+ Timestamp: []byte("2016-09-16"),
+ }
+
+ http.ListenAndServe(":8080", p)
+
+}