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 pathcontact.go
116 lines (102 loc) · 3.17 KB
/
contact.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)
contactPhone := PhoneNumber{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&contactPhone)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Validate
phone, err := ParsePhone(contactPhone.PhoneNumber)
if err != nil || len(contactPhone.PhoneNumber) < 1 {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// Generate ID (just in case)
id := "u-" + RandomHex()
// Create contact if not exists, returning the id regardless
contact := User{}
err = h.db.QueryRow(`
INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, '', '', '', '', '', $2)
ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number
RETURNING id, username, bio, profile_pic, first_name, last_name, phone_number
`, id, phone).Scan(&contact.ID, &contact.Username, &contact.Bio, &contact.ProfilePic, &contact.FirstName, &contact.LastName, &contact.PhoneNumber)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Insert
_, err = h.db.Exec(`
INSERT INTO contact ("user", contact) VALUES ($1, $2)
`, userID, contact.ID)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
// Publish NATs
if h.nc != nil {
contact := Contact{
UserA: userID,
UserB: contact.ID,
}
contactString, err := json.Marshal(&contact)
if err == nil {
updateMsg := UpdateMsg{
Type: "add",
Data: string(contactString),
}
updateMsgString, err := json.Marshal(&updateMsg)
if err == nil {
h.nc.Publish("contact", updateMsgString)
}
}
}
// Respond
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(contact)
}
func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse
userID := r.Context().Value("user").(string)
// Response object
contacts := make([]User, 0)
// Select
rows, err := h.db.Query(`
SELECT id, username, bio, profile_pic, first_name, last_name, phone_number FROM "user"
INNER JOIN contact
ON contact.contact = "user".id AND contact.user = $1
`, userID)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
defer rows.Close()
// Scan
for rows.Next() {
contact := User{}
if err := rows.Scan(&contact.ID, &contact.Username, &contact.Bio, &contact.ProfilePic, &contact.FirstName, &contact.LastName, &contact.PhoneNumber); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err)
return
}
contacts = append(contacts, contact)
}
// Respond
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(contacts)
}