From d84bde9b4d6cabe0ff2d1bfa82aae22cee83bd05 Mon Sep 17 00:00:00 2001 From: UnicodingUnicorn <7555ic@gmail.com> Date: Fri, 14 Jun 2019 05:30:32 +0800 Subject: [PATCH] Fixes #6 Added ON CONFLICT clause to INSERT in CreateUser --- handlers.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/handlers.go b/handlers.go index 4ecc159..af28bf1 100644 --- a/handlers.go +++ b/handlers.go @@ -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)