aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-11-14 15:55:50 (UTC)
committerrtz12 <koenig@fagott.pw>2016-11-15 17:52:22 (UTC)
commitdae4433aae4804901e3d2a36342fe1522224ce64 (patch)
treed4162ac39125b7858f4fe4853e90a5a628c12060 /modules
parentadec4bc8d8aead5c2c75a5e827df341cef498c87 (diff)
FIX: likes und so.
Diffstat (limited to 'modules')
-rw-r--r--modules/likes/likes.go153
1 files changed, 0 insertions, 153 deletions
diff --git a/modules/likes/likes.go b/modules/likes/likes.go
index 80f632b..949cba2 100644
--- a/modules/likes/likes.go
+++ b/modules/likes/likes.go
@@ -2,14 +2,11 @@ package likes
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
5 "io/ioutil"
6 "log" 5 "log"
7 "net/http" 6 "net/http"
8 "net/url"
9 "strconv" 7 "strconv"
10 8
11 "fagott.pw/charakterin" 9 "fagott.pw/charakterin"
12 "fagott.pw/grilist/cache"
13 "fagott.pw/grilist/grilist" 10 "fagott.pw/grilist/grilist"
14 "fagott.pw/grilist/modules/lists" 11 "fagott.pw/grilist/modules/lists"
15 12
@@ -18,7 +15,6 @@ import (
18 15
19type Module struct { 16type Module struct {
20 g *grilist.Grilist 17 g *grilist.Grilist
21 c *cache.Cache
22 lists *lists.Module 18 lists *lists.Module
23} 19}
24 20
@@ -37,11 +33,7 @@ func (m *Module) Init(g *grilist.Grilist) {
37 log.Fatal("tags: lists module not found") 33 log.Fatal("tags: lists module not found")
38 } 34 }
39 m.lists = gm.(*lists.Module) 35 m.lists = gm.(*lists.Module)
40 m.c = cache.New()
41 m.g.Router.GET("/api/likes/count", m.getLikeCount) 36 m.g.Router.GET("/api/likes/count", m.getLikeCount)
42 m.g.Router.GET("/api/likes/liked_by", m.isLikedBy)
43 m.g.Router.PUT("/api/likes", m.addLike)
44 m.g.Router.DELETE("/api/likes", m.removeLike)
45} 37}
46 38
47func (m *Module) Interface() interface{} { 39func (m *Module) Interface() interface{} {
@@ -76,148 +68,3 @@ func (m *Module) getLikeCount(w http.ResponseWriter, r *http.Request, p httprout
76 w.WriteHeader(200) 68 w.WriteHeader(200)
77 w.Write([]byte(fmt.Sprintf("%d", res))) 69 w.Write([]byte(fmt.Sprintf("%d", res)))
78} 70}
79func (m *Module) isLikedBy(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
80 params := r.URL.Query()
81
82 contentId, err := strconv.Atoi(params.Get("id"))
83 if err != nil {
84 http.Error(w, "invalid content id", http.StatusBadRequest)
85 return
86 }
87 contentType, err := strconv.Atoi(params.Get("type"))
88 if err != nil {
89 http.Error(w, "invalid content type", http.StatusBadRequest)
90 return
91 }
92 userId, err := strconv.Atoi(params.Get("user"))
93 if err != nil {
94 http.Error(w, "invalid user id", http.StatusBadRequest)
95 return
96 }
97
98 var res int
99 if err := m.g.DB.QueryRow(`SELECT COUNT(*) FROM grilist.likes WHERE content = $1 AND type = $2 AND "user" = $3`, contentId, contentType, userId).Scan(&res); err != nil {
100 http.Error(w, "pq error", http.StatusInternalServerError)
101 log.Printf("error getting like by user: %s", err)
102 return
103 }
104
105 w.WriteHeader(200)
106 w.Write([]byte(fmt.Sprintf("%t", res > 0)))
107}
108func (m *Module) addLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
109 user, _ := m.g.Charakterin.GetUserFromRequest(r)
110
111 if user == nil {
112 http.Error(w, "403", http.StatusForbidden)
113 return
114 }
115
116 params, err := readBody(r)
117 if err != nil {
118 log.Println(err)
119 http.Error(w, "invalid body", http.StatusBadRequest)
120 return
121 }
122
123 contentId, err := strconv.Atoi(params.Get("id"))
124 if err != nil {
125 log.Println(err)
126 http.Error(w, "invalid content id", http.StatusBadRequest)
127 return
128 }
129 contentType, err := strconv.Atoi(params.Get("type"))
130 if err != nil {
131 log.Println(err)
132 http.Error(w, "invalid content type", http.StatusBadRequest)
133 return
134 }
135 userId, err := strconv.Atoi(params.Get("user"))
136 if err != nil {
137 http.Error(w, "invalid user id", http.StatusBadRequest)
138 return
139 }
140 if user.ID != userId {
141 http.Error(w, "403", http.StatusForbidden)
142 return
143 }
144
145 lists := m.lists.GetUserLists(user, false)
146 for _, list := range lists {
147 if list.Owner.ID == user.ID {
148 http.Error(w, "403", http.StatusForbidden)
149 return
150 }
151 }
152
153 _, err = m.g.DB.Exec(`INSERT INTO grilist.likes(content, "user", type) SELECT $1, $2, $3 WHERE NOT EXISTS (SELECT * FROM grilist.likes WHERE content = $1 AND "user" = $2 AND type = $3)`, contentId, userId, contentType)
154 if err != nil {
155 http.Error(w, "pq error", http.StatusInternalServerError)
156 log.Printf("error add like: %s", err)
157 return
158 }
159
160 w.WriteHeader(200)
161}
162
163func (m *Module) removeLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
164 user, _ := m.g.Charakterin.GetUserFromRequest(r)
165
166 if user == nil {
167 http.Error(w, "403", http.StatusForbidden)
168 return
169 }
170
171 params, err := readBody(r)
172 if err != nil {
173 log.Println(err)
174 http.Error(w, "invalid body", http.StatusBadRequest)
175 return
176 }
177
178 contentId, err := strconv.Atoi(params.Get("id"))
179 if err != nil {
180 log.Println(err)
181 http.Error(w, "invalid content id", http.StatusBadRequest)
182 return
183 }
184 contentType, err := strconv.Atoi(params.Get("type"))
185 if err != nil {
186 log.Println(err)
187 http.Error(w, "invalid content type", http.StatusBadRequest)
188 return
189 }
190 userId, err := strconv.Atoi(params.Get("user"))
191 if err != nil {
192 http.Error(w, "invalid user id", http.StatusBadRequest)
193 return
194 }
195 if user.ID != userId {
196 http.Error(w, "403", http.StatusForbidden)
197 return
198 }
199
200 _, err = m.g.DB.Exec(`DELETE FROM grilist.likes WHERE content = $1 AND "user" = $2 AND type = $3`, contentId, userId, contentType)
201 if err != nil {
202 http.Error(w, "pq error", http.StatusInternalServerError)
203 log.Printf("error add like: %s", err)
204 return
205 }
206
207 w.WriteHeader(200)
208}
209
210func readBody(r *http.Request) (url.Values, error) {
211 defer r.Body.Close()
212 data, err := ioutil.ReadAll(r.Body)
213 if err != nil {
214 return nil, err
215 }
216
217 values, err := url.ParseQuery(string(data))
218 if err != nil {
219 return nil, err
220 }
221
222 return values, nil
223}