diff options
Diffstat (limited to 'modules/likes')
-rw-r--r-- | modules/likes/likes.go | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/modules/likes/likes.go b/modules/likes/likes.go index 949cba2..4d69d7b 100644 --- a/modules/likes/likes.go +++ b/modules/likes/likes.go | |||
@@ -2,11 +2,14 @@ package likes | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "io/ioutil" | ||
5 | "log" | 6 | "log" |
6 | "net/http" | 7 | "net/http" |
8 | "net/url" | ||
7 | "strconv" | 9 | "strconv" |
8 | 10 | ||
9 | "fagott.pw/charakterin" | 11 | "fagott.pw/charakterin" |
12 | "fagott.pw/grilist/cache" | ||
10 | "fagott.pw/grilist/grilist" | 13 | "fagott.pw/grilist/grilist" |
11 | "fagott.pw/grilist/modules/lists" | 14 | "fagott.pw/grilist/modules/lists" |
12 | 15 | ||
@@ -15,6 +18,7 @@ import ( | |||
15 | 18 | ||
16 | type Module struct { | 19 | type Module struct { |
17 | g *grilist.Grilist | 20 | g *grilist.Grilist |
21 | c *cache.Cache | ||
18 | lists *lists.Module | 22 | lists *lists.Module |
19 | } | 23 | } |
20 | 24 | ||
@@ -33,7 +37,11 @@ func (m *Module) Init(g *grilist.Grilist) { | |||
33 | log.Fatal("tags: lists module not found") | 37 | log.Fatal("tags: lists module not found") |
34 | } | 38 | } |
35 | m.lists = gm.(*lists.Module) | 39 | m.lists = gm.(*lists.Module) |
40 | m.c = cache.New() | ||
36 | m.g.Router.GET("/api/likes/count", m.getLikeCount) | 41 | 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) | ||
37 | } | 45 | } |
38 | 46 | ||
39 | func (m *Module) Interface() interface{} { | 47 | func (m *Module) Interface() interface{} { |
@@ -68,3 +76,115 @@ func (m *Module) getLikeCount(w http.ResponseWriter, r *http.Request, p httprout | |||
68 | w.WriteHeader(200) | 76 | w.WriteHeader(200) |
69 | w.Write([]byte(fmt.Sprintf("%d", res))) | 77 | w.Write([]byte(fmt.Sprintf("%d", res))) |
70 | } | 78 | } |
79 | func (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 | } | ||
108 | func (m *Module) addLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
109 | params, err := readBody(r) | ||
110 | if err != nil { | ||
111 | log.Println(err) | ||
112 | http.Error(w, "invalid body", http.StatusBadRequest) | ||
113 | return | ||
114 | } | ||
115 | |||
116 | contentId, err := strconv.Atoi(params.Get("id")) | ||
117 | if err != nil { | ||
118 | log.Println(err) | ||
119 | http.Error(w, "invalid content id", http.StatusBadRequest) | ||
120 | return | ||
121 | } | ||
122 | contentType, err := strconv.Atoi(params.Get("type")) | ||
123 | if err != nil { | ||
124 | log.Println(err) | ||
125 | http.Error(w, "invalid content type", http.StatusBadRequest) | ||
126 | return | ||
127 | } | ||
128 | userId, err := strconv.Atoi(params.Get("user")) | ||
129 | if err != nil { | ||
130 | http.Error(w, "invalid user id", http.StatusBadRequest) | ||
131 | return | ||
132 | } | ||
133 | |||
134 | _, 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) | ||
135 | if err != nil { | ||
136 | http.Error(w, "pq error", http.StatusInternalServerError) | ||
137 | log.Printf("error add like: %s", err) | ||
138 | return | ||
139 | } | ||
140 | |||
141 | w.WriteHeader(200) | ||
142 | } | ||
143 | |||
144 | func (m *Module) removeLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
145 | params, err := readBody(r) | ||
146 | if err != nil { | ||
147 | http.Error(w, "invalid body", http.StatusBadRequest) | ||
148 | return | ||
149 | } | ||
150 | |||
151 | contentId, err := strconv.Atoi(params.Get("id")) | ||
152 | if err != nil { | ||
153 | http.Error(w, "invalid content id", http.StatusBadRequest) | ||
154 | return | ||
155 | } | ||
156 | contentType, err := strconv.Atoi(params.Get("type")) | ||
157 | if err != nil { | ||
158 | http.Error(w, "invalid content type", http.StatusBadRequest) | ||
159 | return | ||
160 | } | ||
161 | userId, err := strconv.Atoi(params.Get("user")) | ||
162 | if err != nil { | ||
163 | http.Error(w, "invalid user id", http.StatusBadRequest) | ||
164 | return | ||
165 | } | ||
166 | |||
167 | _, err = m.g.DB.Exec(`DELETE FROM grilist.likes WHERE content = $1 AND "user" = $2 AND type = $3`, contentId, userId, contentType) | ||
168 | if err != nil { | ||
169 | http.Error(w, "pq error", http.StatusInternalServerError) | ||
170 | log.Printf("error add like: %s", err) | ||
171 | return | ||
172 | } | ||
173 | |||
174 | w.WriteHeader(200) | ||
175 | } | ||
176 | |||
177 | func readBody(r *http.Request) (url.Values, error) { | ||
178 | defer r.Body.Close() | ||
179 | data, err := ioutil.ReadAll(r.Body) | ||
180 | if err != nil { | ||
181 | return nil, err | ||
182 | } | ||
183 | |||
184 | values, err := url.ParseQuery(string(data)) | ||
185 | if err != nil { | ||
186 | return nil, err | ||
187 | } | ||
188 | |||
189 | return values, nil | ||
190 | } | ||