From c1d895a7db943b758bfbf214c3727569976f85e0 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Mon, 3 Oct 2016 14:34:36 +0200 Subject: Formatierung gefixt und go fmt Ein gewisser jemand sollte endlich mal seinen Editor richtig konfigurieren 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 @@ } .gril-img { - background-repeat: no-repeat; - background-position: center center; - background-size: cover; - width: 64px; - height: 64px; -} \ No newline at end of file + background-repeat: no-repeat; + background-position: center center; + background-size: cover; + width: 64px; + height: 64px; +} 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'; import * as ajax from './lib/ajax'; dom.ready(() => { - $('.modal-trigger').leanModal({ - async ready() { - const el = document.getElementById('modal-add-gril-content'); - - el.innerHTML = ` -
-
-
`; - - let data = {}; - try { - data = JSON.parse(await ajax.get('/api/lists/user')); - } catch(e) { - console.error(e); - } - - el.innerHTML = ''; - - data.forEach(d => { - const element = document.createElement('a'); - element.innerHTML = d.Name; - element.className = 'collection-item purple-text text-lighten-2'; - element.addEventListener('click', async () => { - try { - await ajax.post( - `/list/${d.ID}`, - 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1], - { headers: { - 'Content-type': 'application/x-www-form-urlencoded' - } }); - $('#modal_add_gril').closeModal(); - } catch(e) { - console.error(e); - } - - return true; - }); - el.appendChild(element); - }); - }, - }); -}); \ No newline at end of file + $('.modal-trigger').leanModal({ + async ready() { + const el = document.getElementById('modal-add-gril-content'); + + el.innerHTML = ` +
+
+
`; + + let data = {}; + try { + data = JSON.parse(await ajax.get('/api/lists/user')); + } catch(e) { + console.error(e); + } + + el.innerHTML = ''; + + data.forEach(d => { + const element = document.createElement('a'); + element.innerHTML = d.Name; + element.className = 'collection-item purple-text text-lighten-2'; + element.addEventListener('click', async () => { + try { + await ajax.post( + `/list/${d.ID}`, + 'id=' + window.location.href.match(/\/gril\/([0-9]{1,})\//)[1], + { headers: { + 'Content-type': 'application/x-www-form-urlencoded' + } }); + $('#modal_add_gril').closeModal(); + } catch(e) { + console.error(e); + } + + return true; + }); + el.appendChild(element); + }); + }, + }); +}); 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) { } async function updateGrilOrder(gril, pos) { - return await ajax.post( - `${window.location}/order`, - `gril=${gril}&pos=${pos}`, - { headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - } }); + return await ajax.post( + `${window.location}/order`, + `gril=${gril}&pos=${pos}`, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } + }); } function getCurrentOrder(gril) { - const els = document.querySelectorAll('#gril-list > li') - - for (let i = 0; i < els.length; i++) { - if (els[i].id === gril) { - console.log(i); - return i; - } - } - return -1; + const els = document.querySelectorAll('#gril-list > li') + + for (let i = 0; i < els.length; i++) { + if (els[i].id === gril) { + console.log(i); + return i; + } + } + return -1; } async function listButtonHandler(e) { @@ -47,20 +49,20 @@ async function listButtonHandler(e) { nItem = dom.next(dom.next(item, liFilter), liFilter); switch (e.target.classList[0]) { case 'gril-sort-up': - try { - await updateGrilOrder(item.id, getCurrentOrder(item.id) - 1); - } catch(e) { - alert(e.message); - } + try { + await updateGrilOrder(item.id, getCurrentOrder(item.id) - 1); + } catch(e) { + alert(e.message); + } item.parentNode.insertBefore(item, pItem); break; case 'gril-sort-down': - try { - await updateGrilOrder(item.id, getCurrentOrder(item.id) + 1); - } catch(e) { - alert(e.message); - } - + try { + await updateGrilOrder(item.id, getCurrentOrder(item.id) + 1); + } catch(e) { + alert(e.message); + } + if (nItem) { item.parentNode.insertBefore(item, nItem); } else { @@ -68,16 +70,18 @@ async function listButtonHandler(e) { } break; case 'gril-delete': - let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; - let item = dom.closest(e.target, liFilter); + let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; + let item = dom.closest(e.target, liFilter); try { - await ajax.del( + await ajax.del( `${window.location}/order`, `gril=${item.id}`, - { headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - } }); - item.parentNode.removeChild(item); + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } + }); + item.parentNode.removeChild(item); } catch(e) { alert(e.message); } 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 @@ package cache import ( - "time" - "log" + "log" + "time" ) type CacheItem struct { - Expires time.Time - Value interface{} + Expires time.Time + Value interface{} } type Cache struct { - items map[int]CacheItem + items map[int]CacheItem } func New() *Cache { - c := &Cache{ make(map[int]CacheItem) } - go c.ticker() - return c + c := &Cache{make(map[int]CacheItem)} + go c.ticker() + return c } func (c *Cache) ticker() { - for _ = range time.NewTicker(time.Second * 1).C { - t := time.Now() - for key, item := range c.items { - if t.Sub(item.Expires) > 0 { - log.Println("cache delete", key) - delete(c.items, key) - } - } - } + for _ = range time.NewTicker(time.Second * 1).C { + t := time.Now() + for key, item := range c.items { + if t.Sub(item.Expires) > 0 { + log.Println("cache delete", key) + delete(c.items, key) + } + } + } } func (c *Cache) Insert(key int, item interface{}) { - c.items[key] = CacheItem{ time.Now().Add(time.Minute * 15), item } + c.items[key] = CacheItem{time.Now().Add(time.Minute * 15), item} } func (c *Cache) Remove(key int) { - delete(c.items, key) + delete(c.items, key) } func (c *Cache) Clear() { - c.items = make(map[int]CacheItem) + c.items = make(map[int]CacheItem) } func (c *Cache) Get(key int) (interface{}, bool) { - if ci, ok := c.items[key]; ok { - return ci.Value, true - } - return nil, false + if ci, ok := c.items[key]; ok { + return ci.Value, true + } + return nil, false } func (c *Cache) Has(key int) bool { - _, ok := c.items[key] - return ok -} \ No newline at end of file + _, ok := c.items[key] + return ok +} diff --git a/dashboard.go b/dashboard.go index ad3c565..64a0240 100644 --- a/dashboard.go +++ b/dashboard.go @@ -1,12 +1,13 @@ package main import ( - "fagott.pw/grilist/grilist" - "github.com/julienschmidt/httprouter" "log" "net/http" "sync" "time" + + "fagott.pw/grilist/grilist" + "github.com/julienschmidt/httprouter" ) func 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 ( "fagott.pw/grilist/modules/tags" "fagott.pw/grilist/modules/user" + "strings" + "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" - "strings" ) var app *grilist.Grilist func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - if strings.Index(r.Referer(), "127.0.0.1") != -1 { - w.WriteHeader(200) - return - } + if strings.Index(r.Referer(), "127.0.0.1") != -1 { + w.WriteHeader(200) + return + } http.Redirect(w, r, "/dashboard", 301) } 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 ( "net/http" "regexp" "strconv" - "time" "strings" + "time" "fagott.pw/charakterin" + "fagott.pw/grilist/cache" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" - "fagott.pw/grilist/cache" "github.com/julienschmidt/httprouter" ) type CachedGril struct { - Created time.Time - Gril *Gril + Created time.Time + Gril *Gril } var ( @@ -38,7 +38,7 @@ func findIdx() { type GrilsModule struct { g *grilist.Grilist - c *cache.Cache + c *cache.Cache } func (m *GrilsModule) Name() string { @@ -50,8 +50,8 @@ func (m *GrilsModule) Init(g *grilist.Grilist) { m.g = g m.g.Router.GET("/gril/:id", m.viewGril) m.g.Router.GET("/gril/:id/*rest", m.viewGril) - - m.c = cache.New() + + m.c = cache.New() } func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { @@ -152,77 +152,77 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das } func (m *GrilsModule) FromID(id int) (*Gril, error) { - if g, ok := m.c.Get(id); ok { - return g.(*Gril), nil - } - + if g, ok := m.c.Get(id); ok { + return g.(*Gril), nil + } + gril := &Gril{ ID: id, } - t1 := time.Now() + t1 := time.Now() err := m.g.DB.QueryRow(`SELECT updated_at, age, birthday FROM grilist.grils WHERE id = $1`, id).Scan(&gril.UpdatedAt, &gril.Age, &gril.Birthday) log.Printf("get_gril_from_id_raw: %dms", time.Since(t1).Nanoseconds()/1000000) if err != nil { return nil, err } - - // Namen rausholen - rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id) + + // Namen rausholen + rows, err := m.g.DB.Query(`SELECT name, name_type FROM grilist.gril_names WHERE gril_id = $1`, id) if err != nil { return nil, err } log.Printf("get_gril_from_id_names: %dms", time.Since(t1).Nanoseconds()/1000000) - defer rows.Close() - - for rows.Next() { - var name string - var name_type int - if err := rows.Scan(&name, &name_type); err != nil { - return nil, err - } - - switch name_type { - case 0: - gril.KanjiName = name - break - case 1: - gril.RomajiName = name - default: - gril.OtherNames = append(gril.OtherNames, name) - } - } - /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id) - if err != nil { - return nil, err - } - defer rows.Close() - - for rows.Next() { - var name string - if err := rows.Scan(&name); err != nil { - return nil, err - } - gril.OtherNames = append(gril.OtherNames, name) - } - rows.Close() - - // Tags holen - 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) - if err != nil { - return nil, err + defer rows.Close() + + for rows.Next() { + var name string + var name_type int + if err := rows.Scan(&name, &name_type); err != nil { + return nil, err + } + + switch name_type { + case 0: + gril.KanjiName = name + break + case 1: + gril.RomajiName = name + default: + gril.OtherNames = append(gril.OtherNames, name) + } } - defer rows.Close() - - for rows.Next() { - var name string - if err := rows.Scan(&name); err != nil { - return nil, err - } - gril.Tags = append(gril.Tags, name) - }*/ - - m.c.Insert(id, gril) + /*rows, err := m.g.DB.Query(`SELECT name FROM grilist.gril_names WHERE gril_id = $1 AND name_type > 1`, id) + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + return nil, err + } + gril.OtherNames = append(gril.OtherNames, name) + } + rows.Close() + + // Tags holen + 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) + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + return nil, err + } + gril.Tags = append(gril.Tags, name) + }*/ + + m.c.Insert(id, gril) log.Printf("get_gril_from_id: %dms", time.Since(t1).Nanoseconds()/1000000) return gril, nil } 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 ( "database/sql" "encoding/json" "errors" - "fagott.pw/charakterin" - "fagott.pw/grilist/cache" - "fagott.pw/grilist/frontend" - "fagott.pw/grilist/grilist" - "fagott.pw/grilist/modules/grils" "fmt" - "github.com/julienschmidt/httprouter" - "github.com/lib/pq" "io/ioutil" "log" "net/http" "net/url" "sort" "strconv" + + "fagott.pw/charakterin" + "fagott.pw/grilist/cache" + "fagott.pw/grilist/frontend" + "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/grils" + "github.com/julienschmidt/httprouter" + "github.com/lib/pq" ) // 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 @@ {{ define "card_gril" }}
-
- -
-
- {{ .RomajiName }} {{ .KanjiName }} -
- +
+ +
+
+ {{ .RomajiName }} {{ .KanjiName }} +
+
-{{ end }} \ No newline at end of file +{{ 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 @@ {{ define "navbar" }} - -{{ end }} \ No newline at end of file + +{{ end }} -- cgit v0.10.1 From 46185bf12dd907e536aa216a03c818f40f88cae8 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Mon, 3 Oct 2016 15:44:55 +0200 Subject: =?UTF-8?q?Neue=20Datenbankfunktion=20f=C3=BCr=20das=20holen=20der?= =?UTF-8?q?=20letzten=20Grils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/modules/grils/grils.go b/modules/grils/grils.go index 3ff03ec..135398d 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -105,43 +105,35 @@ func (m *GrilsModule) GetListsOfGril(gril *Gril) error { func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { var categories []grilist.DashboardCategory - t1 := time.Now() - rows, err := m.g.DB.Query(`SELECT id FROM grilist.grils ORDER BY updated_at DESC LIMIT 5`) + + rows, err := m.g.DB.Query( + `SELECT id, romaji_name, kanji_name FROM grilist.get_recently_updated_grils();`) if err != nil { log.Println(err) return categories } defer rows.Close() - log.Printf("get_newest_grils: %dms", time.Since(t1).Nanoseconds()/1000000) cat := grilist.DashboardCategory{ Title: "Neue Grils", } for rows.Next() { - var id int - if err := rows.Scan(&id); err != nil { - log.Println(err) - continue - } - - t2 := time.Now() - gril, err := m.FromID(id) - if err != nil { + var g Gril + if err := rows.Scan(&g.ID, &g.RomajiName, &g.KanjiName); err != nil { log.Println(err) continue } - log.Printf("get_gril_from_id_in_newest_grils: %dms", time.Since(t2).Nanoseconds()/1000000) cat.Cards = append(cat.Cards, frontend.Card{ - Title: gril.RomajiName, - Description: gril.KanjiName, + Title: g.RomajiName, + Description: g.KanjiName, Size: "medium", Actions: []frontend.Action{ frontend.Action{ Name: "anguckieren", - Link: "/gril/" + gril.Slug(), + Link: "/gril/" + g.Slug(), }, }, }) -- cgit v0.10.1