From b24874b661fc41b4979dd8065547cde4730b4953 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sat, 31 Aug 2019 11:37:54 +0800 Subject: [PATCH] Re-add PinConversation --- conversation.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/conversation.go b/conversation.go index 0ff6f3f..b2eeb3e 100644 --- a/conversation.go +++ b/conversation.go @@ -374,3 +374,28 @@ func (h *Handler) GetConversationMembers(w http.ResponseWriter, r *http.Request, w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(users) } + +func (h *Handler) PinConversation(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + conversationID := p.ByName("conversation") + userID := r.Context().Value("user").(string) + + // Check relation exists + var exists int + err := h.db.QueryRow(`SELECT 1 FROM member WHERE "user" = $1 AND "conversation" = $2`, userID, conversationID).Scan(&exists) + if err == sql.ErrNoRows { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } else if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + + // Update relation + _, err = h.db.Exec(`UPDATE "member" SET "pinned" = TRUE WHERE "user" = $1 AND "conversation" = $2`, userID, conversationID) + if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + + w.WriteHeader(200) +}