aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets_src/css/list.css12
-rw-r--r--assets_src/js/gril.js84
-rw-r--r--assets_src/js/list.js70
-rw-r--r--cache/cache.go54
-rw-r--r--dashboard.go5
-rw-r--r--main.go11
-rw-r--r--modules/grils/grils.go128
-rw-r--r--modules/lists/lists.go15
-rw-r--r--views/includes/card_gril.html16
-rw-r--r--views/includes/navbar.html32
10 files changed, 217 insertions, 210 deletions
diff --git a/assets_src/css/list.css b/assets_src/css/list.css
index 7e6dde9..51b2514 100644
--- a/assets_src/css/list.css
+++ b/assets_src/css/list.css
@@ -19,9 +19,9 @@
19} 19}
20 20
21.gril-img { 21.gril-img {
22 background-repeat: no-repeat; 22 background-repeat: no-repeat;
23 background-position: center center; 23 background-position: center center;
24 background-size: cover; 24 background-size: cover;
25 width: 64px; 25 width: 64px;
26 height: 64px; 26 height: 64px;
27} \ No newline at end of file 27}
diff --git a/assets_src/js/gril.js b/assets_src/js/gril.js
index 561c221..7d92b5a 100644
--- a/assets_src/js/gril.js
+++ b/assets_src/js/gril.js
@@ -3,45 +3,45 @@ import * as dom from './lib/dom';
3import * as ajax from './lib/ajax'; 3import * as ajax from './lib/ajax';
4 4
5dom.ready(() => { 5dom.ready(() => {
6 $('.modal-trigger').leanModal({ 6 $('.modal-trigger').leanModal({
7 async ready() { 7 async ready() {
8 const el = document.getElementById('modal-add-gril-content'); 8 const el = document.getElementById('modal-add-gril-content');
9 9
10 el.innerHTML = ` 10 el.innerHTML = `
11 <div class="progress"> 11 <div class="progress">
12 <div class="indeterminate"></div> 12 <div class="indeterminate"></div>
13 </div>`; 13 </div>`;
14 14
15 let data = {}; 15 let data = {};
16 try { 16 try {
17 data = JSON.parse(await ajax.get('/api/lists/user')); 17 data = JSON.parse(await ajax.get('/api/lists/user'));
18 } catch(e) { 18 } catch(e) {
19 console.error(e); 19 console.error(e);
20 } 20 }
21 21
22 el.innerHTML = ''; 22 el.innerHTML = '';
23 23
24 data.forEach(d => { 24 data.forEach(d => {
25 const element = document.createElement('a'); 25 const element = document.createElement('a');
26 element.innerHTML = d.Name; 26 element.innerHTML = d.Name;
27 element.className = 'collection-item purple-text text-lighten-2'; 27 element.className = 'collection-item purple-text text-lighten-2';
28 element.addEventListener('click', async () => { 28 element.addEventListener('click', async () => {
29 try { 29 try {
30 await ajax.post( 30 await ajax.post(
31 `/list/${d.ID}`, 31 `/list/${d.ID}`,
32 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1], 32 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1],
33 { headers: { 33 { headers: {
34 'Content-type': 'application/x-www-form-urlencoded' 34 'Content-type': 'application/x-www-form-urlencoded'
35 } }); 35 } });
36 $('#modal_add_gril').closeModal(); 36 $('#modal_add_gril').closeModal();
37 } catch(e) { 37 } catch(e) {
38 console.error(e); 38 console.error(e);
39 } 39 }
40 40
41 return true; 41 return true;
42 }); 42 });
43 el.appendChild(element); 43 el.appendChild(element);
44 }); 44 });
45 }, 45 },
46 }); 46 });
47}); \ No newline at end of file 47});
diff --git a/assets_src/js/list.js b/assets_src/js/list.js
index 7a4f69e..7af7444 100644
--- a/assets_src/js/list.js
+++ b/assets_src/js/list.js
@@ -20,24 +20,26 @@ async function resultClicked(grilId) {
20} 20}
21 21
22async function updateGrilOrder(gril, pos) { 22async function updateGrilOrder(gril, pos) {
23 return await ajax.post( 23 return await ajax.post(
24 `${window.location}/order`, 24 `${window.location}/order`,
25 `gril=${gril}&pos=${pos}`, 25 `gril=${gril}&pos=${pos}`,
26 { headers: { 26 {
27 'Content-Type': 'application/x-www-form-urlencoded', 27 headers: {
28 } }); 28 'Content-Type': 'application/x-www-form-urlencoded',
29 }
30 });
29} 31}
30 32
31function getCurrentOrder(gril) { 33function getCurrentOrder(gril) {
32 const els = document.querySelectorAll('#gril-list > li') 34 const els = document.querySelectorAll('#gril-list > li')
33 35
34 for (let i = 0; i < els.length; i++) { 36 for (let i = 0; i < els.length; i++) {
35 if (els[i].id === gril) { 37 if (els[i].id === gril) {
36 console.log(i); 38 console.log(i);
37 return i; 39 return i;
38 } 40 }
39 } 41 }
40 return -1; 42 return -1;
41} 43}
42 44
43async function listButtonHandler(e) { 45async function listButtonHandler(e) {
@@ -47,20 +49,20 @@ async function listButtonHandler(e) {
47 nItem = dom.next(dom.next(item, liFilter), liFilter); 49 nItem = dom.next(dom.next(item, liFilter), liFilter);
48 switch (e.target.classList[0]) { 50 switch (e.target.classList[0]) {
49 case 'gril-sort-up': 51 case 'gril-sort-up':
50 try { 52 try {
51 await updateGrilOrder(item.id, getCurrentOrder(item.id) - 1); 53 await updateGrilOrder(item.id, getCurrentOrder(item.id) - 1);
52 } catch(e) { 54 } catch(e) {
53 alert(e.message); 55 alert(e.message);
54 } 56 }
55 item.parentNode.insertBefore(item, pItem); 57 item.parentNode.insertBefore(item, pItem);
56 break; 58 break;
57 case 'gril-sort-down': 59 case 'gril-sort-down':
58 try { 60 try {
59 await updateGrilOrder(item.id, getCurrentOrder(item.id) + 1); 61 await updateGrilOrder(item.id, getCurrentOrder(item.id) + 1);
60 } catch(e) { 62 } catch(e) {
61 alert(e.message); 63 alert(e.message);
62 } 64 }
63 65
64 if (nItem) { 66 if (nItem) {
65 item.parentNode.insertBefore(item, nItem); 67 item.parentNode.insertBefore(item, nItem);
66 } else { 68 } else {
@@ -68,16 +70,18 @@ async function listButtonHandler(e) {
68 } 70 }
69 break; 71 break;
70 case 'gril-delete': 72 case 'gril-delete':
71 let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; 73 let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li';
72 let item = dom.closest(e.target, liFilter); 74 let item = dom.closest(e.target, liFilter);
73 try { 75 try {
74 await ajax.del( 76 await ajax.del(
75 `${window.location}/order`, 77 `${window.location}/order`,
76 `gril=${item.id}`, 78 `gril=${item.id}`,
77 { headers: { 79 {
78 'Content-Type': 'application/x-www-form-urlencoded', 80 headers: {
79 } }); 81 'Content-Type': 'application/x-www-form-urlencoded',
80 item.parentNode.removeChild(item); 82 }
83 });
84 item.parentNode.removeChild(item);
81 } catch(e) { 85 } catch(e) {
82 alert(e.message); 86 alert(e.message);
83 } 87 }
diff --git a/cache/cache.go b/cache/cache.go
index f6e5661..9647836 100644
--- a/cache/cache.go
+++ b/cache/cache.go
@@ -1,56 +1,56 @@
1package cache 1package cache
2 2
3import ( 3import (
4 "time" 4 "log"
5 "log" 5 "time"
6) 6)
7 7
8type CacheItem struct { 8type CacheItem struct {
9 Expires time.Time 9 Expires time.Time
10 Value interface{} 10 Value interface{}
11} 11}
12type Cache struct { 12type Cache struct {
13 items map[int]CacheItem 13 items map[int]CacheItem
14} 14}
15 15
16func New() *Cache { 16func New() *Cache {
17 c := &Cache{ make(map[int]CacheItem) } 17 c := &Cache{make(map[int]CacheItem)}
18 go c.ticker() 18 go c.ticker()
19 return c 19 return c
20} 20}
21 21
22func (c *Cache) ticker() { 22func (c *Cache) ticker() {
23 for _ = range time.NewTicker(time.Second * 1).C { 23 for _ = range time.NewTicker(time.Second * 1).C {
24 t := time.Now() 24 t := time.Now()
25 for key, item := range c.items { 25 for key, item := range c.items {
26 if t.Sub(item.Expires) > 0 { 26 if t.Sub(item.Expires) > 0 {
27 log.Println("cache delete", key) 27 log.Println("cache delete", key)
28 delete(c.items, key) 28 delete(c.items, key)
29 } 29 }
30 } 30 }
31 } 31 }
32} 32}
33 33
34func (c *Cache) Insert(key int, item interface{}) { 34func (c *Cache) Insert(key int, item interface{}) {
35 c.items[key] = CacheItem{ time.Now().Add(time.Minute * 15), item } 35 c.items[key] = CacheItem{time.Now().Add(time.Minute * 15), item}
36} 36}
37 37
38func (c *Cache) Remove(key int) { 38func (c *Cache) Remove(key int) {
39 delete(c.items, key) 39 delete(c.items, key)
40} 40}
41 41
42func (c *Cache) Clear() { 42func (c *Cache) Clear() {
43 c.items = make(map[int]CacheItem) 43 c.items = make(map[int]CacheItem)
44} 44}
45 45
46func (c *Cache) Get(key int) (interface{}, bool) { 46func (c *Cache) Get(key int) (interface{}, bool) {
47 if ci, ok := c.items[key]; ok { 47 if ci, ok := c.items[key]; ok {
48 return ci.Value, true 48 return ci.Value, true
49 } 49 }
50 return nil, false 50 return nil, false
51} 51}
52 52
53func (c *Cache) Has(key int) bool { 53func (c *Cache) Has(key int) bool {
54 _, ok := c.items[key] 54 _, ok := c.items[key]
55 return ok 55 return ok
56} \ No newline at end of file 56}
diff --git a/dashboard.go b/dashboard.go
index ad3c565..64a0240 100644
--- a/dashboard.go
+++ b/dashboard.go
@@ -1,12 +1,13 @@
1package main 1package main
2 2
3import ( 3import (
4 "fagott.pw/grilist/grilist"
5 "github.com/julienschmidt/httprouter"
6 "log" 4 "log"
7 "net/http" 5 "net/http"
8 "sync" 6 "sync"
9 "time" 7 "time"
8
9 "fagott.pw/grilist/grilist"
10 "github.com/julienschmidt/httprouter"
10) 11)
11 12
12func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 13func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
diff --git a/main.go b/main.go
index dd24d01..68a9ede 100644
--- a/main.go
+++ b/main.go
@@ -15,18 +15,19 @@ import (
15 "fagott.pw/grilist/modules/tags" 15 "fagott.pw/grilist/modules/tags"
16 "fagott.pw/grilist/modules/user" 16 "fagott.pw/grilist/modules/user"
17 17
18 "strings"
19
18 "github.com/julienschmidt/httprouter" 20 "github.com/julienschmidt/httprouter"
19 _ "github.com/lib/pq" 21 _ "github.com/lib/pq"
20 "strings"
21) 22)
22 23
23var app *grilist.Grilist 24var app *grilist.Grilist
24 25
25func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 26func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
26 if strings.Index(r.Referer(), "127.0.0.1") != -1 { 27 if strings.Index(r.Referer(), "127.0.0.1") != -1 {
27 w.WriteHeader(200) 28 w.WriteHeader(200)
28 return 29 return
29 } 30 }
30 http.Redirect(w, r, "/dashboard", 301) 31 http.Redirect(w, r, "/dashboard", 301)
31} 32}
32 33
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index 116b689..3ff03ec 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -6,20 +6,20 @@ import (
6 "net/http" 6 "net/http"
7 "regexp" 7 "regexp"
8 "strconv" 8 "strconv"
9 "time"
10 "strings" 9 "strings"
10 "time"
11 11
12 "fagott.pw/charakterin" 12 "fagott.pw/charakterin"
13 "fagott.pw/grilist/cache"
13 "fagott.pw/grilist/frontend" 14 "fagott.pw/grilist/frontend"
14 "fagott.pw/grilist/grilist" 15 "fagott.pw/grilist/grilist"
15 "fagott.pw/grilist/cache"
16 16
17 "github.com/julienschmidt/httprouter" 17 "github.com/julienschmidt/httprouter"
18) 18)
19 19
20type CachedGril struct { 20type CachedGril struct {
21 Created time.Time 21 Created time.Time
22 Gril *Gril 22 Gril *Gril
23} 23}
24 24
25var ( 25var (
@@ -38,7 +38,7 @@ func findIdx() {
38 38
39type GrilsModule struct { 39type GrilsModule struct {
40 g *grilist.Grilist 40 g *grilist.Grilist
41 c *cache.Cache 41 c *cache.Cache
42} 42}
43 43
44func (m *GrilsModule) Name() string { 44func (m *GrilsModule) Name() string {
@@ -50,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) {
50 m.g = g 50 m.g = g
51 m.g.Router.GET("/gril/:id", m.viewGril) 51 m.g.Router.GET("/gril/:id", m.viewGril)
52 m.g.Router.GET("/gril/:id/*rest", m.viewGril) 52 m.g.Router.GET("/gril/:id/*rest", m.viewGril)
53 53
54 m.c = cache.New() 54 m.c = cache.New()
55} 55}
56 56
57func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { 57func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) {
@@ -152,77 +152,77 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
152} 152}
153 153
154func (m *GrilsModule) FromID(id int) (*Gril, error) { 154func (m *GrilsModule) FromID(id int) (*Gril, error) {
155 if g, ok := m.c.Get(id); ok { 155 if g, ok := m.c.Get(id); ok {
156 return g.(*Gril), nil 156 return g.(*Gril), nil
157 } 157 }
158 158
159 gril := &Gril{ 159 gril := &Gril{
160 ID: id, 160 ID: id,
161 } 161 }
162 t1 := time.Now() 162 t1 := time.Now()
163 err := m.g.DB.QueryRow(`SELECT updated_at, age, birthday FROM grilist.grils WHERE id = $1`, id).Scan(&gril.UpdatedAt, &gril.Age, &gril.Birthday) 163 err := m.g.DB.QueryRow(`SELECT updated_at, age, birthday FROM grilist.grils WHERE id = $1`, id).Scan(&gril.UpdatedAt, &gril.Age, &gril.Birthday)
164 log.Printf("get_gril_from_id_raw: %dms", time.Since(t1).Nanoseconds()/1000000) 164 log.Printf("get_gril_from_id_raw: %dms", time.Since(t1).Nanoseconds()/1000000)
165 165
166 if err != nil { 166 if err != nil {
167 return nil, err 167 return nil, err
168 } 168 }
169 169
170 // Namen rausholen 170 // Namen rausholen
171 rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id) 171 rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id)
172 if err != nil { 172 if err != nil {
173 return nil, err 173 return nil, err
174 } 174 }
175 log.Printf("get_gril_from_id_names: %dms", time.Since(t1).Nanoseconds()/1000000) 175 log.Printf("get_gril_from_id_names: %dms", time.Since(t1).Nanoseconds()/1000000)
176 defer rows.Close() 176 defer rows.Close()
177 177
178 for rows.Next() { 178 for rows.Next() {
179 var name string 179 var name string
180 var name_type int 180 var name_type int
181 if err := rows.Scan(&name, &name_type); err != nil { 181 if err := rows.Scan(&name, &name_type); err != nil {
182 return nil, err 182 return nil, err
183 } 183 }
184 184
185 switch name_type { 185 switch name_type {
186 case 0: 186 case 0:
187 gril.KanjiName = name 187 gril.KanjiName = name
188 break 188 break
189 case 1: 189 case 1:
190 gril.RomajiName = name 190 gril.RomajiName = name
191 default: 191 default:
192 gril.OtherNames = append(gril.OtherNames, name) 192 gril.OtherNames = append(gril.OtherNames, name)
193 } 193 }
194 }
195 /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id)
196 if err != nil {
197 return nil, err
198 }
199 defer rows.Close()
200
201 for rows.Next() {
202 var name string
203 if err := rows.Scan(&name); err != nil {
204 return nil, err
205 }
206 gril.OtherNames = append(gril.OtherNames, name)
207 }
208 rows.Close()
209
210 // Tags holen
211 rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id)
212 if err != nil {
213 return nil, err
214 } 194 }
215 defer rows.Close() 195 /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id)
216 196 if err != nil {
217 for rows.Next() { 197 return nil, err
218 var name string 198 }
219 if err := rows.Scan(&name); err != nil { 199 defer rows.Close()
220 return nil, err 200
221 } 201 for rows.Next() {
222 gril.Tags = append(gril.Tags, name) 202 var name string
223 }*/ 203 if err := rows.Scan(&name); err != nil {
224 204 return nil, err
225 m.c.Insert(id, gril) 205 }
206 gril.OtherNames = append(gril.OtherNames, name)
207 }
208 rows.Close()
209
210 // Tags holen
211 rows, err := m.g.DB.Query(`SELECT name FROM grilist.tags WHERE id IN (SELECT tag_id FROM grilist.grils_tags WHERE gril_id = $1)`, id)
212 if err != nil {
213 return nil, err
214 }
215 defer rows.Close()
216
217 for rows.Next() {
218 var name string
219 if err := rows.Scan(&name); err != nil {
220 return nil, err
221 }
222 gril.Tags = append(gril.Tags, name)
223 }*/
224
225 m.c.Insert(id, gril)
226 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) 226 log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000)
227 return gril, nil 227 return gril, nil
228} 228}
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
index 90de18c..67856fe 100644
--- a/modules/lists/lists.go
+++ b/modules/lists/lists.go
@@ -4,20 +4,21 @@ import (
4 "database/sql" 4 "database/sql"
5 "encoding/json" 5 "encoding/json"
6 "errors" 6 "errors"
7 "fagott.pw/charakterin"
8 "fagott.pw/grilist/cache"
9 "fagott.pw/grilist/frontend"
10 "fagott.pw/grilist/grilist"
11 "fagott.pw/grilist/modules/grils"
12 "fmt" 7 "fmt"
13 "github.com/julienschmidt/httprouter"
14 "github.com/lib/pq"
15 "io/ioutil" 8 "io/ioutil"
16 "log" 9 "log"
17 "net/http" 10 "net/http"
18 "net/url" 11 "net/url"
19 "sort" 12 "sort"
20 "strconv" 13 "strconv"
14
15 "fagott.pw/charakterin"
16 "fagott.pw/grilist/cache"
17 "fagott.pw/grilist/frontend"
18 "fagott.pw/grilist/grilist"
19 "fagott.pw/grilist/modules/grils"
20 "github.com/julienschmidt/httprouter"
21 "github.com/lib/pq"
21) 22)
22 23
23// Module und so. 24// Module und so.
diff --git a/views/includes/card_gril.html b/views/includes/card_gril.html
index 1cd45fa..4166c9a 100644
--- a/views/includes/card_gril.html
+++ b/views/includes/card_gril.html
@@ -1,11 +1,11 @@
1{{ define "card_gril" }} 1{{ define "card_gril" }}
2<div class="card white"> 2<div class="card white">
3 <div class="center-align" style="padding-top: 8px"> 3 <div class="center-align" style="padding-top: 8px">
4 <img class="circle responsive-img" src="/{{ .ImagePath false }}" /> 4 <img class="circle responsive-img" src="/{{ .ImagePath false }}" />
5 </div> 5 </div>
6 <div class="card-content black-text"> 6 <div class="card-content black-text">
7 <span class="card-title">{{ .RomajiName }} <small class="grey-text">{{ .KanjiName }}</small></span> 7 <span class="card-title">{{ .RomajiName }} <small class="grey-text">{{ .KanjiName }}</small></span>
8 </div> 8 </div>
9 <div class="card-action"><a href="/gril/{{ .Slug }}">anguckieren</a></div> 9 <div class="card-action"><a href="/gril/{{ .Slug }}">anguckieren</a></div>
10</div> 10</div>
11{{ end }} \ No newline at end of file 11{{ end }}
diff --git a/views/includes/navbar.html b/views/includes/navbar.html
index a0db664..23514f8 100644
--- a/views/includes/navbar.html
+++ b/views/includes/navbar.html
@@ -1,17 +1,17 @@
1{{ define "navbar" }} 1{{ define "navbar" }}
2 <nav> 2<nav>
3 <div class="nav-wrapper purple lighten-2"> 3 <div class="nav-wrapper purple lighten-2">
4 <a href="/" class="brand-logo center">grilist</a> 4 <a href="/" class="brand-logo center">grilist</a>
5 <ul id="nav-mobile" class="right hide-on-med-and-down"> 5 <ul id="nav-mobile" class="right hide-on-med-and-down">
6 {{ if .user }} 6 {{ if .user }}
7 <li><a href="/new/list">Liste erstellen</a></li> 7 <li><a href="/new/list">Liste erstellen</a></li>
8 <li><a href="/user/{{ .user.ID }}/{{ .user.GetName }}">{{ .user.GetName }}</a></li> 8 <li><a href="/user/{{ .user.ID }}/{{ .user.GetName }}">{{ .user.GetName }}</a></li>
9 <li><a href="/logout">Ausloggen</a></li> 9 <li><a href="/logout">Ausloggen</a></li>
10 {{ else }} 10 {{ else }}
11 <li><a href="/login">Einloggen</a></li> 11 <li><a href="/login">Einloggen</a></li>
12 <li><a href="/register">Registrieren</a></li> 12 <li><a href="/register">Registrieren</a></li>
13 {{ end }} 13 {{ end }}
14 </ul> 14 </ul>
15 </div> 15 </div>
16 </nav> 16</nav>
17{{ end }} \ No newline at end of file 17{{ end }}