-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
243 lines (212 loc) · 6.23 KB
/
edit.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"log"
"net/http"
"github.com/colin353/markdown.ninja/models"
"github.com/colin353/markdown.ninja/requesthandler"
)
// NewEditHandler returns an instance of the edit handler, with
// the routes populated.
func NewEditHandler() *requesthandler.GenericRequestHandler {
a := requesthandler.GenericRequestHandler{}
a.RouteMap = map[string]requesthandler.Responder{
"page": page,
"pages": pages,
"create_page": createPage,
"edit_page": editPage,
"rename_page": renamePage,
"delete_page": deletePage,
"set_style": setStyle,
"get_style": getStyle,
}
return &a
}
// Create a new page. If that page already exists, will return an error.
func createPage(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type createArgs struct {
Markdown string `json:"markdown"`
HTML string `json:"html"`
}
args := createArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Create a new instance of the page object.
p := models.Page{
Markdown: args.Markdown,
HTML: args.HTML,
Domain: u.Domain,
}
err = p.GenerateName()
if err != nil {
log.Printf("Tried to create a new page, but couldn't make a unique name. (tried %s)", p.Key())
http.Error(w, "", http.StatusBadRequest)
}
err = models.Insert(&p)
if err != nil {
log.Printf("Tried to create a new page called `%s`, but encountered an error.", p.Key())
http.Error(w, "", http.StatusBadRequest)
}
return p.Export()
}
func editPage(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type editArgs struct {
Name string `json:"name"`
Markdown string `json:"markdown"`
HTML string `json:"html"`
}
args := editArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Load the old version of the page.
p := models.Page{}
p.Domain = u.Domain
p.Name = args.Name
err = models.Load(&p)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Update the data and save it.
p.Markdown = args.Markdown
p.HTML = args.HTML
err = models.Save(&p)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
return requesthandler.ResponseOK
}
// Rename an existing page to a new name.
func renamePage(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type renameArgs struct {
OldName string `json:"old_name"`
NewName string `json:"new_name"`
}
args := renameArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Get the old version of the page.
p := models.Page{}
p.Domain = u.Domain
p.Name = args.OldName
err = models.Load(&p)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Rename that page.
err = p.RenamePage(args.NewName)
// The most common reason this fails is because of validation
// failure because an invalid name was provided.
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
return requesthandler.ResponseOK
}
// This function searches for a specific page, and returns it.
func page(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type pageArgs struct {
Name string `json:"name"`
}
args := pageArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Create a page object to search with.
p := models.Page{}
p.Domain = u.Domain
p.Name = args.Name
err = models.Load(&p)
if err != nil {
http.Error(w, "", http.StatusNotFound)
return requesthandler.ResponseError
}
// Return the page.
return p.Export()
}
// Return a list of pages belonging to that user.
func pages(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
// Create a page object and use it to search for its own siblings.
p := models.Page{}
p.Domain = u.Domain
iterator, err := models.GetList(&p)
if err != nil {
log.Printf("Tried to load pages under `%s`, but it failed.", p.RegistrationKey())
return requesthandler.ResponseError
}
pageList := make([]map[string]interface{}, 0, iterator.Count())
for iterator.Next() {
pageList = append(pageList, iterator.Value().Export())
}
return pageList
}
// Delete a page.
func deletePage(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type deleteArgs struct {
Name string `json:"name"`
}
args := deleteArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// First, try to load the page.
p := models.Page{}
p.Domain = u.Domain
p.Name = args.Name
err = models.Load(&p)
if err != nil {
log.Printf("Tried to delete existing page `%s`", p.Key())
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
// Now, delete the page.
err = models.Delete(&p)
if err != nil {
log.Printf("Failed to delete page `%s`", p.Key())
http.Error(w, "", http.StatusInternalServerError)
return requesthandler.ResponseError
}
return requesthandler.ResponseOK
}
// Set the global style for that user's website.
func setStyle(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type styleArgs struct {
Style string `json:"style"`
}
args := styleArgs{}
err := requesthandler.ParseArguments(r, &args)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return requesthandler.ResponseInvalidArgs
}
log.Printf("Set style: %s", args.Style)
u.Style = args.Style
err = models.Save(u)
if err != nil {
log.Printf("Failed to set style for `%s`", u.Key())
http.Error(w, "", http.StatusInternalServerError)
return requesthandler.ResponseError
}
return requesthandler.ResponseOK
}
// Get the style setting for this user.
func getStyle(u *models.User, w http.ResponseWriter, r *http.Request) interface{} {
type styleResponse struct {
Style string `json:"style"`
}
return styleResponse{u.Style}
}