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
|
package likes
import (
"fmt"
"log"
"net/http"
"strconv"
"fagott.pw/charakterin"
"fagott.pw/grilist/grilist"
"fagott.pw/grilist/modules/lists"
"github.com/julienschmidt/httprouter"
)
type Module struct {
g *grilist.Grilist
lists *lists.Module
}
func New() *Module {
return &Module{}
}
func (m *Module) Name() string {
return "Likes"
}
func (m *Module) Init(g *grilist.Grilist) {
m.g = g
gm, ok := g.Modules["Lists"]
if !ok {
log.Fatal("tags: lists module not found")
}
m.lists = gm.(*lists.Module)
m.g.Router.GET("/api/likes/count", m.getLikeCount)
}
func (m *Module) Interface() interface{} {
return m
}
func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
return make([]grilist.DashboardCategory, 0)
}
func (m *Module) getLikeCount(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
params := r.URL.Query()
contentId, err := strconv.Atoi(params.Get("id"))
if err != nil {
http.Error(w, "invalid content id", http.StatusBadRequest)
return
}
contentType, err := strconv.Atoi(params.Get("type"))
if err != nil {
http.Error(w, "invalid content id", http.StatusBadRequest)
return
}
var res int
if err := m.g.DB.QueryRow(`SELECT COUNT(*) FROM grilist.likes WHERE content = $1 AND type = $2`, contentId, contentType).Scan(&res); err != nil {
http.Error(w, "pq error", http.StatusInternalServerError)
log.Printf("error getting like count: %s", err)
return
}
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("%d", res)))
}
|