merch-api/internal/merch/helper.go
2026-03-13 16:51:14 +03:00

48 lines
924 B
Go

package merch
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
"strings"
"time"
)
const (
pkgLogHeader = "Merch"
)
// logDebug wrapper for log messages
func logDebug(header, msg string) {
log.Debugf("%v %v %v", pkgLogHeader, header, msg)
}
// logErr wrapper for log messages
func logErr(header string, err error) {
log.WithError(err).Errorf("%v %v", pkgLogHeader, header)
}
func logWarn(header string, msg string) {
log.WithField("warning", msg).Warnf("%v %v", pkgLogHeader, header)
}
func getUserId(c *gin.Context) int64 {
id, _ := c.Get("userId")
return id.(int64)
}
func getPeriod(days int) time.Time {
if days > 365 {
days = 7
}
return time.Now().UTC().Add(-(time.Duration(days) * time.Hour * 24))
}
func getDays(c *gin.Context) int {
daysQuery := strings.ToLower(c.Query("days"))
days, err := strconv.Atoi(daysQuery)
if err != nil {
days = 7
}
return days
}