package validations

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

var settingKey = "validations:errors"

func AddError(db *gorm.DB, resource interface{}, err string) {
	var errors = GetErrors(db)
	var scope = db.NewScope(resource)

	key := fmt.Sprintf("%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
	errors[key] = append(errors[key], err)

	db.InstantSet(settingKey, errors)
}

func AddErrorForColumn(db *gorm.DB, resource interface{}, column, err string) {
	var errors = GetErrors(db)
	var scope = db.NewScope(resource)

	key := fmt.Sprintf("%v_%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue(), column)
	errors[key] = append(errors[key], err)

	db.InstantSet(settingKey, errors)
}

func GetErrors(db *gorm.DB) map[string][]string {
	var errors = map[string][]string{}
	if e, ok := db.Get(settingKey); ok {
		errors = e.(map[string][]string)
	}
	return errors
}

package validations

import (
	"errors"
	"fmt"

	"github.com/jinzhu/gorm"
)

var settingKey = "validations:errors"

func AddError(db *gorm.DB, resource interface{}, err string) {
	var validationErrors = GetErrors(db)
	var scope = db.NewScope(resource)

	key := fmt.Sprintf("%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
	validationErrors[key] = append(validationErrors[key], err)

	db.InstantSet(settingKey, validationErrors).Error = errors.New(err)
}

func AddErrorForColumn(db *gorm.DB, resource interface{}, column, err string) {
	var validationErrors = GetErrors(db)
	var scope = db.NewScope(resource)

	key := fmt.Sprintf("%v_%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue(), column)
	validationErrors[key] = append(validationErrors[key], err)

	db.InstantSet(settingKey, validationErrors).Error = errors.New(err)
}

func GetErrors(db *gorm.DB) map[string][]string {
	var validationErrors = map[string][]string{}
	if errors, ok := db.Get(settingKey); ok {
		validationErrors = errors.(map[string][]string)
	}
	return validationErrors
}

Set DB's Error if get any validation error
Set DB's Error if get any validation error
