-
Notifications
You must be signed in to change notification settings - Fork 2
/
forms_handler.go
158 lines (129 loc) · 4.54 KB
/
forms_handler.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/flosch/pongo2"
"github.com/gorilla/mux"
"github.com/plentiform/plentiform/models"
repo "github.com/plentiform/plentiform/repositories"
)
func (app *Application) FormsIndexHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
forms, _ := repo.NewFormsRepository(app.db).FindByUserId(currentUser.Id)
app.Render(w, r, "forms/index", pongo2.Context{
"forms": forms,
})
}
func (app *Application) FormsNewHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
app.Render(w, r, "forms/new", pongo2.Context{})
}
func (app *Application) FormsCreateHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
recaptchaSecretKey := r.PostFormValue("recaptcha_secret_key")
description := r.PostFormValue("description")
form := new(models.Form)
form.Name = r.PostFormValue("name")
if len(description) > 0 {
form.Description = &description
} else {
form.Description = nil
}
if len(recaptchaSecretKey) > 0 {
form.RecaptchaSecretKey = &recaptchaSecretKey
} else {
form.RecaptchaSecretKey = nil
}
_, err := repo.NewFormsRepository(app.db).Create(currentUser.Id, form.Name, form.Description, form.RecaptchaSecretKey)
if err != nil {
session.AddFlash("An error occured while creating your form")
session.Save(r, w)
app.Render(w, r, "forms/new", pongo2.Context{"form": form})
return
}
session.AddFlash("Successfully created form!")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
}
func (app *Application) FormsShowHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
id, _ := strconv.Atoi(mux.Vars(r)["id"])
form, _ := repo.NewFormsRepository(app.db).FindById(id)
submissions, _ := repo.NewSubmissionsRepository(app.db).FindByFormId(form.Id)
if !currentUser.CanView(form) {
session.AddFlash("You are not authorized to access this resource.")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
return
}
app.Render(w, r, "forms/show", pongo2.Context{
"form": form,
"submissions": submissions,
})
}
func (app *Application) FormsEditHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
id, _ := strconv.Atoi(mux.Vars(r)["id"])
form, _ := repo.NewFormsRepository(app.db).FindById(id)
if !currentUser.CanUpdate(form) {
session.AddFlash("You are not authorized to access this resource.")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
return
}
app.Render(w, r, "forms/edit", pongo2.Context{"form": form})
}
func (app *Application) FormsUpdateHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
id, _ := strconv.Atoi(mux.Vars(r)["id"])
form, err := repo.NewFormsRepository(app.db).FindById(id)
if !currentUser.CanUpdate(form) {
session.AddFlash("You are not authorized to access this resource.")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
return
}
form.Name = r.PostFormValue("name")
description := r.PostFormValue("description")
if len(description) > 0 {
form.Description = &description
} else {
form.Description = nil
}
recaptchaSecretKey := r.PostFormValue("recaptcha_secret_key")
if len(recaptchaSecretKey) > 0 {
form.RecaptchaSecretKey = &recaptchaSecretKey
} else {
form.RecaptchaSecretKey = nil
}
_, err = repo.NewFormsRepository(app.db).Update(id, form.Name, form.Description, form.RecaptchaSecretKey)
if err != nil {
session.AddFlash("An error occured while updating this form")
session.Save(r, w)
app.Render(w, r, "forms/edit", pongo2.Context{"form": form})
return
}
session.AddFlash("Successfully updated form!")
session.Save(r, w)
http.Redirect(w, r, fmt.Sprintf("/forms/%d", form.Id), 302)
}
func (app *Application) FormsDestroyHandler(w http.ResponseWriter, r *http.Request, currentUser *models.User) {
session, _ := app.GetSession(r)
id, _ := strconv.Atoi(mux.Vars(r)["id"])
form, _ := repo.NewFormsRepository(app.db).FindById(id)
if !currentUser.CanDelete(form) {
session.AddFlash("You are not authorized to access this resource.")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
return
}
_, err := repo.NewFormsRepository(app.db).Delete(form.Id)
if err != nil {
session.AddFlash("An error occured while deleting this form")
session.Save(r, w)
app.Render(w, r, "forms/edit", pongo2.Context{"form": form})
return
}
session.AddFlash("Successfully deleted form!")
session.Save(r, w)
http.Redirect(w, r, "/forms", 302)
}