Skip to content

Commit

Permalink
Remove obsolete pagination logic and replace with paginator lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Dec 11, 2021
1 parent c74983e commit e0516cb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 109 deletions.
123 changes: 20 additions & 103 deletions cmd/dictmaker/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import (
"github.com/knadh/paginator"
)

const (
entriesPerPage = 20
// glossaryPerPage = 100
)

// Results represents a set of results.
type Results struct {
FromLang string `json:"-"`
Expand All @@ -41,19 +36,6 @@ type Glossary struct {
paginator.Set
}

// pagination represents a query's pagination (limit, offset) related values.
type pagination struct {
PerPage int `json:"per_page"`
Page int `json:"page"`
Offset int `json:"offset"`
Limit int `json:"limit"`
Total int `json:"total"`

PageStart int `json:"-"`
Pages []int `json:"-"`
PageEnd int `json:"-"`
}

// okResp represents the HTTP response wrapper.
type okResp struct {
Data interface{} `json:"data"`
Expand Down Expand Up @@ -141,7 +123,7 @@ func doSearch(r *http.Request, app *App) (data.Query, *Results, error) {
q = strings.TrimSpace(chi.URLParam(r, "q"))

qp = r.URL.Query()
pg = getPagination(qp, entriesPerPage, entriesPerPage)
pg = app.resultsPg.NewFromURL(r.URL.Query())
out = &Results{}
)

Expand Down Expand Up @@ -184,13 +166,10 @@ func doSearch(r *http.Request, app *App) (data.Query, *Results, error) {
return query, out, err
}

// HTTP response.
out = &Results{}
out.Page = pg.Page
out.PerPage = pg.PerPage
out.Entries = data.Entries{}

// Find entries matching the query.
// Search and compose results.
out = &Results{
Entries: data.Entries{},
}
res, total, err := app.data.Search(query)
if err != nil {
if err == sql.ErrNoRows {
Expand Down Expand Up @@ -223,20 +202,24 @@ func doSearch(r *http.Request, app *App) (data.Query, *Results, error) {
}
}

out.Total = total
out.Entries = res

pg.SetTotal(total)
out.Page = pg.Page
out.PerPage = pg.PerPage
out.TotalPages = pg.TotalPages
out.Total = total

return query, out, nil
}

// getGlossaryWords is a helper function that takes an HTTP query context,
// gets params from it and returns a glossary of words for a language.
func getGlossaryWords(lang, initial string, pg paginator.Set, app *App) (*Glossary, error) {
// HTTP response.
out := &Glossary{}
out.Page = pg.Page
out.PerPage = pg.PerPage
out.Words = []data.GlossaryWord{}
out := &Glossary{
Words: []data.GlossaryWord{},
}

// Get glossary words.
res, total, err := app.data.GetGlossaryWords(lang, initial, pg.Offset, pg.Limit)
Expand All @@ -254,81 +237,15 @@ func getGlossaryWords(lang, initial string, pg paginator.Set, app *App) (*Glossa
return out, nil
}

out.Total = total
out.Words = res

return out, nil
}

// getPagination takes form values and extracts pagination values from it.
func getPagination(q url.Values, defaultPerPage, maxPerPage int) pagination {
var (
perPage, _ = strconv.Atoi(q.Get("per_page"))
page, _ = strconv.Atoi(q.Get("page"))
)

if perPage < 1 || perPage > defaultPerPage {
perPage = maxPerPage
}

if page < 1 {
page = 0
} else {
page--
}

return pagination{
Page: page + 1,
PerPage: perPage,
Offset: page * perPage,
Limit: perPage,
}
}

func (p *pagination) GenerateNumbers() {
if p.Total <= p.PerPage {
return
}

var (
// Page divisor.
div = p.Total / p.PerPage
divStart = 1
hints = 0
)

if p.Total%p.PerPage == 0 {
div = div - 1
}

div++

if div > 10 {
hints = div
div = 10
}

// Generate the page numbers
if p.Page >= 10 {
divStart = p.PerPage - 5
div = divStart + 15
}

if (div * p.PerPage) > (p.Total + p.PerPage) {
div = (p.Total) / p.PerPage
}

// If the page number has exceeded the limit, fix the first to
// print to 1.
if p.Page >= 10 {
p.PageStart = 1
} else {
p.PageStart = 1
}
pg.SetTotal(total)
out.Page = pg.Page
out.PerPage = pg.PerPage
out.TotalPages = pg.TotalPages
out.Total = total

if hints-10 > p.Page {
p.PageEnd = hints
}
return out, nil
}

// validateSearchQuery does basic validation and sanity checks
Expand Down
12 changes: 6 additions & 6 deletions cmd/dictmaker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ func main() {

// Pagination.
o := paginator.Default()
o.DefaultPerPage = ko.Int("results.default_per_page")
o.MaxPerPage = ko.Int("results.max_per_page")
o.NumPageNums = ko.Int("results.num_page_nums")
o.DefaultPerPage = ko.MustInt("results.default_per_page")
o.MaxPerPage = ko.MustInt("results.max_per_page")
o.NumPageNums = ko.MustInt("results.num_page_nums")
app.resultsPg = paginator.New(o)

o.DefaultPerPage = ko.Int("glossary.default_per_page")
o.MaxPerPage = ko.Int("glossary.max_per_page")
o.NumPageNums = ko.Int("glossary.num_page_nums")
o.DefaultPerPage = ko.MustInt("glossary.default_per_page")
o.MaxPerPage = ko.MustInt("glossary.max_per_page")
o.NumPageNums = ko.MustInt("glossary.num_page_nums")
app.glossaryPg = paginator.New(o)

// Load admin theme.
Expand Down

0 comments on commit e0516cb

Please sign in to comment.