aboutsummaryrefslogtreecommitdiff
path: root/modules/grils
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2015-12-27 13:03:49 (UTC)
committerjan <jan@ruken.pw>2015-12-27 13:03:49 (UTC)
commit8e5c7e54f952e5c34ab64c512ccdbc27af9447c9 (patch)
tree14d546d8d6d252f85536a7055210c263e397b3a0 /modules/grils
parent6fabf512767b458a44b4b2547a7b07e9db71c00a (diff)
tatsächliche daten aus der datenbank werden nun für grils verwendet. ein hoch auf unsern busfahrer, busfahrer, busfahrer.
Diffstat (limited to 'modules/grils')
-rw-r--r--modules/grils/gril.go7
-rw-r--r--modules/grils/grils.go92
2 files changed, 81 insertions, 18 deletions
diff --git a/modules/grils/gril.go b/modules/grils/gril.go
index 645a97a..9fd3be5 100644
--- a/modules/grils/gril.go
+++ b/modules/grils/gril.go
@@ -1,6 +1,8 @@
1package grils 1package grils
2 2
3import ( 3import (
4 "database/sql"
5 "github.com/lib/pq"
4 "time" 6 "time"
5) 7)
6 8
@@ -16,8 +18,9 @@ type Gril struct {
16 KanjiName string 18 KanjiName string
17 RomajiName string 19 RomajiName string
18 OtherNames []string 20 OtherNames []string
19 Age int 21 Age sql.NullInt64
20 Birthday time.Time 22 Birthday pq.NullTime
21 Tags []string 23 Tags []string
22 ForeignIDs map[DataSource]int 24 ForeignIDs map[DataSource]int
25 UpdatedAt time.Time
23} 26}
diff --git a/modules/grils/grils.go b/modules/grils/grils.go
index 3663c7f..e2eb543 100644
--- a/modules/grils/grils.go
+++ b/modules/grils/grils.go
@@ -6,11 +6,27 @@ import (
6 "fagott.pw/grilist/grilist" 6 "fagott.pw/grilist/grilist"
7 "fmt" 7 "fmt"
8 "github.com/julienschmidt/httprouter" 8 "github.com/julienschmidt/httprouter"
9 "log"
9 "net/http" 10 "net/http"
11 "regexp"
10 "strconv" 12 "strconv"
11 "time" 13 "strings"
12) 14)
13 15
16var (
17 pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`)
18 pgValueIdx int
19)
20
21func findIdx() {
22 for i, subexp := range pgArrayReg.SubexpNames() {
23 if subexp == "value" {
24 pgValueIdx = i
25 break
26 }
27 }
28}
29
14type GrilsModule struct { 30type GrilsModule struct {
15 g *grilist.Grilist 31 g *grilist.Grilist
16} 32}
@@ -20,6 +36,7 @@ func (m *GrilsModule) Name() string {
20} 36}
21 37
22func (m *GrilsModule) Init(g *grilist.Grilist) { 38func (m *GrilsModule) Init(g *grilist.Grilist) {
39 findIdx()
23 m.g = g 40 m.g = g
24 m.g.Router.GET("/gril/:id", m.viewGril) 41 m.g.Router.GET("/gril/:id", m.viewGril)
25} 42}
@@ -28,15 +45,43 @@ func (m *GrilsModule) Interface() interface{} {
28 return m 45 return m
29} 46}
30 47
48func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) {
49 var grils []*Gril
50
51 rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, kanji_name, romaji_name, other_names, updated_at, age, birthday, tags FROM grilist.grils_flattened WHERE %s`, whereClause), params...)
52 if err != nil {
53 return nil, err
54 }
55
56 defer rows.Close()
57 for rows.Next() {
58 gril := &Gril{}
59 var tags []byte
60 var otherNames []byte
61 err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
62 if err != nil {
63 log.Println(gril.ID)
64 log.Println("error scanning in getGrils:", err)
65 continue
66 }
67
68 gril.Tags = pgArray(tags)
69 gril.OtherNames = pgArray(otherNames)
70
71 grils = append(grils, gril)
72 }
73
74 return grils, nil
75}
76
31func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { 77func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
32 var categories []grilist.DashboardCategory 78 var categories []grilist.DashboardCategory
33 79
34 var grils []*Gril 80 grils, err := m.getGrils(`1 = 1 ORDER BY updated_at DESC LIMIT 5`)
35 81 if err != nil {
36 // TODO 82 log.Println(err)
37 // NUR TEMPORÄR 83 return categories
38 g, _ := m.FromID(1) 84 }
39 grils = append(grils, g)
40 85
41 cat := grilist.DashboardCategory{ 86 cat := grilist.DashboardCategory{
42 Title: "Neue Grils", 87 Title: "Neue Grils",
@@ -61,15 +106,19 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das
61} 106}
62 107
63func (m *GrilsModule) FromID(id int) (*Gril, error) { 108func (m *GrilsModule) FromID(id int) (*Gril, error) {
64 return &Gril{ 109 gril := &Gril{
65 ID: id, 110 ID: id,
66 KanjiName: "藤林 杏", 111 }
67 RomajiName: "Kyou Fujibayashi", 112 var tags []byte
68 OtherNames: []string{"ふじばやし きょう"}, 113 var otherNames []byte
69 Age: 17, 114 err := m.g.DB.QueryRow(`SELECT kanji_name, romaji_name, other_names, updated_at, age, birthday, tags FROM grilist.grils_flattened WHERE id = $1`, id).Scan(&gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags)
70 Birthday: time.Now(), 115
71 Tags: []string{"tsundere", "hair intakes", "hair ribbon", "school uniform", "school crest"}, 116 gril.Tags = pgArray(tags)
72 }, nil 117 gril.OtherNames = pgArray(otherNames)
118 if err != nil {
119 return nil, err
120 }
121 return gril, nil
73} 122}
74 123
75func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { 124func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@@ -95,6 +144,17 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou
95 m.g.Renderer.RenderPage("gril", w, data) 144 m.g.Renderer.RenderPage("gril", w, data)
96} 145}
97 146
147func pgArray(array []byte) []string {
148 var results []string
149 matches := pgArrayReg.FindAllStringSubmatch(string(array), -1)
150 for _, match := range matches {
151 s := match[pgValueIdx]
152 s = strings.Trim(s, "\"")
153 results = append(results, s)
154 }
155 return results
156}
157
98func New() *GrilsModule { 158func New() *GrilsModule {
99 return &GrilsModule{} 159 return &GrilsModule{}
100} 160}