package role

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

type DelReq struct {
	ID string `src:"path@id" desc:"Role ID"`
}

func del(x *vigo.X, req *DelReq) error {
	var role models.Role
	if err := cfg.DB().First(&role, "id = ?", req.ID).Error; err != nil {
		return vigo.ErrNotFound
	}

	if role.IsSystem {
		return vigo.NewError("cannot delete system role").WithCode(40300)
	}

	if err := cfg.DB().Delete(&role).Error; err != nil {
		return vigo.ErrDatabase.WithError(err)
	}

	return nil
}
