Nest user/branch/cardType/cardState on card, order, pin-order, and visa-master reads; accept flexible dates/IDs on pin create; widen pin region; ignore undefined list query params; extract internal/cards/shared to remove duplicated types and helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package shared
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
DefaultPage = 1
|
|
DefaultLimit = 10
|
|
MaxLimit = 100
|
|
)
|
|
|
|
func ParseID(raw string) (int64, bool) {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || id <= 0 {
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func QueryValue(c *gin.Context, keys ...string) string {
|
|
for _, key := range keys {
|
|
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
|
return raw
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func OptionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
|
|
raw := QueryValue(c, keys...)
|
|
if raw == "" {
|
|
return nil, true
|
|
}
|
|
id, ok := ParseID(raw)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return &id, true
|
|
}
|
|
|
|
func ParsePageLimit(c *gin.Context) (page, limit int, ok bool) {
|
|
page = DefaultPage
|
|
limit = DefaultLimit
|
|
|
|
if raw := QueryValue(c, "page"); raw != "" {
|
|
p, err := strconv.Atoi(raw)
|
|
if err != nil || p < 1 {
|
|
return 0, 0, false
|
|
}
|
|
page = p
|
|
}
|
|
|
|
if raw := QueryValue(c, "limit"); raw != "" {
|
|
l, err := strconv.Atoi(raw)
|
|
if err != nil || l < 1 {
|
|
return 0, 0, false
|
|
}
|
|
if l > MaxLimit {
|
|
l = MaxLimit
|
|
}
|
|
limit = l
|
|
}
|
|
|
|
return page, limit, true
|
|
}
|