Extract GetUserID and ParseID to boilerplate.go in handlers package

This commit is contained in:
qowevisa 2024-10-31 11:05:52 +02:00
parent 255f812078
commit b09e85b4cb

39
handlers/boilerplate.go Normal file
View File

@ -0,0 +1,39 @@
package handlers
import (
"errors"
"fmt"
"strconv"
"github.com/gin-gonic/gin"
)
var (
ERROR_UTIL_USERIDER1 = errors.New("internal error 001: UserID not found")
ERROR_UTIL_USERIDER2 = errors.New("internal error 002: UserID conversion error")
)
func GetUserID(c *gin.Context) (uint, error) {
userIDAny, exists := c.Get("UserID")
if !exists {
return 0, ERROR_UTIL_USERIDER1
}
userID, ok := userIDAny.(uint)
if !ok {
return 0, ERROR_UTIL_USERIDER2
}
return userID, nil
}
//
// Parses id as uint
func ParseID(c *gin.Context) (uint, error) {
idStr := c.Param("id")
idVal, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid request: %w", err)
}
return uint(idVal), nil
}