From 2115122d59320f9652e0cb3879cf68d6df92805c Mon Sep 17 00:00:00 2001 From: Meutel Date: Wed, 2 Aug 2017 20:17:12 +0200 Subject: [PATCH] Offset/limit --- bouquins/bouquins.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index 44d8d96..81a874c 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -16,9 +16,11 @@ const ( TPL_SERIES = "series.html" TPL_INDEX = "index.html" - PARAM_LIST = "list" - PARAM_ORDER = "order" - PARAM_SORT = "sort" + PARAM_LIST = "list" + PARAM_ORDER = "order" + PARAM_SORT = "sort" + PARAM_LIMIT = "limit" + PARAM_OFFSET = "offset" LIST_AUTHORS = "authors" LIST_SERIES = "series" @@ -162,6 +164,23 @@ func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface } } +func paramInt(name string, req *http.Request) int { + val := req.URL.Query().Get(name) + valInt, err := strconv.Atoi(val) + if err != nil { + log.Print("Invalid value for", name, ":", val) + return 0 + } + return valInt +} +func paramOrder(req *http.Request) string { + val := req.URL.Query().Get(PARAM_ORDER) + if val == "desc" || val == "asc" { + return val + } + return "" +} + // ROUTES // func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { @@ -170,16 +189,17 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { log.Print(err) } model := NewIndexModel("", count) - order, sort := req.URL.Query().Get(PARAM_ORDER), req.URL.Query().Get(PARAM_SORT) + order, sort := paramOrder(req), req.URL.Query().Get(PARAM_SORT) + limit, offset := paramInt(PARAM_LIMIT, req), paramInt(PARAM_OFFSET, req) switch req.URL.Query().Get(PARAM_LIST) { case LIST_AUTHORS: - model.Authors, err = app.AuthorsAdv(0, 0, sort, order) + model.Authors, err = app.AuthorsAdv(limit, offset, sort, order) case LIST_SERIES: - model.Series, err = app.SeriesAdv(0, 0, sort, order) + model.Series, err = app.SeriesAdv(limit, offset, sort, order) case LIST_BOOKS: fallthrough default: - model.Books, err = app.BooksAdv(0, 0, sort, order) + model.Books, err = app.BooksAdv(limit, offset, sort, order) } if err != nil { log.Print(err)