diff --git a/conversation.go b/conversation.go index b2eeb3e..b525ecb 100644 --- a/conversation.go +++ b/conversation.go @@ -71,14 +71,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) - ELSE title - END AS title, - picture - FROM "conversation" - INNER JOIN member - ON member.conversation = "conversation".id AND member.user = $1 + 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, + "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) @@ -113,14 +113,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) - ELSE title - END AS title, - picture - FROM "conversation" - INNER JOIN member - ON member.conversation = "conversation".id AND member.user = $1 AND member.conversation = $2 + 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, + "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 { @@ -263,12 +263,12 @@ func (h *Handler) CreateConversationMember(w http.ResponseWriter, r *http.Reques // 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) + 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 @@ -280,19 +280,19 @@ func (h *Handler) CreateConversationMember(w http.ResponseWriter, r *http.Reques // Check for valid conversation and prevent duplicate entries var test string err = h.db.QueryRow(` - SELECT "conversation".id FROM "conversation", "member" - WHERE - "conversation".id = $1 - AND ( - "conversation".dm = FALSE - OR (SELECT - COUNT("member".user) - FROM "member" - WHERE "member".conversation = $1) - <= 2) - AND "member".conversation = "conversation".id - AND "member".user <> $2 - `, conversationID, member.ID).Scan(&test) + SELECT "conversation".id FROM "conversation", "member" + WHERE + "conversation".id = $1 + AND ( + "conversation".dm = FALSE + OR (SELECT + COUNT("member".user) + FROM "member" + WHERE "member".conversation = $1) + <= 2) + AND "member".conversation = "conversation".id + AND "member".user <> $2 + `, conversationID, member.ID).Scan(&test) switch { case err == sql.ErrNoRows: http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) diff --git a/main.go b/main.go index 260f0f4..d1f7690 100644 --- a/main.go +++ b/main.go @@ -52,7 +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/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/types.go b/types.go index 0fdb545..970eaac 100644 --- a/types.go +++ b/types.go @@ -3,9 +3,9 @@ package main type Conversation struct { ID string `json:"id"` // id Title string `json:"title"` // title - DM bool `json:"dm"` // dm - Picture string `json:"picture"` // picture - Pinned bool `json:"pinned"` // pinned + DM bool `json:"dm"` // dm + Picture string `json:"picture"` // picture + Pinned bool `json:"pinned"` // pinned } type User struct {