/
savepost.go
43 lines (32 loc) · 1.04 KB
/
savepost.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
package endpoints
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/aditya43/golang/69-social-network-built-in-go-and-gopherjs/models/socialmedia"
"github.com/aditya43/golang/69-social-network-built-in-go-and-gopherjs/common/authenticate"
"github.com/aditya43/golang/69-social-network-built-in-go-and-gopherjs/common"
)
func SavePostEndpoint(env *common.Env) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gfSession, err := authenticate.SessionStore.Get(r, "gopherface-session")
if err != nil {
log.Print(err)
return
}
uuid := gfSession.Values["uuid"].(string)
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print("Encountered error when attempting to read the request body: ", err)
}
var p socialmedia.Post
json.Unmarshal(reqBody, &p)
err = env.DB.SavePost(uuid, p.Caption, p.MessageBody, p.RawMoodValue)
if err != nil {
w.Write([]byte("ERROR: Failed to save post!"))
} else {
w.Write([]byte("Post saved successfully!"))
}
})
}