package app

import (
	"dsbqj-admin/pkg/logger"
	"errors"
	"github.com/astaxie/beego/validation"
	"github.com/unknwon/com"
)

// MarkErrors logs error logs
func MarkErrors(errors []*validation.Error) {
	for _, err := range errors {
		logger.Info(err.Key, err.Message)
	}

	return
}

func (g *Gin) ValidKey() string {
	return g.C.Param("key")
}

func (g *Gin) ValidId() (int, error) {
	id := com.StrTo(g.C.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id")

	if valid.HasErrors() {
		MarkErrors(valid.Errors)
		return id, errors.New("id illegal")
	}

	return id, nil
}

func (g *Gin) ValidCode() (int, error) {
	id := com.StrTo(g.C.Param("code")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "code")

	if valid.HasErrors() {
		MarkErrors(valid.Errors)
		return id, errors.New("code illegal")
	}

	return id, nil
}
