You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
846 B
43 lines
846 B
package api
|
|
|
|
import (
|
|
"log"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type Failure struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func IfErrNil(err error, c *fiber.Ctx) error {
|
|
if err != nil {
|
|
log.Output(10, err.Error())
|
|
c.SendStatus(fiber.StatusInternalServerError)
|
|
return c.JSON(Failure{err.Error()})
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func ReceivePost[T any](c *fiber.Ctx) (*T, error) {
|
|
var result *T
|
|
result = new(T)
|
|
|
|
if err := c.BodyParser(result); err != nil {
|
|
log.Println(err);
|
|
return result, err
|
|
}
|
|
|
|
var validate *validator.Validate
|
|
validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
if err := validate.Struct(result); err != nil {
|
|
validationErrors := err.(validator.ValidationErrors)
|
|
log.Println(validationErrors)
|
|
return result, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
|