From 1256066597c83679beb5f87ce78a88b87e18e196 Mon Sep 17 00:00:00 2001 From: orcas Date: Sun, 11 Aug 2019 11:06:19 +0800 Subject: [PATCH] Resolves #17 pinned field for conversation --- README.md | 34 +++++++++++++++++++++++++-- handlers.go | 49 +++++++++++++++++++++++++++++---------- main.go | 1 + postgres/1_initial.up.sql | 1 + types.go | 1 + 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7599ae3..b8e7ed7 100644 --- a/README.md +++ b/README.md @@ -357,7 +357,8 @@ List of conversations. { "id": "", "title": "" - "picture": "<picture>" + "picture": "<picture>", + "pinned: "<pinned:bool>" }, ... ] @@ -394,7 +395,8 @@ Conversation object. { "id": "<id>", "title": "<title>", - "picture": "<picture>" + "picture": "<picture>", + "pinned: "<pinned:bool>" } ``` @@ -408,6 +410,34 @@ Conversation object. --- +### Pin Conversation + +``` +POST /user/conversation/:conversation/pin +``` + +Mark the specified conversation as pinned. + +#### URL Params + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| conversation | String | Conversation's ID. | ✓ | + +#### Success Response (200 OK) + +Blank body. + +#### Errors + +| Code | Description | +| ---- | ----------- | +| 400 | Invalid `X-User-Claim` header. | +| 404 | Conversation with supplied ID could not be found in database. | +| 500 | Error occurred editing entry in the database. | + +--- + ### Create Conversation Member* ``` diff --git a/handlers.go b/handlers.go index 68aa082..eb2950b 100644 --- a/handlers.go +++ b/handlers.go @@ -248,14 +248,14 @@ func (h *Handler) GetConversations(w http.ResponseWriter, r *http.Request, p htt // Select rows, err := h.db.Query(` - SELECT id, CASE - WHEN dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) + SELECT "conversation".id, CASE + WHEN "conversation".dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) ELSE title END AS title, - picture - FROM "conversation" - INNER JOIN member - ON member.conversation = "conversation".id AND member.user = $1 + "conversation".picture, + member.pinned + FROM "conversation", member + WHERE member.conversation = "conversation".id AND member.user = $1 `, userID) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) @@ -290,14 +290,14 @@ func (h *Handler) GetConversation(w http.ResponseWriter, r *http.Request, p http // Select err := h.db.QueryRow(` - SELECT id, CASE - WHEN dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) + SELECT "conversation".id, CASE + WHEN "conversation".dm THEN (SELECT CONCAT("user".first_name, ' ', "user".last_name) FROM "user", member WHERE "user".id <> $1 AND "user".id = member.user AND member.conversation = "conversation".id) ELSE title END AS title, - picture - FROM "conversation" - INNER JOIN member - ON member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2 + "conversation" picture, + member.pinned + FROM "conversation", member + WHERE member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2 `, userID, conversationID).Scan(&conversation.ID, &conversation.Title, &conversation.Picture) switch { @@ -643,6 +643,31 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout json.NewEncoder(w).Encode(contacts) } +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) +} + func NewHandler(db *sql.DB) *Handler { return &Handler{db} } diff --git a/main.go b/main.go index 246620f..6c17be6 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,7 @@ func main() { router.GET("/user/conversation/:conversation", AuthMiddleware(h.GetConversation)) // USER MEMBER CONVERSATION router.PATCH("/user/conversation/:conversation", AuthMiddleware(h.UpdateConversation)) // USER MEMBER CONVERSATION ADMIN=true -> update conversation title //router.DELETE("/user/:user/conversation/:conversation", h.DeleteConversation) // USER MEMBER CONVERSATION -> delete membership + router.POST("/user/conversation/:conversation/pin", AuthMiddleware(h.PinConversation)) router.POST("/user/conversation/:conversation/member", AuthMiddleware(h.CreateConversationMember)) // USER MEMBER CONVERSATION ADMIN=true -> create new membership router.GET("/user/conversation/:conversation/member", AuthMiddleware(h.GetConversationMembers)) // USER MEMBER CONVERSATION //router.DELETE("/user/:user/conversation/:conversation/member/:member", h.DeleteConversationMember) // USER MEMBER CONVERSATION ADMIN=true -> delete membership diff --git a/postgres/1_initial.up.sql b/postgres/1_initial.up.sql index d8a2e81..a633d11 100644 --- a/postgres/1_initial.up.sql +++ b/postgres/1_initial.up.sql @@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS "conversation" ( CREATE TABLE IF NOT EXISTS member ( "user" BYTEA REFERENCES "user"(id), "conversation" BYTEA REFERENCES "conversation"(id), + "pinned" BOOLEAN DEFAULT FALSE, UNIQUE ("user", "conversation") ); diff --git a/types.go b/types.go index 3015573..b807077 100644 --- a/types.go +++ b/types.go @@ -5,6 +5,7 @@ type Conversation struct { Title string `json:"title"` // title DM bool `json:"dm"` // dm Picture string `json:"picture"` // picture + Pinned bool `json:"pinned"` // pinned } type User struct {