-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_controller.go
More file actions
109 lines (95 loc) · 2.2 KB
/
Copy pathuser_controller.go
File metadata and controls
109 lines (95 loc) · 2.2 KB
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
package main
import (
"log"
"net/http"
)
/**
*
*
*/
func userHandlerEdit(w http.ResponseWriter, r *http.Request) {
user := RequestUser(r)
RenderTemplate(w, r, "users/edit", map[string]interface{}{
"User": user,
})
}
/**
*
*
*/
func userHandlerUpdate(w http.ResponseWriter, r *http.Request) {
currentUser := RequestUser(r)
email := r.FormValue("email")
currentPassword := r.FormValue("currentPassword")
newPassword := r.FormValue("newPassword")
firstName := r.FormValue("firstName")
log.Println(firstName)
lastName := r.FormValue("lastName")
log.Println(lastName)
user, err := UpdateUser(currentUser,
email,
currentPassword,
newPassword,
firstName,
lastName)
if err != nil {
if IsValidationError(err) {
RenderTemplate(w, r, "users/edit", map[string]interface{}{
"Error": err.Error(),
"User": user,
})
return
}
panic(err)
}
err = globalUserStore.Save(*currentUser)
if err != nil {
panic(err)
}
http.Redirect(w, r, "/account?flash=User+Updated", http.StatusFound)
}
/********************************************************
* Registration Handler
* Used to render the registration view/page
*/
func registrationHandlerGET(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, r, "users/new", nil)
}
/********************************************************
* Registration Handler
* Used to save the registration information
*/
func registrationHandlerPOST(w http.ResponseWriter, r *http.Request) {
//create user from form information
user, err := NewUser(
r.FormValue("username"),
r.FormValue("email"),
r.FormValue("password"))
//error checking for user created
if err != nil {
if IsValidationError(err) {
log.Println(err.Error())
RenderTemplate(w, r, "/users/new", map[string]interface{}{
"Error": err.Error(),
"User": user,
})
panic(err)
return
}
}
//(Attemp to)Save the user
err = globalUserStore.Save(user)
if err != nil {
panic(err)
return
}
//Create a session for newly created user
session := NewSession(w)
session.UserID = user.ID
err = globalSessionStore.Save(session)
if err != nil {
panic(err)
}
//Redirect user to account view/page
http.Redirect(w, r, "/account?flash=User+created", http.StatusFound)
}