summaryrefslogtreecommitdiff
path: root/content/post.go
blob: b12eb1d72c825138333e851b73437b3d257a6cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package content

import (
	"fmt"

	"github.com/nilslice/cms/management/editor"
)

// Post is the generic content struct
type Post struct {
	Item
	editor editor.Editor

	Title     string `json:"title"`
	Content   string `json:"content"`
	Author    string `json:"author"`
	Timestamp string `json:"timestamp"`
}

func init() {
	Types["Post"] = func() interface{} { return new(Post) }
}

// SetContentID partially implements editor.Editable
func (p *Post) SetContentID(id int) { p.ID = id }

// ContentID partially implements editor.Editable
func (p *Post) ContentID() int { return p.ID }

// ContentName partially implements editor.Editable
func (p *Post) ContentName() string { return p.Title }

// SetSlug partially implements editor.Editable
func (p *Post) SetSlug(slug string) { p.Slug = slug }

// Editor partially implements editor.Editable
func (p *Post) Editor() *editor.Editor { return &p.editor }

// MarshalEditor writes a buffer of html to edit a Post and partially implements editor.Editable
func (p *Post) MarshalEditor() ([]byte, error) {
	view, err := editor.Form(p,
		editor.Field{
			View: editor.Input("Slug", p, map[string]string{
				"label":       "URL Path",
				"type":        "text",
				"disabled":    "true",
				"placeholder": "Will be set automatically",
			}),
		},
		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
}