forked from nytlabs/st-core
/
link.go
141 lines (114 loc) · 2.68 KB
/
link.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package server
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
type LinkLedger struct {
Source struct {
Id int `json:"id"`
} `json:"source"` // the soure id
Block struct {
Id int `json:"id"`
} `json:"block"` // the block id
Id int `json:"id"` // link id
}
type ProtoLink struct {
Source struct {
Id int `json:"id"`
} `json:"source"` // the soure id
Block struct {
Id int `json:"id"`
} `json:"block"` // the block id
}
func (s *Server) CreateLink(l ProtoLink) (*LinkLedger, error) {
b, ok := s.blocks[l.Block.Id]
if !ok {
return nil, errors.New("could not find block")
}
sl, ok := s.sources[l.Source.Id]
if !ok {
return nil, errors.New("could not find source")
}
link := &LinkLedger{}
link.Id = s.GetNextID()
link.Source.Id = l.Source.Id
link.Block.Id = l.Block.Id
err := b.Block.SetSource(sl.Source)
if err != nil {
return nil, err
}
s.links[link.Id] = link
s.websocketBroadcast(Update{Action: CREATE, Type: LINK, Data: wsLink{*link}})
return link, nil
}
func (s *Server) DeleteLink(id int) error {
link, ok := s.links[id]
if !ok {
return errors.New("could not find link")
}
block, ok := s.blocks[link.Block.Id]
if !ok {
return errors.New("could not find block")
}
block.Block.SetSource(nil)
delete(s.links, id)
s.websocketBroadcast(Update{Action: DELETE, Type: LINK, Data: wsLink{wsId{id}}})
return nil
}
func (s *Server) listLinks() []LinkLedger {
links := []LinkLedger{}
for _, l := range s.links {
links = append(links, *l)
}
return links
}
func (s *Server) LinkIndexHandler(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()
c := s.listLinks()
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(c); err != nil {
panic(err)
}
}
func (s *Server) LinkCreateHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, Error{err.Error()})
return
}
var newLink ProtoLink
json.Unmarshal(body, &newLink)
s.Lock()
defer s.Unlock()
nl, err := s.CreateLink(newLink)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, Error{err.Error()})
return
}
w.WriteHeader(http.StatusOK)
writeJSON(w, nl)
}
func (s *Server) LinkDeleteHandler(w http.ResponseWriter, r *http.Request) {
id, err := getIDFromMux(mux.Vars(r))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, err)
return
}
s.Lock()
defer s.Unlock()
err = s.DeleteLink(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, Error{err.Error()})
return
}
w.WriteHeader(http.StatusNoContent)
}