This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
200 lines (171 loc) · 5.16 KB
/
user.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
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Parse
user := User{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&user)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Validate
phone, err := ParsePhone(user.PhoneNumber)
if err != nil || len(user.FirstName) < 1 || len(user.LastName) < 1 {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
user.PhoneNumber = phone // shouldn't be needed but makes life easier
// Generate ID
id := "u-" + RandomHex()
user.ID = id
// Log
log.Print(user)
// Insert
var finalId string
err = h.db.QueryRow(`
INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number, username=$2, first_name=$5, last_name=$6
RETURNING id
`, user.ID, user.Username, user.Bio, user.ProfilePic, user.FirstName, user.LastName, user.PhoneNumber).Scan(&finalId)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
user.ID = finalId
// Publish NATs
if h.nc != nil {
userString, err := json.Marshal(&user)
if err == nil {
updateMsg := UpdateMsg{
Type: "add",
Data: string(userString),
}
updateMsgString, err := json.Marshal(&updateMsg)
if err == nil {
h.nc.Publish("user", updateMsgString)
}
}
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
func (h *Handler) GetUserByPhone(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Parse
phone, err := ParsePhone(r.FormValue("phone_number"))
// Validate
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Response object
user := User{}
// Select
err = h.db.QueryRow(`
SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user" WHERE phone_number = $1
`, phone).Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := p.ByName("user")
// Response object
user := User{}
// Select
err := h.db.QueryRow(`
SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user" WHERE id = $1
`, userID).Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch {
case err == sql.ErrNoRows:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
case err != nil:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
func (h *Handler) GetUserByUsername(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
username := p.ByName("username")
// Response object
user := User{}
// Select
err := h.db.QueryRow(`
SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user" WHERE username = $1
`, username).Scan(&user.ID, &user.Username, &user.Bio, &user.ProfilePic, &user.FirstName, &user.LastName, &user.PhoneNumber)
switch {
case err == sql.ErrNoRows:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
case err != nil:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(user)
}
func (h *Handler) UpdateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)
user := User{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&user)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Update
_, err = h.db.Exec(`
UPDATE "user"
SET
username = $2,
bio = $3,
profile_pic = $4,
first_name = $5,
last_name = $6
WHERE id = $1
`, userID, user.Username, user.Bio, user.ProfilePic, user.FirstName, user.LastName)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Publish NATs
if h.nc != nil {
userString, err := json.Marshal(&user)
if err == nil {
updateMsg := UpdateMsg{
Type: "update",
Data: string(userString),
}
updateMsgString, err := json.Marshal(&updateMsg)
if err == nil {
h.nc.Publish("user", updateMsgString)
}
}
}
w.WriteHeader(200)
}