mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
31 lines
755 B
Go
31 lines
755 B
Go
package helper
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"strconv"
|
|
)
|
|
|
|
func GetIDOfElement(c *gin.Context, elementName string, source string, providedID int) (int, error) {
|
|
|
|
if source == "path" {
|
|
id, err := strconv.Atoi(c.Param(elementName))
|
|
if err != nil {
|
|
BadRequestError(c, fmt.Sprintf("No or incorrect format of path parameter"))
|
|
return -1, err
|
|
}
|
|
return id, nil
|
|
} else if source == "query" {
|
|
id, err := strconv.Atoi(c.Request.URL.Query().Get(elementName))
|
|
if err != nil {
|
|
BadRequestError(c, fmt.Sprintf("No or incorrect format of query parameter"))
|
|
return -1, err
|
|
}
|
|
return id, nil
|
|
} else if source == "body" {
|
|
id := providedID
|
|
return id, nil
|
|
} else {
|
|
return -1, fmt.Errorf("invalid source of element ID")
|
|
}
|
|
}
|