aboutsummaryrefslogtreecommitdiff
path: root/modules/likes/likes.go
blob: 4d69d7bb3f1a5f2b0ee0bb96a211cc6887b72507 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package likes

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strconv"

	"fagott.pw/charakterin"
	"fagott.pw/grilist/cache"
	"fagott.pw/grilist/grilist"
	"fagott.pw/grilist/modules/lists"

	"github.com/julienschmidt/httprouter"
)

type Module struct {
	g     *grilist.Grilist
	c     *cache.Cache
	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.c = cache.New()
	m.g.Router.GET("/api/likes/count", m.getLikeCount)
	m.g.Router.GET("/api/likes/liked_by", m.isLikedBy)
	m.g.Router.PUT("/api/likes", m.addLike)
	m.g.Router.DELETE("/api/likes", m.removeLike)
}

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)))
}
func (m *Module) isLikedBy(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 type", http.StatusBadRequest)
		return
	}
	userId, err := strconv.Atoi(params.Get("user"))
	if err != nil {
		http.Error(w, "invalid user id", http.StatusBadRequest)
		return
	}

	var res int
	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 {
		http.Error(w, "pq error", http.StatusInternalServerError)
		log.Printf("error getting like by user: %s", err)
		return
	}

	w.WriteHeader(200)
	w.Write([]byte(fmt.Sprintf("%t", res > 0)))
}
func (m *Module) addLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	params, err := readBody(r)
	if err != nil {
		log.Println(err)
		http.Error(w, "invalid body", http.StatusBadRequest)
		return
	}

	contentId, err := strconv.Atoi(params.Get("id"))
	if err != nil {
		log.Println(err)
		http.Error(w, "invalid content id", http.StatusBadRequest)
		return
	}
	contentType, err := strconv.Atoi(params.Get("type"))
	if err != nil {
		log.Println(err)
		http.Error(w, "invalid content type", http.StatusBadRequest)
		return
	}
	userId, err := strconv.Atoi(params.Get("user"))
	if err != nil {
		http.Error(w, "invalid user id", http.StatusBadRequest)
		return
	}

	_, 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)
	if err != nil {
		http.Error(w, "pq error", http.StatusInternalServerError)
		log.Printf("error add like: %s", err)
		return
	}

	w.WriteHeader(200)
}

func (m *Module) removeLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	params, err := readBody(r)
	if err != nil {
		http.Error(w, "invalid body", http.StatusBadRequest)
		return
	}

	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 type", http.StatusBadRequest)
		return
	}
	userId, err := strconv.Atoi(params.Get("user"))
	if err != nil {
		http.Error(w, "invalid user id", http.StatusBadRequest)
		return
	}

	_, err = m.g.DB.Exec(`DELETE FROM grilist.likes WHERE content = $1 AND "user" = $2 AND type = $3`, contentId, userId, contentType)
	if err != nil {
		http.Error(w, "pq error", http.StatusInternalServerError)
		log.Printf("error add like: %s", err)
		return
	}

	w.WriteHeader(200)
}

func readBody(r *http.Request) (url.Values, error) {
	defer r.Body.Close()
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}

	values, err := url.ParseQuery(string(data))
	if err != nil {
		return nil, err
	}

	return values, nil
}