package api import ( "net/http" "github.com/go-playground/validator/v10" "github.com/labstack/echo/v4" ) type Response[T any] struct { Code int `json:"code"` Message string `json:"message"` Data T `json:"data"` } type Pagination[T any] struct { Page int `json:"page"` Size int `json:"size"` Total int `json:"total"` Data []T `json:"data"` } type CustomValidator struct { validator *validator.Validate } func (cv *CustomValidator) Validate(i interface{}) error { return cv.validator.Struct(i) } func NewCustomValidator() *CustomValidator { return &CustomValidator{validator: validator.New()} } func BindAndValidate(c echo.Context, i interface{}) error { if err := c.Bind(i); err != nil { return err } if err := c.Validate(i); err != nil { return err } return nil } func Success[T any](c echo.Context, data T, message string) error { return c.JSON(http.StatusOK, Response[T]{ Code: http.StatusOK, Message: message, Data: data, }) } func Failed(c echo.Context, message string) error { return c.JSON(http.StatusOK, Response[interface{}]{ Code: http.StatusInternalServerError, Message: message, Data: nil, }) } func Error(c echo.Context, code int, message string) error { return c.JSON(code, Response[interface{}]{ Code: code, Message: message, Data: nil, }) } func Paginate[T any](c echo.Context, data []T, page, size, total int, message string) error { return Success(c, Pagination[T]{ Page: page, Size: size, Total: total, Data: data, }, message, ) }