diff options
-rw-r--r-- | modules/grils/gril.go | 7 | ||||
-rw-r--r-- | modules/grils/grils.go | 92 | ||||
-rw-r--r-- | modules/lists/lists.go | 6 | ||||
-rw-r--r-- | views/gril.html | 5 |
4 files changed, 89 insertions, 21 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 @@ | |||
1 | package grils | 1 | package grils |
2 | 2 | ||
3 | import ( | 3 | import ( |
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 | ||
16 | var ( | ||
17 | pgArrayReg = regexp.MustCompile(`(((?P<value>(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) | ||
18 | pgValueIdx int | ||
19 | ) | ||
20 | |||
21 | func findIdx() { | ||
22 | for i, subexp := range pgArrayReg.SubexpNames() { | ||
23 | if subexp == "value" { | ||
24 | pgValueIdx = i | ||
25 | break | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | |||
14 | type GrilsModule struct { | 30 | type 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 | ||
22 | func (m *GrilsModule) Init(g *grilist.Grilist) { | 38 | func (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 | ||
48 | func (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 | |||
31 | func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | 77 | func (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 | ||
63 | func (m *GrilsModule) FromID(id int) (*Gril, error) { | 108 | func (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 | ||
75 | func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | 124 | func (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 | ||
147 | func 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 | |||
98 | func New() *GrilsModule { | 158 | func New() *GrilsModule { |
99 | return &GrilsModule{} | 159 | return &GrilsModule{} |
100 | } | 160 | } |
diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 970b15b..a7e822a 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go | |||
@@ -9,10 +9,10 @@ import ( | |||
9 | "fagott.pw/grilist/modules/grils" | 9 | "fagott.pw/grilist/modules/grils" |
10 | "fmt" | 10 | "fmt" |
11 | "github.com/julienschmidt/httprouter" | 11 | "github.com/julienschmidt/httprouter" |
12 | "github.com/lib/pq" | ||
12 | "log" | 13 | "log" |
13 | "net/http" | 14 | "net/http" |
14 | "strconv" | 15 | "strconv" |
15 | "time" | ||
16 | ) | 16 | ) |
17 | 17 | ||
18 | // Module und so. | 18 | // Module und so. |
@@ -27,8 +27,8 @@ type List struct { | |||
27 | Name string | 27 | Name string |
28 | Description string | 28 | Description string |
29 | Owner *charakterin.User | 29 | Owner *charakterin.User |
30 | ForkOf *int | 30 | ForkOf sql.NullInt64 |
31 | UpdatedAt *time.Time | 31 | UpdatedAt pq.NullTime |
32 | Grils []*ListGril | 32 | Grils []*ListGril |
33 | } | 33 | } |
34 | 34 | ||
diff --git a/views/gril.html b/views/gril.html index d623abf..3983061 100644 --- a/views/gril.html +++ b/views/gril.html | |||
@@ -11,6 +11,11 @@ | |||
11 | {{ template "navbar" . }} | 11 | {{ template "navbar" . }} |
12 | <div class="container"> | 12 | <div class="container"> |
13 | <h1>{{ $gril.RomajiName }}<small>{{ $gril.KanjiName }}</small></h1> | 13 | <h1>{{ $gril.RomajiName }}<small>{{ $gril.KanjiName }}</small></h1> |
14 | {{ range $tag := $gril.Tags }} | ||
15 | <div class="chip"> | ||
16 | {{ $tag }} | ||
17 | </div> | ||
18 | {{ end }} | ||
14 | </div> | 19 | </div> |
15 | </body> | 20 | </body> |
16 | </html> | 21 | </html> |