diff --git a/README.md b/README.md index 2a3e26c..639a4f5 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ Create a new conversation for a user. | ---- | ---- | ----------- | -------- | | title | String | Title of the conversation | X | | dm | Boolean | Whether the conversation is a DM or not | X | +| picture | String | URL of the group's picture | X | #### Success Response (200 OK) @@ -261,7 +262,8 @@ Conversation object. ```json { "id": "", - "title": "" + "title": "<title>", + "picture": "<picture>" } ``` @@ -322,6 +324,7 @@ Update a conversation's details (mainly just title for now). | Name | Type | Description | Required | | ---- | ---- | ----------- | -------- | | title | String | New title of the conversation. | X | +| picture | String | New URL of the group's picture | X | #### Success Response (200 OK) @@ -354,6 +357,7 @@ List of conversations. { "id": "<id>", "title": "<title>" + "picture": "<picture>" }, ... ] @@ -389,7 +393,8 @@ Conversation object. ```json { "id": "<id>", - "title": "<title>" + "title": "<title>", + "picture": "<picture>" } ``` diff --git a/core b/core new file mode 100755 index 0000000..61d5abc Binary files /dev/null and b/core differ diff --git a/handlers.go b/handlers.go index 1902be0..ea3d73b 100644 --- a/handlers.go +++ b/handlers.go @@ -225,8 +225,8 @@ func (h *Handler) CreateConversation(w http.ResponseWriter, r *http.Request, p h // Conversation _, err1 := tx.Exec(` - INSERT INTO "conversation" (id, title, dm) VALUES ($1, $2, $3) - `, conversation.ID, conversation.Title, conversation.DM) + INSERT INTO "conversation" (id, title, dm, picture) VALUES ($1, $2, $3, $4) + `, conversation.ID, conversation.Title, conversation.DM, conversation.Picture) // First member _, err2 := tx.Exec(` INSERT INTO member ("user", "conversation") VALUES ($1, $2) @@ -262,7 +262,8 @@ func (h *Handler) GetConversations(w http.ResponseWriter, r *http.Request, p htt 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) ELSE title - END AS title + END AS title, + picture FROM "conversation" INNER JOIN member ON member.conversation = "conversation".id AND member.user = $1 @@ -276,13 +277,13 @@ func (h *Handler) GetConversations(w http.ResponseWriter, r *http.Request, p htt // Scan for rows.Next() { - var id, title string - if err := rows.Scan(&id, &title); err != nil { + var id, title, picture string + if err := rows.Scan(&id, &title, &picture); err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) log.Print(err) return } - conversations = append(conversations, Conversation{ID:id, Title:title, DM:false}) + conversations = append(conversations, Conversation{ID:id, Title:title, DM:false, Picture:picture}) } // Respond @@ -303,11 +304,12 @@ func (h *Handler) GetConversation(w http.ResponseWriter, r *http.Request, p http 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) ELSE title - END AS title + END AS title, + picture FROM "conversation" INNER JOIN member ON member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2 - `, userID, conversationID).Scan(&conversation.ID, &conversation.Title) + `, userID, conversationID).Scan(&conversation.ID, &conversation.Title, &conversation.Picture) switch { case err == sql.ErrNoRows: @@ -357,9 +359,9 @@ func (h *Handler) UpdateConversation(w http.ResponseWriter, r *http.Request, p h if len(conversation.Title) > 0 { _, err = h.db.Exec(` UPDATE "conversation" - SET title = $2 + SET title = $2, picture = $3 WHERE id = $1 - `, conversationID, conversation.Title) + `, conversationID, conversation.Title, conversation.Picture) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) log.Print(err) diff --git a/types.go b/types.go index abd8b6d..3015573 100644 --- a/types.go +++ b/types.go @@ -1,9 +1,10 @@ package main type Conversation struct { - ID string `json:"id"` // id - Title string `json:"title"` // title - DM bool `json:"dm"` // dm + ID string `json:"id"` // id + Title string `json:"title"` // title + DM bool `json:"dm"` // dm + Picture string `json:"picture"` // picture } type User struct {