From 4635f2c3cd374ddaf963f577535485bcddcf1bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Wed, 27 Nov 2019 11:07:48 +0100 Subject: [PATCH] pagination utility --- pagination.go | 34 ++++++++++++++++++++++++++++++++++ payload.go | 53 +++++++++++------------------------------------------ 2 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 pagination.go diff --git a/pagination.go b/pagination.go new file mode 100644 index 0000000..4a1ecae --- /dev/null +++ b/pagination.go @@ -0,0 +1,34 @@ +package webutility + +import "net/http" + +type PaginationParams struct { + URL string + Offset int64 + Limit int64 + SortBy string + Order string +} + +func (p *PaginationParams) links() PaginationLinks { + return PaginationLinks{} +} + +// PaginationLinks ... +type PaginationLinks struct { + Count int64 + Total int64 + Base string `json:"base"` + Next string `json:"next"` + Prev string `json:"prev"` + Self string `json:"self"` +} + +func GetPaginationParameters(req *http.Request) (p PaginationParams) { + p.Offset = StringToInt64(req.FormValue("offset")) + p.Limit = StringToInt64(req.FormValue("limit")) + p.SortBy = req.FormValue("sortBy") + p.Order = req.FormValue("order") + + return p +} diff --git a/payload.go b/payload.go index cefe9bf..784d900 100644 --- a/payload.go +++ b/payload.go @@ -48,34 +48,6 @@ type Translation struct { FieldsLabels map[string]string `json:"fieldsLabels"` } -// PaginationLinks ... -type PaginationLinks struct { - Base string `json:"base"` - Next string `json:"next"` - Prev string `json:"prev"` - Self string `json:"self"` -} - -// PaginationParameters ... -type PaginationParameters struct { - URL string `json:"-"` - Offset int64 `json:"offset"` - Limit int64 `json:"limit"` - SortBy string `json:"sortBy"` - Order string `json:"order"` -} - -// GetPaginationParameters ... -// TODO(marko) -func GetPaginationParameters(req *http.Request) (p PaginationParameters) { - return p -} - -// TODO(marko) -func (p *PaginationParameters) paginationLinks() (links PaginationLinks) { - return links -} - // Payload ... type Payload struct { Method string `json:"method"` @@ -85,15 +57,19 @@ type Payload struct { Correlations []CorrelationField `json:"correlationFields"` IDField string `json:"idField"` - // Pagination - Count int64 `json:"count"` - Total int64 `json:"total"` Links PaginationLinks `json:"_links"` // Data holds JSON payload. It can't be used for itteration. Data interface{} `json:"data"` } +// NewPayload returs a payload sceleton for entity described with key. +func NewPayload(r *http.Request, key string) Payload { + p := metadata[key] + p.Method = r.Method + " " + r.RequestURI + return p +} + func (p *Payload) addLang(code string, labels map[string]string) { t := Translation{ Language: code, @@ -108,17 +84,10 @@ func (p *Payload) SetData(data interface{}) { } // SetPaginationInfo ... -func (p *Payload) SetPaginationInfo(count, total int64, params PaginationParameters) { - p.Count = count - p.Total = total - p.Links = params.paginationLinks() -} - -// NewPayload returs a payload sceleton for entity described with key. -func NewPayload(r *http.Request, key string) Payload { - p := metadata[key] - p.Method = r.Method + " " + r.RequestURI - return p +func (p *Payload) SetPaginationInfo(count, total int64, params PaginationParams) { + p.Links.Count = count + p.Links.Total = total + p.Links = params.links() } // InitPayloadsMetadata loads all payloads' information into 'metadata' variable. -- 1.8.1.2