diff options
| author | jan <jan@ruken.pw> | 2016-11-14 17:46:52 (UTC) |
|---|---|---|
| committer | rtz12 <koenig@fagott.pw> | 2016-11-15 17:52:28 (UTC) |
| commit | 0f62ade5a05c755f18fd4d16a2a280b7870b08b7 (patch) | |
| tree | 960bead4a69abaae2474d4af6a9b1cc36f7aa474 | |
| parent | dae4433aae4804901e3d2a36342fe1522224ce64 (diff) | |
likes funktionieren
| -rw-r--r-- | assets_src/js/like.js | 75 | ||||
| -rw-r--r-- | modules/likes/likes.go | 120 | ||||
| -rw-r--r-- | views/includes/like.html | 4 | ||||
| -rw-r--r-- | views/pages/list.html | 2 |
4 files changed, 196 insertions, 5 deletions
diff --git a/assets_src/js/like.js b/assets_src/js/like.js index 03b15a1..feae345 100644 --- a/assets_src/js/like.js +++ b/assets_src/js/like.js | |||
| @@ -11,12 +11,81 @@ async function updateLikeCount(el) { | |||
| 11 | 11 | ||
| 12 | const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {}); | 12 | const count = await ajax.get(`/api/likes/count?id=${contentId}&type=${type}`, {}); |
| 13 | 13 | ||
| 14 | el.textContent = count; | 14 | dom.firstChild(el, e => e.classList.contains('like-count')).textContent = count; |
| 15 | } | ||
| 16 | |||
| 17 | async function isLikedByCurrentUser(el) { | ||
| 18 | const type = parseInt(el.getAttribute('content-type'), 10); | ||
| 19 | const contentId = parseInt(el.getAttribute('content-id'), 10); | ||
| 20 | const userId = parseInt(el.getAttribute('update-with'), 10); | ||
| 21 | |||
| 22 | if (isNaN(type) || isNaN(contentId) || isNaN(userId)) { | ||
| 23 | return; | ||
| 24 | } | ||
| 25 | |||
| 26 | const b = await ajax.get(`/api/likes/liked_by?id=${contentId}&type=${type}&user=${userId}`, {}); | ||
| 27 | |||
| 28 | console.log(b); | ||
| 29 | return b === 'true'; | ||
| 30 | } | ||
| 31 | |||
| 32 | async function updateLikeByCurrentUser(el, to) { | ||
| 33 | const type = parseInt(el.getAttribute('content-type'), 10); | ||
| 34 | const contentId = parseInt(el.getAttribute('content-id'), 10); | ||
| 35 | const userId = parseInt(el.getAttribute('update-with'), 10); | ||
| 36 | |||
| 37 | if (isNaN(type) || isNaN(contentId) || isNaN(userId)) { | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | updateLikeClass(el, to); | ||
| 42 | |||
| 43 | if (to) { | ||
| 44 | await ajax.put('/api/likes', `id=${contentId}&type=${type}&user=${userId}`, { | ||
| 45 | headers: { | ||
| 46 | 'Content-Type': 'application/x-www-form-urlencoded', | ||
| 47 | } | ||
| 48 | }); | ||
| 49 | } else { | ||
| 50 | await ajax.del('/api/likes', `id=${contentId}&type=${type}&user=${userId}`, { | ||
| 51 | headers: { | ||
| 52 | 'Content-Type': 'application/x-www-form-urlencoded', | ||
| 53 | } | ||
| 54 | }); | ||
| 55 | } | ||
| 56 | const cont = dom.firstChild(el, e => e.classList.contains('like-count')); | ||
| 57 | |||
| 58 | cont.textContent = parseInt(cont.textContent, 10) + (to ? 1 : -1); | ||
| 59 | } | ||
| 60 | |||
| 61 | function updateLikeClass(el, to) { | ||
| 62 | el.classList.toggle('liked', to); | ||
| 15 | } | 63 | } |
| 16 | 64 | ||
| 17 | dom.ready(() => { | 65 | dom.ready(() => { |
| 18 | dom.withClass('like-count') | 66 | dom.withClass('like-div') |
| 19 | .forEach(el => { | 67 | .forEach(async el => { |
| 20 | updateLikeCount(el); | 68 | updateLikeCount(el); |
| 69 | |||
| 70 | const userId = parseInt(el.getAttribute('update-with'), 10); | ||
| 71 | |||
| 72 | let liked = await isLikedByCurrentUser(el); | ||
| 73 | updateLikeClass(el, liked); | ||
| 74 | if (!isNaN(userId)) { | ||
| 75 | el.addEventListener('click', async () => { | ||
| 76 | liked = !liked; | ||
| 77 | await updateLikeByCurrentUser(el, liked); | ||
| 78 | }); | ||
| 79 | const cap = dom.firstChild(el, e => e.classList.contains('like-caption')); | ||
| 80 | if (cap) { | ||
| 81 | el.addEventListener('mouseover', () => { | ||
| 82 | cap.textContent = `${liked ? 'nicht mehr ' : ''}geil finden`; | ||
| 83 | }); | ||
| 84 | el.addEventListener('mouseout', () => cap.textContent = 'Finden das geil'); | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | el.classList.add('disabled'); | ||
| 88 | } | ||
| 89 | |||
| 21 | }); | 90 | }); |
| 22 | }); | 91 | }); |
diff --git a/modules/likes/likes.go b/modules/likes/likes.go index 949cba2..4d69d7b 100644 --- a/modules/likes/likes.go +++ b/modules/likes/likes.go | |||
| @@ -2,11 +2,14 @@ package likes | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "io/ioutil" | ||
| 5 | "log" | 6 | "log" |
| 6 | "net/http" | 7 | "net/http" |
| 8 | "net/url" | ||
| 7 | "strconv" | 9 | "strconv" |
| 8 | 10 | ||
| 9 | "fagott.pw/charakterin" | 11 | "fagott.pw/charakterin" |
| 12 | "fagott.pw/grilist/cache" | ||
| 10 | "fagott.pw/grilist/grilist" | 13 | "fagott.pw/grilist/grilist" |
| 11 | "fagott.pw/grilist/modules/lists" | 14 | "fagott.pw/grilist/modules/lists" |
| 12 | 15 | ||
| @@ -15,6 +18,7 @@ import ( | |||
| 15 | 18 | ||
| 16 | type Module struct { | 19 | type Module struct { |
| 17 | g *grilist.Grilist | 20 | g *grilist.Grilist |
| 21 | c *cache.Cache | ||
| 18 | lists *lists.Module | 22 | lists *lists.Module |
| 19 | } | 23 | } |
| 20 | 24 | ||
| @@ -33,7 +37,11 @@ func (m *Module) Init(g *grilist.Grilist) { | |||
| 33 | log.Fatal("tags: lists module not found") | 37 | log.Fatal("tags: lists module not found") |
| 34 | } | 38 | } |
| 35 | m.lists = gm.(*lists.Module) | 39 | m.lists = gm.(*lists.Module) |
| 40 | m.c = cache.New() | ||
| 36 | m.g.Router.GET("/api/likes/count", m.getLikeCount) | 41 | m.g.Router.GET("/api/likes/count", m.getLikeCount) |
| 42 | m.g.Router.GET("/api/likes/liked_by", m.isLikedBy) | ||
| 43 | m.g.Router.PUT("/api/likes", m.addLike) | ||
| 44 | m.g.Router.DELETE("/api/likes", m.removeLike) | ||
| 37 | } | 45 | } |
| 38 | 46 | ||
| 39 | func (m *Module) Interface() interface{} { | 47 | func (m *Module) Interface() interface{} { |
| @@ -68,3 +76,115 @@ func (m *Module) getLikeCount(w http.ResponseWriter, r *http.Request, p httprout | |||
| 68 | w.WriteHeader(200) | 76 | w.WriteHeader(200) |
| 69 | w.Write([]byte(fmt.Sprintf("%d", res))) | 77 | w.Write([]byte(fmt.Sprintf("%d", res))) |
| 70 | } | 78 | } |
| 79 | func (m *Module) isLikedBy(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
| 80 | params := r.URL.Query() | ||
| 81 | |||
| 82 | contentId, err := strconv.Atoi(params.Get("id")) | ||
| 83 | if err != nil { | ||
| 84 | http.Error(w, "invalid content id", http.StatusBadRequest) | ||
| 85 | return | ||
| 86 | } | ||
| 87 | contentType, err := strconv.Atoi(params.Get("type")) | ||
| 88 | if err != nil { | ||
| 89 | http.Error(w, "invalid content type", http.StatusBadRequest) | ||
| 90 | return | ||
| 91 | } | ||
| 92 | userId, err := strconv.Atoi(params.Get("user")) | ||
| 93 | if err != nil { | ||
| 94 | http.Error(w, "invalid user id", http.StatusBadRequest) | ||
| 95 | return | ||
| 96 | } | ||
| 97 | |||
| 98 | var res int | ||
| 99 | 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 { | ||
| 100 | http.Error(w, "pq error", http.StatusInternalServerError) | ||
| 101 | log.Printf("error getting like by user: %s", err) | ||
| 102 | return | ||
| 103 | } | ||
| 104 | |||
| 105 | w.WriteHeader(200) | ||
| 106 | w.Write([]byte(fmt.Sprintf("%t", res > 0))) | ||
| 107 | } | ||
| 108 | func (m *Module) addLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
| 109 | params, err := readBody(r) | ||
| 110 | if err != nil { | ||
| 111 | log.Println(err) | ||
| 112 | http.Error(w, "invalid body", http.StatusBadRequest) | ||
| 113 | return | ||
| 114 | } | ||
| 115 | |||
| 116 | contentId, err := strconv.Atoi(params.Get("id")) | ||
| 117 | if err != nil { | ||
| 118 | log.Println(err) | ||
| 119 | http.Error(w, "invalid content id", http.StatusBadRequest) | ||
| 120 | return | ||
| 121 | } | ||
| 122 | contentType, err := strconv.Atoi(params.Get("type")) | ||
| 123 | if err != nil { | ||
| 124 | log.Println(err) | ||
| 125 | http.Error(w, "invalid content type", http.StatusBadRequest) | ||
| 126 | return | ||
| 127 | } | ||
| 128 | userId, err := strconv.Atoi(params.Get("user")) | ||
| 129 | if err != nil { | ||
| 130 | http.Error(w, "invalid user id", http.StatusBadRequest) | ||
| 131 | return | ||
| 132 | } | ||
| 133 | |||
| 134 | _, 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) | ||
| 135 | if err != nil { | ||
| 136 | http.Error(w, "pq error", http.StatusInternalServerError) | ||
| 137 | log.Printf("error add like: %s", err) | ||
| 138 | return | ||
| 139 | } | ||
| 140 | |||
| 141 | w.WriteHeader(200) | ||
| 142 | } | ||
| 143 | |||
| 144 | func (m *Module) removeLike(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
| 145 | params, err := readBody(r) | ||
| 146 | if err != nil { | ||
| 147 | http.Error(w, "invalid body", http.StatusBadRequest) | ||
| 148 | return | ||
| 149 | } | ||
| 150 | |||
| 151 | contentId, err := strconv.Atoi(params.Get("id")) | ||
| 152 | if err != nil { | ||
| 153 | http.Error(w, "invalid content id", http.StatusBadRequest) | ||
| 154 | return | ||
| 155 | } | ||
| 156 | contentType, err := strconv.Atoi(params.Get("type")) | ||
| 157 | if err != nil { | ||
| 158 | http.Error(w, "invalid content type", http.StatusBadRequest) | ||
| 159 | return | ||
| 160 | } | ||
| 161 | userId, err := strconv.Atoi(params.Get("user")) | ||
| 162 | if err != nil { | ||
| 163 | http.Error(w, "invalid user id", http.StatusBadRequest) | ||
| 164 | return | ||
| 165 | } | ||
| 166 | |||
| 167 | _, err = m.g.DB.Exec(`DELETE FROM grilist.likes WHERE content = $1 AND "user" = $2 AND type = $3`, contentId, userId, contentType) | ||
| 168 | if err != nil { | ||
| 169 | http.Error(w, "pq error", http.StatusInternalServerError) | ||
| 170 | log.Printf("error add like: %s", err) | ||
| 171 | return | ||
| 172 | } | ||
| 173 | |||
| 174 | w.WriteHeader(200) | ||
| 175 | } | ||
| 176 | |||
| 177 | func readBody(r *http.Request) (url.Values, error) { | ||
| 178 | defer r.Body.Close() | ||
| 179 | data, err := ioutil.ReadAll(r.Body) | ||
| 180 | if err != nil { | ||
| 181 | return nil, err | ||
| 182 | } | ||
| 183 | |||
| 184 | values, err := url.ParseQuery(string(data)) | ||
| 185 | if err != nil { | ||
| 186 | return nil, err | ||
| 187 | } | ||
| 188 | |||
| 189 | return values, nil | ||
| 190 | } | ||
diff --git a/views/includes/like.html b/views/includes/like.html index 226aaff..bfb9576 100644 --- a/views/includes/like.html +++ b/views/includes/like.html | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | {{ define "like" }} | 1 | {{ define "like" }} |
| 2 | <span class="like-count" content-type={{ .ContentType }} content-id={{ .ContentID }}></span> Likes | 2 | <div class="like-div btn purple lighten-4" {{ if .User }}update-with="{{ .User.ID }}"{{ end }} content-type={{ .ContentType }} content-id={{ .ContentID }}> |
| 3 | <span class="like-count"></span> <span class="like-caption">Finden das geil</span> | ||
| 4 | </div> | ||
| 3 | {{ end }} | 5 | {{ end }} |
diff --git a/views/pages/list.html b/views/pages/list.html index 3c95548..3676241 100644 --- a/views/pages/list.html +++ b/views/pages/list.html | |||
| @@ -35,7 +35,7 @@ | |||
| 35 | </div> | 35 | </div> |
| 36 | </div>{{ end }} | 36 | </div>{{ end }} |
| 37 | <blockquote>{{ $list.Description }}</blockquote><br /> | 37 | <blockquote>{{ $list.Description }}</blockquote><br /> |
| 38 | {{ template "like" (map "ContentType" 0 "ContentID" $list.ID) }} | 38 | {{ template "like" (map "ContentType" 0 "ContentID" $list.ID "User" $user) }} |
| 39 | <div class="row"> | 39 | <div class="row"> |
| 40 | <div class="col s12 {{ if ($user) and eq $user.ID $list.Owner.ID }}l8{{ end }}"> | 40 | <div class="col s12 {{ if ($user) and eq $user.ID $list.Owner.ID }}l8{{ end }}"> |
| 41 | <ul id="gril-list" class="gril-list"> | 41 | <ul id="gril-list" class="gril-list"> |
