-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_handlers.go
executable file
·136 lines (105 loc) · 3.65 KB
/
users_handlers.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
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
type UsersHandlers struct {
dbConnection *DBConnection
}
func (uHandlers *UsersHandlers) SignIn(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
email := r.FormValue("email")
password := r.FormValue("password")
source := r.FormValue("source")
user := &User{nick_name: name, email: email, password: password, source: source}
result := user.CreateNewUser(uHandlers.dbConnection)
if result {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
func (uHandlers *UsersHandlers) Login(w http.ResponseWriter, r *http.Request) {
email := r.FormValue("email")
password := r.FormValue("password")
user := &User{}
user = user.LoginWithCredentials(uHandlers.dbConnection, email, password)
if user != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
userMap := make(map[string]string)
userMap["token"] = user.id
if err := json.NewEncoder(w).Encode(userMap); err != nil {
panic(err)
}
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
func (uHandlers *UsersHandlers) UpdateProfile(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
firstName := r.FormValue("first_name")
lastName := r.FormValue("last_name")
tel1 := r.FormValue("tel1")
tel2 := r.FormValue("tel2")
imageName := r.FormValue("image_name")
timeNow := time.Now()
nanoTime := timeNow.UnixNano()
nanoInMillis := nanoTime / 100000
sha1Hash := sha1.New()
sha1Hash.Write([]byte(strconv.FormatInt(nanoInMillis, 10) + "" + token + "" + firstName + lastName + "" + imageName))
sha1HashString := sha1Hash.Sum(nil)
fileHash := fmt.Sprintf("%x", sha1HashString)
hashedFileName := fileHash + "" + imageName
user := &User{}
userDetails := &UserDetails{credentials_id: token, first_name: firstName, last_name: lastName, tel1: tel1, tel2: tel2, avatar: hashedFileName}
result := user.UpdateUserProfile(uHandlers.dbConnection, userDetails)
if result {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}
func (uHandlers *UsersHandlers) ReadUserProfile(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("token")
user := &User{}
userDetails := user.ReadUserProfile(uHandlers.dbConnection, token)
if userDetails != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
userMap := map[string]string{
"first_name": userDetails.first_name,
"last_name": userDetails.last_name,
"tel1": userDetails.tel1,
"tel2": userDetails.tel2,
"avatar": userDetails.avatar,
}
if err := json.NewEncoder(w).Encode(userMap); err != nil {
panic(err)
}
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}
}