Apparently for 2 weeks (yes 2 whole fucking weeks) I was using NamedExec wrong instead of MustExec so inserts haven't worked since then. Other than that, this is a more cleaned up MVC style setup than before.
This commit is contained in:
parent
1785a8e4f5
commit
bac4472c3a
5 changed files with 127 additions and 93 deletions
43
api/tools.go
Normal file
43
api/tools.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
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
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue