aboutsummaryrefslogtreecommitdiff
path: root/modules/likes
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-11-14 15:55:50 (UTC)
committerjan <jan@ruken.pw>2016-11-14 15:55:50 (UTC)
commitc735553214400f3af69e516ff41a6da9214449a7 (patch)
treec9a8e8fc84fdbaffb3a3fa318453b36270550ebb /modules/likes
parent9832cd28de49421021ed7c1f3caa7fdda7bc4372 (diff)
likes und so.
Diffstat (limited to 'modules/likes')
-rw-r--r--modules/likes/likes.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/modules/likes/likes.go b/modules/likes/likes.go
new file mode 100644
index 0000000..949cba2
--- /dev/null
+++ b/modules/likes/likes.go
@@ -0,0 +1,70 @@
1package likes
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7 "strconv"
8
9 "fagott.pw/charakterin"
10 "fagott.pw/grilist/grilist"
11 "fagott.pw/grilist/modules/lists"
12
13 "github.com/julienschmidt/httprouter"
14)
15
16type Module struct {
17 g *grilist.Grilist
18 lists *lists.Module
19}
20
21func New() *Module {
22 return &Module{}
23}
24
25func (m *Module) Name() string {
26 return "Likes"
27}
28
29func (m *Module) Init(g *grilist.Grilist) {
30 m.g = g
31 gm, ok := g.Modules["Lists"]
32 if !ok {
33 log.Fatal("tags: lists module not found")
34 }
35 m.lists = gm.(*lists.Module)
36 m.g.Router.GET("/api/likes/count", m.getLikeCount)
37}
38
39func (m *Module) Interface() interface{} {
40 return m
41}
42
43func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
44 return make([]grilist.DashboardCategory, 0)
45}
46
47func (m *Module) getLikeCount(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
48 params := r.URL.Query()
49
50 contentId, err := strconv.Atoi(params.Get("id"))
51 if err != nil {
52 http.Error(w, "invalid content id", http.StatusBadRequest)
53 return
54 }
55 contentType, err := strconv.Atoi(params.Get("type"))
56 if err != nil {
57 http.Error(w, "invalid content id", http.StatusBadRequest)
58 return
59 }
60
61 var res int
62 if err := m.g.DB.QueryRow(`SELECT COUNT(*) FROM grilist.likes WHERE content = $1 AND type = $2`, contentId, contentType).Scan(&res); err != nil {
63 http.Error(w, "pq error", http.StatusInternalServerError)
64 log.Printf("error getting like count: %s", err)
65 return
66 }
67
68 w.WriteHeader(200)
69 w.Write([]byte(fmt.Sprintf("%d", res)))
70}