/
page_setup.go
146 lines (128 loc) · 3.26 KB
/
page_setup.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
package main
import (
"github.com/gorilla/schema"
"github.com/jbaikge/ingress-inventory/communities"
db "github.com/jbaikge/ingress-inventory/mongo"
"github.com/jbaikge/ingress-inventory/parser"
"github.com/jbaikge/ingress-inventory/profile"
"log"
"net/http"
)
type setupExtra struct {
Errors map[string]string
}
var setupDecoder *schema.Decoder
func init() {
router.HandleFunc("/setup", HandleSetup)
router.HandleFunc("/setupThanks", HandleSetupThanks)
setupDecoder = schema.NewDecoder()
}
func HandleSetup(w http.ResponseWriter, r *http.Request) {
// Grab Profile from cookie set in HandleLoginOAuth
p := profile.Profile{}
if cookie, err := r.Cookie("Profile"); err == nil {
if err = sCookie.Decode(cookie.Name, cookie.Value, &p); err != nil {
log.Print(err)
}
}
// Make sure we got the profile out of the cookie
if p.GoogleId == "" {
http.Redirect(w, r, "/cannotGetCookie", http.StatusTemporaryRedirect)
return
}
p.Communities = communities.All()
e := newExtra()
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
log.Println(err)
}
// Fill the profile communities with all, filter out falses later
if err := setupDecoder.Decode(&p, r.PostForm); err != nil {
log.Println(err)
}
if err := validateUsername(&p); err != "" {
e.Errors["DisplayUsername"] = err
}
if err := validateCommunities(&p); err != "" {
e.Errors["Communities"] = err
}
// Insert new profile
if len(e.Errors) == 0 {
if err := db.SaveProfile(&p); err != nil {
e.Errors["Overall"] = err.Error()
}
}
if len(e.Errors) == 0 {
encoded, err := sCookie.Encode("Profile", p)
if err != nil {
log.Print(err)
http.Redirect(w, r, "/cannotSetCookie", http.StatusTemporaryRedirect)
return
}
http.SetCookie(w, &http.Cookie{
Name: "Profile",
Value: encoded,
Path: "/",
})
http.Redirect(w, r, "/setupThanks", http.StatusTemporaryRedirect)
return
}
}
ctx := &parser.Context{
Title: "Setup Your Account",
Description: "Establish your account details",
Profile: p,
Extra: e,
}
if err := parser.Render(w, ctx, "setup.html"); err != nil {
log.Println(err)
}
}
func HandleSetupThanks(w http.ResponseWriter, r *http.Request) {
// Grab Profile from cookie set in HandleLoginOAuth
p := profile.Profile{}
if cookie, err := r.Cookie("Profile"); err == nil {
if err = sCookie.Decode(cookie.Name, cookie.Value, &p); err != nil {
log.Print(err)
}
}
ctx := &parser.Context{
Title: "Thank You for Setting Up Your Account",
Description: "",
Profile: p,
}
if err := parser.Render(w, ctx, "setup-thanks.html"); err != nil {
log.Println(err)
}
}
func newExtra() (e setupExtra) {
return setupExtra{
Errors: map[string]string{},
}
}
func validateUsername(p *profile.Profile) (e string) {
if p.DisplayUsername == "" {
return "Please provide your username"
}
reg, err := db.UsernameRegistered(p.DisplayUsername)
if err != nil {
log.Print(err)
return "Could not verify username"
}
if reg {
return "Username already registered"
}
return
}
func validateCommunities(p *profile.Profile) (e string) {
var onePicked bool
for _, c := range p.Communities {
if c.Selected {
onePicked = true
}
}
if !onePicked {
return "Please select at least one community"
}
return
}