CreateContact support for unregistered user
parent
7e30174ed3
commit
4db8c52237
|
@ -421,7 +421,7 @@ Add a new contact for the specified user.
|
|||
|
||||
| Name | Type | Description | Required |
|
||||
| ---- | ---- | ----------- | -------- |
|
||||
| id | String | New contact's ID. | ✓ |
|
||||
| phone_number | String | New contact's phone number. A blank user object will be created if no such ID exists in the database. | ✓ |
|
||||
|
||||
#### Success Response (200 OK)
|
||||
|
||||
|
|
28
handlers.go
28
handlers.go
|
@ -433,10 +433,13 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request,
|
|||
json.NewEncoder(w).Encode(users)
|
||||
}
|
||||
|
||||
type PhoneNumber struct {
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
}
|
||||
func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
// Parse
|
||||
userID := p.ByName("user")
|
||||
contact := User{}
|
||||
contact := PhoneNumber{}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&contact)
|
||||
if err != nil {
|
||||
|
@ -445,15 +448,33 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
|
|||
}
|
||||
|
||||
// Validate
|
||||
if len(contact.ID) < 1 || contact.ID == userID {
|
||||
if len(contact.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
|
||||
var contactId string
|
||||
err = h.db.QueryRow(`
|
||||
INSERT INTO "user" (id, first_name, last_name, phone_number)
|
||||
VALUES ($1, '', '', $2)
|
||||
ON CONFLICT(phone_number)
|
||||
DO UPDATE SET phone_number=EXCLUDED.phone_number
|
||||
RETURNING id
|
||||
`, id, contact.PhoneNumber).Scan(&contactId)
|
||||
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)
|
||||
`, userID, contactId)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
log.Print(err)
|
||||
|
@ -461,6 +482,7 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
|
|||
}
|
||||
|
||||
// Respond
|
||||
w.WriteHeader(200)
|
||||
//w.Header().Set("Content-Type", "application/json")
|
||||
//json.NewEncoder(w).Encode(contact)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue