helpers

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLimitOffset

func GetLimitOffset(query url.Values, defaultLimit ...int) (page, limit, offset int64)

GetLimitOffset calculates pagination parameters based on query values. It extracts the "page" and "limit" values from the provided URL query parameters, and computes the corresponding page, limit, and offset values. If the "page" or "limit" values are not provided or are invalid, it defaults to page 1 and a limit of 10, or a user-specified default limit if provided.

Parameters:

query: URL query parameters containing "page" and "limit" values.
defaultLimit: Optional parameter to specify a default limit if "limit" is not provided.

Returns:

page: The current page number.
limit: The number of items per page.
offset: The offset for the database query, calculated as (page - 1) * limit.

func GetSorts added in v1.4.0

func GetSorts(urlQuery url.Values, allowedSort []string) (sorts bson.D)

GetSorts extracts sorting parameters from the URL query and returns them as a bson.D object. It only allows sorting by fields specified in the allowedSort slice. If the "sort" parameter is not provided or is not in the allowedSort list, it returns an empty bson.D. Example usage:

// Simple
urlQuery := url.Values{
	"sort": []string{"name"},
	"dir": []string{"desc"},
}

// Multiple
urlQuery := url.Values{
	"sorts": []string{"name:asc,age:desc"},
}

Parameters:

  • urlQuery: url.Values containing the URL query parameters.
  • allowedSort: []string containing the list of allowed fields for sorting.

Returns:

  • sorts: bson.D containing the sorting parameters.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL