initial commit

This commit is contained in:
kandrusyak
2023-06-23 20:51:41 +03:00
parent 438dc1a716
commit 8e9eeb989c
13 changed files with 458 additions and 0 deletions

52
pkg/utils/url.go Normal file
View File

@@ -0,0 +1,52 @@
package utils
import (
"net/url"
"strconv"
"strings"
)
func GetQueryParam(query url.Values, key string, defaultVal string) string {
data := query.Get(key)
if data == "" {
return defaultVal
}
return data
}
func GetPaginationParams(query url.Values) (int, int) {
offset := GetQueryParam(query, "offset", "0")
limit := GetQueryParam(query, "limit", "10")
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 10
}
offsetInt, err := strconv.Atoi(offset)
if err != nil {
offsetInt = 0
}
return limitInt, offsetInt
}
func GetFilterParams(query url.Values) (string, bool, string, string) {
sort := GetQueryParam(query, "sort", "id:asc")
filter := GetQueryParam(query, "filter", ":")
filters := strings.Split(filter, ":")
sorts := strings.Split(sort, ":")
isDesc := false
if sorts[1] == "desc" {
isDesc = true
}
return sorts[0], isDesc, filters[0], filters[1]
}