From 365bd4578a0c9b2e4ae5938d280609bd9af37faf Mon Sep 17 00:00:00 2001 From: jan Date: Tue, 29 Dec 2015 22:44:46 +0100 Subject: =?UTF-8?q?simple=20pagination=20f=C3=BCr=20tag-ansicht.=20au?= =?UTF-8?q?=C3=9Ferdem=20wird=20nun=20ein=20leeres=20array=20bei=20grils.F?= =?UTF-8?q?romIDs=20zur=C3=BCckgegeben=20anstatt=20lustige=20sql=20fehler?= =?UTF-8?q?=20zu=20provozieren,=20wenn=20der=20ids=20array=20leer=20ist.?= 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 0df93a0..5d548e6 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -153,6 +153,10 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { } func (m *GrilsModule) FromIDs(ids []int) ([]*Gril, error) { + if len(ids) == 0 { + return make([]*Gril, 0), nil + } + idList := "(" first := true for _, v := range ids { diff --git a/modules/tags/tags.go b/modules/tags/tags.go index 9412e24..aca1b44 100644 --- a/modules/tags/tags.go +++ b/modules/tags/tags.go @@ -3,6 +3,7 @@ package tags import ( "log" "net/http" + "strconv" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" @@ -33,6 +34,7 @@ func (m *Module) Init(g *grilist.Grilist) { } m.grils = gm.(*grils.GrilsModule) m.g.Router.GET("/tag/:tag", m.viewTag) + m.g.Router.GET("/tag/:tag/:page", m.viewTagPage) } func (m *Module) Interface() interface{} { @@ -44,21 +46,35 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar } func (m *Module) viewTag(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + p = append(p, httprouter.Param{ + Key: "page", + Value: "1", + }) + m.viewTagPage(w, r, p) +} + +func (m *Module) viewTagPage(w http.ResponseWriter, r *http.Request, p httprouter.Params) { user, _ := m.g.Charakterin.GetUserFromRequest(r) tag := p.ByName("tag") var tagID int row := m.g.DB.QueryRow("SELECT id FROM grilist.tags WHERE name = $1;", tag) if err := row.Scan(&tagID); err != nil { - log.Println(err) + log.Println("error getting tag id", err) http.Redirect(w, r, "/", 302) return } + + pageNumber, err := strconv.Atoi(p.ByName("page")) + if err != nil || pageNumber < 1 { + pageNumber = 1 + } + idList := make([]int, 0) rows, err := m.g.DB.Query( - "SELECT gril_id FROM grilist.grils_tags WHERE tag_id = $1", - tagID) + "SELECT gril_id FROM grilist.grils_tags WHERE tag_id = $1 LIMIT 12 OFFSET $2", + tagID, (pageNumber-1)*12) if err != nil { - log.Println(err) + log.Println("error getting idList", err) http.Redirect(w, r, "/", 302) return } -- cgit v0.10.1