-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_link_add_post.go
executable file
·83 lines (64 loc) · 1.93 KB
/
handler_link_add_post.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package handlers
import (
"encoding/json"
"net/http"
"gopkg.in/mgo.v2"
"github.com/IlijevskiSymphony/symphonyGopher/server/configuration"
"github.com/IlijevskiSymphony/symphonyGopher/server/links"
"github.com/IlijevskiSymphony/symphonyGopher/server/partners"
"github.com/Sirupsen/logrus"
"github.com/gorilla/sessions"
"github.com/pborman/uuid"
)
type LinkPost struct {
Link string `json:"link"`
Tags []string `json:"tags"`
}
type HandlerLinkAddPost struct {
Configuration configuration.Configuration
SessionFn func() *mgo.Session
Store *sessions.CookieStore
}
func (h HandlerLinkAddPost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var posted LinkPost
if err := decoder.Decode(&posted); err != nil {
logrus.Infof("Cannot decode add application data. Error: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
session, err := h.Store.Get(req, "login-session")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if session.Values["id"] == nil {
logrus.Infof("Session id is nil")
w.WriteHeader(http.StatusUnauthorized)
return
}
partnerID := session.Values["id"].(string)
partnersRepo := partners.NewRepository(h.SessionFn())
defer partnersRepo.Close()
partner, err := partnersRepo.FindByID(partnerID)
if err != nil {
logrus.Errorf("Cannot find partner. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if partner.Status != partners.StatusActive {
logrus.Infof("Partner status is not active.")
w.WriteHeader(http.StatusUnauthorized)
return
}
link := links.New(uuid.New(), uuid.New(), posted.Link, posted.Tags)
linksRepo := links.NewRepository(h.SessionFn())
defer linksRepo.Close()
err = linksRepo.Update(link)
if err != nil {
logrus.Infof("Cannot update link. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}