-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLastMessages.go
37 lines (30 loc) · 1003 Bytes
/
getLastMessages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package server
import (
"encoding/json"
"net/http"
"github.com/niakr1s/chatty-server/app/config"
"github.com/niakr1s/chatty-server/app/er"
"github.com/niakr1s/chatty-server/app/internal/httputil"
"github.com/niakr1s/chatty-server/app/internal/sess"
"github.com/niakr1s/chatty-server/app/models"
)
// GetLastMessages ...
func (s *Server) GetLastMessages(w http.ResponseWriter, r *http.Request) {
username := sess.GetUserNameFromCtx(r.Context())
input := models.Chat{}
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
httputil.WriteError(w, err, http.StatusBadRequest)
return
}
if !s.dbStore.ChatDB.IsInChat(input.ChatName, username) {
httputil.WriteError(w, er.ErrNotInChat, http.StatusBadRequest)
return
}
res, _ := s.dbStore.MessageDB.GetLastNMessages(input.ChatName, config.C.LastMessages)
err = json.NewEncoder(w).Encode(res)
if err != nil {
httputil.WriteError(w, err, http.StatusInternalServerError)
return
}
}