From 368c7f87bff1233ad437437804a91bd03833238a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 1 Oct 2018 15:02:13 +0200 Subject: [PATCH] pagination work --- auth.go | 4 ++-- payload.go | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/auth.go b/auth.go index 3783a2e..64ca043 100644 --- a/auth.go +++ b/auth.go @@ -188,9 +188,9 @@ func GetTokenClaims(req *http.Request) (*TokenClaims, error) { } // randomSalt returns a string of 32 random characters. -const saltSize = 32 - func randomSalt() (s string, err error) { + const saltSize = 32 + rawsalt := make([]byte, saltSize) _, err = rand.Read(rawsalt) diff --git a/payload.go b/payload.go index f9c3e40..541b236 100644 --- a/payload.go +++ b/payload.go @@ -47,6 +47,7 @@ type Translation struct { FieldsLabels map[string]string `json:"fieldsLabels"` } +// output type PaginationLinks struct { Base string `json:"base"` Next string `json:"next"` @@ -54,13 +55,25 @@ type PaginationLinks struct { Self string `json:"self"` } +// input type PaginationParameters struct { + URL string `json:"-"` Offset int64 `json:"offset"` Limit int64 `json:"limit"` SortBy string `json:"sortBy"` Order string `json:"order"` } +// TODO(marko) +func GetPaginationParameters(req *http.Request) (p PaginationParameters) { + return p +} + +// TODO(marko) +func (p *PaginationParameters) paginationLinks() (links PaginationLinks) { + return links +} + type Payload struct { Method string `json:"method"` Params map[string]string `json:"params"` @@ -70,9 +83,9 @@ type Payload struct { IdField string `json:"idField"` // Pagination - Count int64 `json:"count"` - Total int64 `json:"total"` - Links *PaginationLinks `json:"_links"` + 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"` @@ -82,17 +95,17 @@ func (p *Payload) SetData(data interface{}) { p.Data = data } -func (p *Payload) SetPaginationInfo(reqUrl string, count, total int64, params PaginationParameters) { +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 etype. -func NewPayload(r *http.Request, etype string) Payload { - pload := metadata[etype] - pload.Method = r.Method + " " + r.RequestURI - return pload +// 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 } // DecodeJSON decodes JSON data from r to v. -- 1.8.1.2