Resolves #17 pinned field for conversation
parent
e81b91eebb
commit
1256066597
34
README.md
34
README.md
|
@ -357,7 +357,8 @@ List of conversations.
|
|||
{
|
||||
"id": "<id>",
|
||||
"title": "<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*
|
||||
|
||||
```
|
||||
|
|
49
handlers.go
49
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}
|
||||
}
|
||||
|
|
1
main.go
1
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
|
||||
|
|
|
@ -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")
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue