package app

import (
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"net/http"

	"bwallet/pkg/e"
	"fmt"
)

// BindAndValid binds and validates data
func BindAndValid(c *gin.Context, form interface{}) (int, int) {
	err := c.Bind(form)

	if err != nil {
		fmt.Println("aa")
		fmt.Println(err)
		return http.StatusBadRequest, e.INVALID_PARAMS
	}

	valid := validation.Validation{}
	check, err := valid.Valid(form)
	if err != nil {
		fmt.Println("bb")
		fmt.Println(err)
		return http.StatusInternalServerError, e.ERROR
	}
	if !check {
		fmt.Println("cc")
		fmt.Println(valid.Errors)
		MarkErrors(valid.Errors)
		return http.StatusBadRequest, e.INVALID_PARAMS
	}

	return http.StatusOK, e.SUCCESS
}
