4
1
Fork 0

Fixes #6 Added ON CONFLICT clause to INSERT in CreateUser

pull/24/head
UnicodingUnicorn 2019-06-14 05:30:32 +08:00
parent a9adc633ab
commit d84bde9b4d
1 changed files with 10 additions and 4 deletions

View File

@ -39,14 +39,20 @@ func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request, _ httproute
log.Print(user)
// Insert
_, err = h.db.Exec(`
INSERT INTO "user" (id, first_name, last_name, phone_number) VALUES ($1, $2, $3, $4)
`, user.ID, user.FirstName, user.LastName, user.PhoneNumber)
var finalId string
err = h.db.QueryRow(`
INSERT INTO "user" (id, first_name, last_name, phone_number)
VALUES ($1, $2, $3, $4)
ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number, first_name=$2, last_name=$3
RETURNING id
`, user.ID, 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
// Respond
w.Header().Set("Content-Type", "application/json")
@ -474,7 +480,7 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
// Insert
_, err = h.db.Exec(`
INSERT INTO contact ("user", contact) VALUES ($1, $2)
INSERT INTO contact ("user", contact) VALUES ($1, $2)
`, userID, contactId)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)