-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
113 lines (99 loc) · 3.06 KB
/
counter.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"github.com/mbertschler/guiapi"
"github.com/mbertschler/html"
"github.com/mbertschler/html/attr"
)
type Counter struct {
*DB
}
func (c *Counter) Register(s *guiapi.Server) {
s.AddPage("/counter", c.RenderPage)
s.AddAction("Counter.Increase", c.Increase)
s.AddAction("Counter.Decrease", c.Decrease)
}
type CounterPage struct {
Content html.Block
}
func (c *CounterPage) WriteHTML(w io.Writer) error {
block := html.Blocks{
html.Doctype("html"),
html.Html(nil,
html.Head(nil,
html.Meta(attr.Charset("utf-8")),
html.Title(nil, html.Text("Guiapi Counter Example")),
html.Link(attr.Rel("stylesheet").Href("https://cdn.jsdelivr.net/npm/[email protected]/simple.min.css")),
html.Link(attr.Rel("stylesheet").Href("/dist/bundle.css")),
),
html.Body(nil,
html.Main(attr.Id("page"), c.Content),
html.Hr(nil),
html.A(attr.Href("/"), html.Text("TodoMVC Example")),
html.A(attr.Href("/reports"), html.Text("Reports Example")),
html.Script(attr.Src("/dist/bundle.js")),
),
),
}
return html.RenderMinified(w, block)
}
func (c *CounterPage) Update() (*guiapi.Update, error) {
out, err := html.RenderMinifiedString(c.Content)
return guiapi.ReplaceContent("#page", out), err
}
func (c *Counter) RenderPage(ctx *guiapi.PageCtx) (guiapi.Page, error) {
block, err := c.RenderBlock(ctx)
if err != nil {
return nil, err
}
main := html.Blocks{
html.H1(nil, html.Text("guiapi")),
html.P(nil, html.Text("guiapi is a framework for building web applications in Go.")),
block,
}
return &CounterPage{Content: main}, nil
}
func (c *Counter) RenderBlock(ctx *guiapi.PageCtx) (html.Block, error) {
sess := c.DB.Session(ctx.Writer, ctx.Request)
counter, err := c.DB.GetCounter(sess.ID)
if err != nil {
return nil, err
}
block := html.Div(attr.Id("counter"),
html.H3(nil, html.Text("Counter")),
html.P(attr.Id("count"), html.Text(fmt.Sprintf("Current count: %d", counter.Count))),
html.Button(attr.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Decrease"), html.Text("-")),
html.Text(" "),
html.Button(attr.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Increase"), html.Text("+")),
)
return block, nil
}
func (c *Counter) Increase(ctx *guiapi.ActionCtx) (*guiapi.Update, error) {
sess := c.DB.Session(ctx.Writer, ctx.Request)
counter, err := c.DB.GetCounter(sess.ID)
if err != nil {
return nil, err
}
counter.Count++
err = c.DB.SetCounter(counter)
if err != nil {
return nil, err
}
out, err := html.RenderMinifiedString(html.Text(fmt.Sprintf("Current count: %d", counter.Count)))
return guiapi.ReplaceContent("#count", out), err
}
func (c *Counter) Decrease(ctx *guiapi.ActionCtx) (*guiapi.Update, error) {
sess := c.DB.Session(ctx.Writer, ctx.Request)
counter, err := c.DB.GetCounter(sess.ID)
if err != nil {
return nil, err
}
counter.Count--
err = c.DB.SetCounter(counter)
if err != nil {
return nil, err
}
out, err := html.RenderMinifiedString(html.Text(fmt.Sprintf("Current count: %d", counter.Count)))
return guiapi.ReplaceContent("#count", out), err
}