From c735553214400f3af69e516ff41a6da9214449a7 Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 14 Nov 2016 16:55:50 +0100 Subject: likes und so. diff --git a/assets_src/js/lib/dom.js b/assets_src/js/lib/dom.js index bfbeea7..3c2f4d9 100644 --- a/assets_src/js/lib/dom.js +++ b/assets_src/js/lib/dom.js @@ -17,6 +17,16 @@ export function closest(el, fn) { return null; } +export function withClass(className) { + const els = document.getElementsByClassName(className); + const arr = []; + + for (let i = 0; i < els.length; i++) { + arr.push(els.item(i)); + } + return arr; +} + export function next(el, fn) { if (!el) { return null; diff --git a/assets_src/js/like.js b/assets_src/js/like.js new file mode 100644 index 0000000..03b15a1 --- /dev/null +++ b/assets_src/js/like.js @@ -0,0 +1,22 @@ +import * as ajax from './lib/ajax'; +import * as dom from './lib/dom'; + +async function updateLikeCount(el) { + const type = parseInt(el.getAttribute('content-type'), 10); + const contentId = parseInt(el.getAttribute('content-id'), 10); + + if (isNaN(type) || isNaN(contentId)) { + return; + } + + const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {}); + + el.textContent = count; +} + +dom.ready(() => { + dom.withClass('like-count') + .forEach(el => { + updateLikeCount(el); + }); +}); diff --git a/frontend/renderer.go b/frontend/renderer.go index d529dff..9c1f7a7 100644 --- a/frontend/renderer.go +++ b/frontend/renderer.go @@ -65,6 +65,15 @@ func New(path string, functions map[string]interface{}) *Renderer { "makeRangePair": func(idx int, value interface{}) RangePair { return RangePair{idx, value} }, + "map": func(values ...interface{}) map[string]interface{} { + m := make(map[string]interface{}, len(values)/2) + for i := 0; i < len(values); i += 2 { + key, _ := values[i].(string) + m[key] = values[i+1] + } + + return m + }, "makeObject": func(name string, existing interface{}, newKey string, newData interface{}) map[string]interface{} { d := make(map[string]interface{}) d[name] = existing diff --git a/main.go b/main.go index dd00a9b..02ef9e7 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" + "fagott.pw/grilist/modules/likes" "fagott.pw/grilist/modules/lists" "fagott.pw/grilist/modules/search" "fagott.pw/grilist/modules/series" @@ -106,6 +107,7 @@ func main() { loadModule(search.New()) loadModule(user.New()) loadModule(series.New()) + loadModule(likes.New()) router.GET("/dashboard", viewDashboard) log.Fatal(http.ListenAndServe(":8080", nil)) diff --git a/models/like.go b/models/like.go new file mode 100644 index 0000000..2faf384 --- /dev/null +++ b/models/like.go @@ -0,0 +1,14 @@ +package models + +import "fagott.pw/charakterin" + +const ( + LIKE_LIST = 0 +) + +type Like struct { + ContentID int + Content interface{} + User *charakterin.User + ContentType int +} 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 @@ +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))) +} diff --git a/views/includes/js.html b/views/includes/js.html new file mode 100644 index 0000000..784c3b6 --- /dev/null +++ b/views/includes/js.html @@ -0,0 +1,3 @@ +{{ define "js" }} + +{{ end }} diff --git a/views/includes/like.html b/views/includes/like.html new file mode 100644 index 0000000..226aaff --- /dev/null +++ b/views/includes/like.html @@ -0,0 +1,3 @@ +{{ define "like" }} + Likes +{{ end }} diff --git a/views/pages/list.html b/views/pages/list.html index f3c652b..f884a63 100644 --- a/views/pages/list.html +++ b/views/pages/list.html @@ -4,6 +4,7 @@ {{ template "materialize" }} + {{ template "js" "like" }} grilist @@ -24,6 +25,7 @@ {{ end }}
{{ $list.Description }}

+ {{ template "like" (map "ContentType" 0 "ContentID" $list.ID) }}
    -- cgit v0.10.1