diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-03-16 17:20:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-16 10:20:13 +0100 |
commit | e0ea3811c4178ffa30452b7ca4bd211e59326f91 (patch) | |
tree | bc9b585c2e5f7b81b285dcf6e48c2905409e137a /services | |
parent | 6ead30dbc469803d7e8361b13217e07d87d6f80d (diff) | |
download | gitea-e0ea3811c4178ffa30452b7ca4bd211e59326f91.tar.gz gitea-e0ea3811c4178ffa30452b7ca4bd211e59326f91.zip |
Refactor AddParam to AddParamIfExist (#29834)
When read the code: `pager.AddParam(ctx, "search", "search")`, the
question always comes: What is it doing? Where is the value from? Why
"search" / "search" ?
Now it is clear: `pager.AddParamIfExist("search", ctx.Data["search"])`
Diffstat (limited to 'services')
-rw-r--r-- | services/context/pagination.go | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/services/context/pagination.go b/services/context/pagination.go index 655a278f9f..11d37283c9 100644 --- a/services/context/pagination.go +++ b/services/context/pagination.go @@ -26,14 +26,13 @@ func NewPagination(total, pagingNum, current, numPages int) *Pagination { return p } -// AddParam adds a value from context identified by ctxKey as link param under a given paramKey -func (p *Pagination) AddParam(ctx *Context, paramKey, ctxKey string) { - _, exists := ctx.Data[ctxKey] - if !exists { +// AddParamIfExist adds a value to the query parameters if the value is not nil +func (p *Pagination) AddParamIfExist(key string, val any) { + if val == nil { return } - paramData := fmt.Sprintf("%v", ctx.Data[ctxKey]) // cast any to string - urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(paramKey), url.QueryEscape(paramData)) + paramData := fmt.Sprint(val) + urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(paramData)) p.urlParams = append(p.urlParams, urlParam) } @@ -50,8 +49,8 @@ func (p *Pagination) GetParams() template.URL { // SetDefaultParams sets common pagination params that are often used func (p *Pagination) SetDefaultParams(ctx *Context) { - p.AddParam(ctx, "sort", "SortType") - p.AddParam(ctx, "q", "Keyword") + p.AddParamIfExist("sort", ctx.Data["SortType"]) + p.AddParamIfExist("q", ctx.Data["Keyword"]) // do not add any more uncommon params here! - p.AddParam(ctx, "fuzzy", "IsFuzzy") + p.AddParamIfExist("fuzzy", ctx.Data["IsFuzzy"]) } |