From 36ae44d829316b0d1683af7908e40bf126e6cb06 Mon Sep 17 00:00:00 2001 From: orcas Date: Wed, 3 Jul 2019 11:03:44 +0800 Subject: [PATCH] Handle adding DM to person whose DM already exists. Fixes #14 --- README.md | 2 +- handlers.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 639a4f5..7599ae3 100644 --- a/README.md +++ b/README.md @@ -430,7 +430,7 @@ Add a member to the specified conversation. #### Success Response (200 OK) -Empty body. +The conversation ID of the conversation the user is added to. #### Errors diff --git a/handlers.go b/handlers.go index 234beb0..ec1418a 100644 --- a/handlers.go +++ b/handlers.go @@ -448,6 +448,23 @@ func (h *Handler) CreateConversationMember(w http.ResponseWriter, r *http.Reques // Log log.Print(member) + // Check for existing DM + var dmID string + err = h.db.QueryRow(` + SELECT "conversation".id FROM "conversation", "member" + WHERE + "conversation".dm = TRUE + AND "conversation".id = "member".conversation + AND "member".user = $1 + `, member.ID).Scan(&dmID) + if err != sql.ErrNoRows { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } else if err == nil { + w.Write([]byte(dmID)) + return + } + // Check for valid conversation and prevent duplicate entries var test string err = h.db.QueryRow(` @@ -504,7 +521,7 @@ func (h *Handler) CreateConversationMember(w http.ResponseWriter, r *http.Reques // Respond //w.Header().Set("Content-Type", "application/json") //json.NewEncoder(w).Encode(member) - w.WriteHeader(200) + w.Write([]byte(conversationID)) } func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request, p httprouter.Params) {