Offset/limit
This commit is contained in:
parent
444c10cdc8
commit
2115122d59
@ -19,6 +19,8 @@ const (
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user