forked from wish/eventmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topics.go
73 lines (59 loc) · 2.12 KB
/
topics.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
package eventmaster
import (
"encoding/json"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/ContextLogic/eventmaster/jh"
eventmaster "github.com/ContextLogic/eventmaster/proto"
)
func (s *Server) addTopic(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
td := Topic{}
if err := json.NewDecoder(r.Body).Decode(&td); err != nil {
return td, jh.NewError(errors.Wrap(err, "json decode").Error(), http.StatusBadRequest)
}
if td.Name == "" {
return td, jh.NewError(errors.New("Must include topic_name in request").Error(), http.StatusBadRequest)
}
id, err := s.store.AddTopic(td)
if err != nil {
return nil, jh.Wrap(err, "add topic")
}
return jh.NewSuccess(map[string]string{"topic_id": id}, http.StatusCreated), nil
}
func (s *Server) getTopic(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
topics, err := s.store.GetTopics()
if err != nil {
return nil, jh.Wrap(err, "get topics")
}
ret := map[string][]Topic{"results": topics}
return ret, nil
}
func (s *Server) updateTopic(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
var td Topic
if err := json.NewDecoder(r.Body).Decode(&td); err != nil {
return td, jh.NewError(errors.Wrap(err, "json decode").Error(), http.StatusBadRequest)
}
topicName := ps.ByName("name")
if topicName == "" {
return nil, jh.NewError(errors.New("Must include topic name in request").Error(), http.StatusBadRequest)
}
id, err := s.store.UpdateTopic(topicName, td)
if err != nil {
return nil, jh.Wrap(err, "update topic")
}
return map[string]string{"topic_id": id}, nil
}
func (s *Server) deleteTopic(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
name := ps.ByName("name")
if name == "" {
return nil, jh.NewError(errors.New("Must include topic name in request").Error(), http.StatusBadRequest)
}
req := &eventmaster.DeleteTopicRequest{
TopicName: name,
}
if err := s.store.DeleteTopic(req); err != nil {
return nil, jh.Wrap(err, "delete topic")
}
return map[string]string{"topic": name}, nil
}