-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageController.go
47 lines (44 loc) · 1.33 KB
/
messageController.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
38
39
40
41
42
43
44
45
46
47
package main
import (
"strings"
"strconv"
"net/http"
"encoding/json"
"github.com/gorilla/mux"
)
func messagePost(w http.ResponseWriter, r *http.Request) {
from := r.FormValue("from")
to := r.FormValue("to")
body := r.FormValue("message")
if (len(strings.TrimSpace(from)) == 0 ||
len(strings.TrimSpace(to)) == 0 ||
len(strings.TrimSpace(body)) == 0){
// invalid input
json.NewEncoder(w).Encode("Error: Bad Input")
}else{
// valid input
NewMessage(from, to, body)
}
}
func messagesIndex(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
from := vars["from"]
to := vars["to"]
fromTimestamp, err := strconv.Atoi(vars["fromTimestamp"])// convert timestamp to int
if err != nil { // if the conversion failed just return all messages
fromTimestamp = 0
}
// call our model as normal
messagesData := GetMessagesAfter(from, to, fromTimestamp)
// return the data
json.NewEncoder(w).Encode(messagesData)
}
func timelessMessagesIndex(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
from := vars["from"]
to := vars["to"]
// fromTimestamp of 0 will return all messages
messagesData := GetMessagesAfter(from, to, 0)
// return the data
json.NewEncoder(w).Encode(messagesData)
}