-
Notifications
You must be signed in to change notification settings - Fork 6
/
controller.go
126 lines (116 loc) · 3.66 KB
/
controller.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
114
115
116
117
118
119
120
121
122
123
124
125
126
package banner_editor
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"github.com/qor/admin"
"github.com/qor/responder"
)
// New handle new setting page
func New(context *admin.Context) {
setting := context.Resource.NewStruct().(QorBannerEditorSettingInterface)
kind := context.Request.URL.Query().Get("kind")
if GetElement(kind) != nil {
setting.SetSerializableArgumentKind(kind)
} else {
context.AddError(fmt.Errorf("BannerEditor: It isn't any element match %v", kind))
}
context.Execute("new", setting)
}
// Create handle create setting
func Create(context *admin.Context) {
var (
res = context.Resource
result = res.NewStruct()
kind = context.Request.Form.Get("QorResource.Kind")
element = GetElement(kind)
)
if context.AddError(res.Decode(context.Context, result)); !context.HasError() {
context.AddError(res.CallSave(result, context.Context))
}
c := element.Context(context, result)
html, err := render(element.Template, c)
if err != nil {
context.AddError(fmt.Errorf("BannerEditor: can't parse %v's template, got %v", kind, err))
}
if context.HasError() {
responder.With("html", func() {
context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
context.Execute("new", result)
}).With([]string{"json"}, func() {
context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
context.Encode("index", map[string]interface{}{"errors": context.GetErrors()})
}).Respond(context.Request)
} else {
responder.With("html", func() {
context.Flash(string(res.GetAdmin().T(context.Context, "qor_admin.form.successfully_created", "{{.Name}} was successfully created", res)), "success")
http.Redirect(context.Writer, context.Request, context.URLFor(result, res), http.StatusFound)
}).With([]string{"json"}, func() {
json, err := settingJSON(html, result)
if err != nil {
panic(err)
}
context.Writer.Write(json)
}).Respond(context.Request)
}
}
// Update handle update setting
func Update(context *admin.Context) {
var (
res = context.Resource
kind = context.Request.Form.Get("QorResource.Kind")
element = GetElement(kind)
html template.HTML
err error
)
result, err := context.FindOne()
if err != nil {
context.AddError(err)
} else {
if context.AddError(res.Decode(context.Context, result)); !context.HasError() {
context.AddError(res.CallSave(result, context.Context))
}
c := element.Context(context, result)
html, err = render(element.Template, c)
if err != nil {
context.AddError(fmt.Errorf("BannerEditor: can't parse %v's template, got %v", kind, err))
}
}
if context.HasError() {
context.Writer.WriteHeader(admin.HTTPUnprocessableEntity)
responder.With("html", func() {
context.Execute("edit", result)
}).With([]string{"json", "xml"}, func() {
context.Encode("edit", map[string]interface{}{"errors": context.GetErrors()})
}).Respond(context.Request)
} else {
responder.With("html", func() {
context.Flash(string(res.GetAdmin().T(context.Context, "qor_admin.form.successfully_updated", "{{.Name}} was successfully updated", res)), "success")
context.Execute("show", result)
}).With([]string{"json"}, func() {
json, err := settingJSON(html, result)
if err != nil {
panic(err)
}
context.Writer.Write(json)
}).Respond(context.Request)
}
}
func settingJSON(html template.HTML, result interface{}) ([]byte, error) {
jsonValue := &bytes.Buffer{}
encoder := json.NewEncoder(jsonValue)
encoder.SetEscapeHTML(false)
err := encoder.Encode(struct {
ID uint
Template template.HTML
}{
ID: result.(QorBannerEditorSettingInterface).GetID(),
Template: html,
})
if err != nil {
return nil, err
}
return jsonValue.Bytes(), nil
}