From 8e5c7e54f952e5c34ab64c512ccdbc27af9447c9 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 27 Dec 2015 14:03:49 +0100 Subject: =?UTF-8?q?tats=C3=A4chliche=20daten=20aus=20der=20datenbank=20wer?= =?UTF-8?q?den=20nun=20f=C3=BCr=20grils=20verwendet.=20ein=20hoch=20auf=20?= =?UTF-8?q?unsern=20busfahrer,=20busfahrer,=20busfahrer.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 @@ package grils import ( + "database/sql" + "github.com/lib/pq" "time" ) @@ -16,8 +18,9 @@ type Gril struct { KanjiName string RomajiName string OtherNames []string - Age int - Birthday time.Time + Age sql.NullInt64 + Birthday pq.NullTime Tags []string ForeignIDs map[DataSource]int + UpdatedAt time.Time } 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 ( "fagott.pw/grilist/grilist" "fmt" "github.com/julienschmidt/httprouter" + "log" "net/http" + "regexp" "strconv" - "time" + "strings" ) +var ( + pgArrayReg = regexp.MustCompile(`(((?P(([^",\\{}\s(NULL)])+|"([^"\\]|\\"|\\\\)*")))(,)?)`) + pgValueIdx int +) + +func findIdx() { + for i, subexp := range pgArrayReg.SubexpNames() { + if subexp == "value" { + pgValueIdx = i + break + } + } +} + type GrilsModule struct { g *grilist.Grilist } @@ -20,6 +36,7 @@ func (m *GrilsModule) Name() string { } func (m *GrilsModule) Init(g *grilist.Grilist) { + findIdx() m.g = g m.g.Router.GET("/gril/:id", m.viewGril) } @@ -28,15 +45,43 @@ func (m *GrilsModule) Interface() interface{} { return m } +func (m *GrilsModule) getGrils(whereClause string, params ...interface{}) ([]*Gril, error) { + var grils []*Gril + + 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...) + if err != nil { + return nil, err + } + + defer rows.Close() + for rows.Next() { + gril := &Gril{} + var tags []byte + var otherNames []byte + err = rows.Scan(&gril.ID, &gril.KanjiName, &gril.RomajiName, &otherNames, &gril.UpdatedAt, &gril.Age, &gril.Birthday, &tags) + if err != nil { + log.Println(gril.ID) + log.Println("error scanning in getGrils:", err) + continue + } + + gril.Tags = pgArray(tags) + gril.OtherNames = pgArray(otherNames) + + grils = append(grils, gril) + } + + return grils, nil +} + func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { var categories []grilist.DashboardCategory - var grils []*Gril - - // TODO - // NUR TEMPORÄR - g, _ := m.FromID(1) - grils = append(grils, g) + grils, err := m.getGrils(`1 = 1 ORDER BY updated_at DESC LIMIT 5`) + if err != nil { + log.Println(err) + return categories + } cat := grilist.DashboardCategory{ Title: "Neue Grils", @@ -61,15 +106,19 @@ func (m *GrilsModule) ProvideDashboardData(user *charakterin.User) []grilist.Das } func (m *GrilsModule) FromID(id int) (*Gril, error) { - return &Gril{ - ID: id, - KanjiName: "藤林 杏", - RomajiName: "Kyou Fujibayashi", - OtherNames: []string{"ふじばやし きょう"}, - Age: 17, - Birthday: time.Now(), - Tags: []string{"tsundere", "hair intakes", "hair ribbon", "school uniform", "school crest"}, - }, nil + gril := &Gril{ + ID: id, + } + var tags []byte + var otherNames []byte + 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) + + gril.Tags = pgArray(tags) + gril.OtherNames = pgArray(otherNames) + if err != nil { + return nil, err + } + return gril, nil } 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 m.g.Renderer.RenderPage("gril", w, data) } +func pgArray(array []byte) []string { + var results []string + matches := pgArrayReg.FindAllStringSubmatch(string(array), -1) + for _, match := range matches { + s := match[pgValueIdx] + s = strings.Trim(s, "\"") + results = append(results, s) + } + return results +} + func New() *GrilsModule { return &GrilsModule{} } 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 ( "fagott.pw/grilist/modules/grils" "fmt" "github.com/julienschmidt/httprouter" + "github.com/lib/pq" "log" "net/http" "strconv" - "time" ) // Module und so. @@ -27,8 +27,8 @@ type List struct { Name string Description string Owner *charakterin.User - ForkOf *int - UpdatedAt *time.Time + ForkOf sql.NullInt64 + UpdatedAt pq.NullTime Grils []*ListGril } 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 @@ {{ template "navbar" . }}

{{ $gril.RomajiName }}{{ $gril.KanjiName }}

+ {{ range $tag := $gril.Tags }} +
+ {{ $tag }} +
+ {{ end }}
-- cgit v0.10.1