diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/likes/likes.go | 70 |
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 @@ | |||
1 | package likes | ||
2 | |||
3 | import ( | ||
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 | |||
16 | type Module struct { | ||
17 | g *grilist.Grilist | ||
18 | lists *lists.Module | ||
19 | } | ||
20 | |||
21 | func New() *Module { | ||
22 | return &Module{} | ||
23 | } | ||
24 | |||
25 | func (m *Module) Name() string { | ||
26 | return "Likes" | ||
27 | } | ||
28 | |||
29 | func (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 | |||
39 | func (m *Module) Interface() interface{} { | ||
40 | return m | ||
41 | } | ||
42 | |||
43 | func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | ||
44 | return make([]grilist.DashboardCategory, 0) | ||
45 | } | ||
46 | |||
47 | func (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 | } | ||