forked from ebhomengo/niki
				
			feat(niki): add ozo package and add validation for kindbox and kindboxreq
This commit is contained in:
		
							parent
							
								
									359cfc3244
								
							
						
					
					
						commit
						1c96539af2
					
				| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
package param
 | 
			
		||||
 | 
			
		||||
type KindBoxDeleteRequest struct {
 | 
			
		||||
	kindBoxID uint
 | 
			
		||||
	KindBoxID uint
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KindBoxDeleteResponse struct{}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ package param
 | 
			
		|||
import "git.gocasts.ir/ebhomengo/niki/entity"
 | 
			
		||||
 | 
			
		||||
type KindBoxGetByIDRequest struct {
 | 
			
		||||
	kindBoxID uint
 | 
			
		||||
	KindBoxID uint
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KindBoxGetByIDResponse struct {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
package param
 | 
			
		||||
 | 
			
		||||
type KindBoxReqDeleteRequest struct {
 | 
			
		||||
	kindBoxID uint
 | 
			
		||||
	KindBoxReqID uint
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KindBoxReqDeleteResponse struct{}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,4 +3,5 @@ package errmsg
 | 
			
		|||
const (
 | 
			
		||||
	ErrorMsgNotFound           = "record not found"
 | 
			
		||||
	ErrorMsgSomethingWentWrong = "something went wrong"
 | 
			
		||||
	ErrorMsgInvalidInput       = "invalid input"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
package kindbox
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindbox.ValidateDeleteRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
package kindbox
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateGetByIdRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindbox.ValidateGetByIdRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
package kindbox
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindbox.ValidateUpdateRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.ValidateStruct(&req,
 | 
			
		||||
		validation.Field(&req.KindBox.SerialNumber, validation.Required),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBox.ReceiverId,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesReceiverUserExist)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBox.SenderID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesSenderUserExist)),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
package kindbox
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Repository interface {
 | 
			
		||||
	ReceiverUserExist(id int) (bool, error)
 | 
			
		||||
	SenderUserExist(id int) (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Validator struct {
 | 
			
		||||
	repo Repository
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(repo Repository) Validator {
 | 
			
		||||
	return Validator{repo: repo}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v Validator) doesReceiverUserExist(value interface{}) error {
 | 
			
		||||
	receiverId := value.(int)
 | 
			
		||||
	_, err := v.repo.ReceiverUserExist(receiverId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v Validator) doesSenderUserExist(value interface{}) error {
 | 
			
		||||
	senderId := value.(int)
 | 
			
		||||
	_, err := v.repo.SenderUserExist(senderId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
package kindboxreq
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindboxreq.ValidateAddRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.ValidateStruct(&req,
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBoxReq.BenefactorID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesBeneFactorExist)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBoxReq.TypeID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesTypeExist)),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
package kindboxreq
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindboxreq.ValidateDeleteRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
package kindboxreq
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateGetByIdRequest(req param.KindBoxReqGetByIDRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindboxreq.ValidateGetByIdRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
package kindboxreq
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req"
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
 | 
			
		||||
	validation "github.com/go-ozzo/ozzo-validation/v4"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) {
 | 
			
		||||
	const op = "kindboxreq.ValidateUpdateRequest"
 | 
			
		||||
 | 
			
		||||
	if err := validation.ValidateStruct(&req,
 | 
			
		||||
		validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBoxReq.BenefactorID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesBeneFactorExist)),
 | 
			
		||||
 | 
			
		||||
		validation.Field(&req.KindBoxReq.TypeID,
 | 
			
		||||
			validation.Required,
 | 
			
		||||
			validation.By(v.doesTypeExist)),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		fieldErrors := make(map[string]string)
 | 
			
		||||
 | 
			
		||||
		errV, ok := err.(validation.Errors)
 | 
			
		||||
		if ok {
 | 
			
		||||
			for key, value := range errV {
 | 
			
		||||
				if value != nil {
 | 
			
		||||
					fieldErrors[key] = value.Error()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return fieldErrors, richerror.New(op).
 | 
			
		||||
			WithMessage(errmsg.ErrorMsgInvalidInput).
 | 
			
		||||
			WithKind(richerror.KindInvalid).
 | 
			
		||||
			WithMeta(map[string]interface{}{"req": req}).
 | 
			
		||||
			WithErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
package kindboxreq
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	MinKindBoxReq = 1
 | 
			
		||||
	MaxKindBoxReq = 100
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Repository interface {
 | 
			
		||||
	BeneFactorExist(id int) (bool, error)
 | 
			
		||||
	TypeExist(id int) (bool, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Validator struct {
 | 
			
		||||
	repo Repository
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func New(repo Repository) Validator {
 | 
			
		||||
	return Validator{repo: repo}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v Validator) doesBeneFactorExist(value interface{}) error {
 | 
			
		||||
	benefactorId := value.(int)
 | 
			
		||||
	_, err := v.repo.BeneFactorExist(benefactorId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (v Validator) doesTypeExist(value interface{}) error {
 | 
			
		||||
	typeId := value.(int)
 | 
			
		||||
	_, err := v.repo.TypeExist(typeId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf(errmsg.ErrorMsgNotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue