70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package backup
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"online-back/internal/shared"
|
|
)
|
|
|
|
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 parsePageLimit(c *gin.Context) (page, limit int, ok bool) {
|
|
page = shared.DefaultPage
|
|
limit = shared.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 > shared.MaxLimit {
|
|
l = shared.MaxLimit
|
|
}
|
|
limit = l
|
|
}
|
|
|
|
return page, limit, true
|
|
}
|
|
|
|
func currentUserID(c *gin.Context) *int64 {
|
|
rawID, exists := c.Get("id")
|
|
if !exists {
|
|
return nil
|
|
}
|
|
switch id := rawID.(type) {
|
|
case int64:
|
|
if id > 0 {
|
|
return &id
|
|
}
|
|
case int:
|
|
if id > 0 {
|
|
v := int64(id)
|
|
return &v
|
|
}
|
|
case string:
|
|
parsed, err := strconv.ParseInt(id, 10, 64)
|
|
if err == nil && parsed > 0 {
|
|
return &parsed
|
|
}
|
|
}
|
|
return nil
|
|
}
|