From 1c96539af299970b5680a35b33121ed8ff936409 Mon Sep 17 00:00:00 2001 From: miaad shahrokhi Date: Thu, 21 Dec 2023 16:30:31 +0330 Subject: [PATCH] feat(niki): add ozo package and add validation for kindbox and kindboxreq --- param/kind_box/delete.go | 2 +- param/kind_box/get_by_id.go | 2 +- param/kind_box_req/delete.go | 2 +- pkg/err_msg/message.go | 1 + validator/kind_box/delete.go | 33 +++++++++++++++++++++ validator/kind_box/get_by_id.go | 33 +++++++++++++++++++++ validator/kind_box/update.go | 43 +++++++++++++++++++++++++++ validator/kind_box/validator.go | 40 +++++++++++++++++++++++++ validator/kind_box_req/add.go | 44 ++++++++++++++++++++++++++++ validator/kind_box_req/delete.go | 33 +++++++++++++++++++++ validator/kind_box_req/get_by_id.go | 33 +++++++++++++++++++++ validator/kind_box_req/update.go | 43 +++++++++++++++++++++++++++ validator/kind_box_req/validator.go | 45 +++++++++++++++++++++++++++++ 13 files changed, 351 insertions(+), 3 deletions(-) create mode 100644 validator/kind_box/delete.go create mode 100644 validator/kind_box/get_by_id.go create mode 100644 validator/kind_box/update.go create mode 100644 validator/kind_box/validator.go create mode 100644 validator/kind_box_req/add.go create mode 100644 validator/kind_box_req/delete.go create mode 100644 validator/kind_box_req/get_by_id.go create mode 100644 validator/kind_box_req/update.go create mode 100644 validator/kind_box_req/validator.go diff --git a/param/kind_box/delete.go b/param/kind_box/delete.go index 83df716..82e56c9 100644 --- a/param/kind_box/delete.go +++ b/param/kind_box/delete.go @@ -1,7 +1,7 @@ package param type KindBoxDeleteRequest struct { - kindBoxID uint + KindBoxID uint } type KindBoxDeleteResponse struct{} diff --git a/param/kind_box/get_by_id.go b/param/kind_box/get_by_id.go index 4e06d58..98da018 100644 --- a/param/kind_box/get_by_id.go +++ b/param/kind_box/get_by_id.go @@ -3,7 +3,7 @@ package param import "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxGetByIDRequest struct { - kindBoxID uint + KindBoxID uint } type KindBoxGetByIDResponse struct { diff --git a/param/kind_box_req/delete.go b/param/kind_box_req/delete.go index e09fda2..a2c7b30 100644 --- a/param/kind_box_req/delete.go +++ b/param/kind_box_req/delete.go @@ -1,7 +1,7 @@ package param type KindBoxReqDeleteRequest struct { - kindBoxID uint + KindBoxReqID uint } type KindBoxReqDeleteResponse struct{} diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 2d67a71..6041a9b 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -3,4 +3,5 @@ package errmsg const ( ErrorMsgNotFound = "record not found" ErrorMsgSomethingWentWrong = "something went wrong" + ErrorMsgInvalidInput = "invalid input" ) diff --git a/validator/kind_box/delete.go b/validator/kind_box/delete.go new file mode 100644 index 0000000..e9838e8 --- /dev/null +++ b/validator/kind_box/delete.go @@ -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 +} diff --git a/validator/kind_box/get_by_id.go b/validator/kind_box/get_by_id.go new file mode 100644 index 0000000..2df433a --- /dev/null +++ b/validator/kind_box/get_by_id.go @@ -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 +} diff --git a/validator/kind_box/update.go b/validator/kind_box/update.go new file mode 100644 index 0000000..c6dde7f --- /dev/null +++ b/validator/kind_box/update.go @@ -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 +} diff --git a/validator/kind_box/validator.go b/validator/kind_box/validator.go new file mode 100644 index 0000000..ca9b354 --- /dev/null +++ b/validator/kind_box/validator.go @@ -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 +} diff --git a/validator/kind_box_req/add.go b/validator/kind_box_req/add.go new file mode 100644 index 0000000..d00ecb4 --- /dev/null +++ b/validator/kind_box_req/add.go @@ -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 +} diff --git a/validator/kind_box_req/delete.go b/validator/kind_box_req/delete.go new file mode 100644 index 0000000..e218a38 --- /dev/null +++ b/validator/kind_box_req/delete.go @@ -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 +} diff --git a/validator/kind_box_req/get_by_id.go b/validator/kind_box_req/get_by_id.go new file mode 100644 index 0000000..b461863 --- /dev/null +++ b/validator/kind_box_req/get_by_id.go @@ -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 +} diff --git a/validator/kind_box_req/update.go b/validator/kind_box_req/update.go new file mode 100644 index 0000000..4428878 --- /dev/null +++ b/validator/kind_box_req/update.go @@ -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 +} diff --git a/validator/kind_box_req/validator.go b/validator/kind_box_req/validator.go new file mode 100644 index 0000000..dcc99fa --- /dev/null +++ b/validator/kind_box_req/validator.go @@ -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 +}