2024-07-13 12:33:07 +00:00
|
|
|
package agentkindboxvalidator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-07-13 12:33:07 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-08-01 10:20:18 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2024-07-13 12:33:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
|
|
|
KindBoxExistForAgent(ctx context.Context, kindBoxID, agentID uint) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Validator struct {
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository) Validator {
|
|
|
|
return Validator{repo: repo}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesKindBoxExistForAgent(ctx context.Context, agentID uint) validation.RuleFunc {
|
2024-07-13 12:33:07 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
kindBoxID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
exists, err := v.repo.KindBoxExistForAgent(ctx, kindBoxID, agentID)
|
2024-07-13 12:33:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|