From 4697645e64fb604d6d71d76772ca8e0b28490c45 Mon Sep 17 00:00:00 2001 From: miaad shahrokhi Date: Thu, 28 Dec 2023 01:25:15 +0330 Subject: [PATCH] feat(validation): update validation section --- validator/admin/kind_box/add.go | 46 ++++++++++ .../update.go => admin/kind_box/delete.go} | 18 ++-- validator/admin/kind_box/get.go | 41 +++++++++ validator/admin/kind_box/update.go | 54 ++++++++++++ validator/admin/kind_box/validator.go | 85 +++++++++++++++++++ validator/{ => admin}/kind_box_req/add.go | 8 +- validator/{ => admin}/kind_box_req/delete.go | 16 +++- .../kind_box_req/get.go} | 18 ++-- validator/{ => admin}/kind_box_req/update.go | 19 +++-- validator/admin/kind_box_req/validator.go | 68 +++++++++++++++ validator/benefactor/kind_box/get.go | 41 +++++++++ .../kind_box/get_all.go} | 14 +-- .../{ => benefactor}/kind_box/validator.go | 18 ++-- validator/benefactor/kind_box_req/add.go | 44 ++++++++++ validator/benefactor/kind_box_req/delete.go | 41 +++++++++ validator/benefactor/kind_box_req/get.go | 41 +++++++++ .../kind_box_req/get_all.go} | 14 +-- validator/benefactor/kind_box_req/update.go | 47 ++++++++++ .../kind_box_req/validator.go | 19 ++++- 19 files changed, 598 insertions(+), 54 deletions(-) create mode 100644 validator/admin/kind_box/add.go rename validator/{kind_box/update.go => admin/kind_box/delete.go} (63%) create mode 100644 validator/admin/kind_box/get.go create mode 100644 validator/admin/kind_box/update.go create mode 100644 validator/admin/kind_box/validator.go rename validator/{ => admin}/kind_box_req/add.go (85%) rename validator/{ => admin}/kind_box_req/delete.go (60%) rename validator/{kind_box_req/get_by_id.go => admin/kind_box_req/get.go} (57%) rename validator/{ => admin}/kind_box_req/update.go (59%) create mode 100644 validator/admin/kind_box_req/validator.go create mode 100644 validator/benefactor/kind_box/get.go rename validator/{kind_box/delete.go => benefactor/kind_box/get_all.go} (62%) rename validator/{ => benefactor}/kind_box/validator.go (50%) create mode 100644 validator/benefactor/kind_box_req/add.go create mode 100644 validator/benefactor/kind_box_req/delete.go create mode 100644 validator/benefactor/kind_box_req/get.go rename validator/{kind_box/get_by_id.go => benefactor/kind_box_req/get_all.go} (57%) create mode 100644 validator/benefactor/kind_box_req/update.go rename validator/{ => benefactor}/kind_box_req/validator.go (58%) diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go new file mode 100644 index 0000000..07a1e4d --- /dev/null +++ b/validator/admin/kind_box/add.go @@ -0,0 +1,46 @@ +package adminkindbox + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/admin/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) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[string]string, error) { + const op = "adminkindbox.KindBoxAddRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.SerialNumber, validation.Required), + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.SenderID, + validation.Required, + validation.By(v.doesEmployeeExist(&req.SenderID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + ); 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/admin/kind_box/delete.go similarity index 63% rename from validator/kind_box/update.go rename to validator/admin/kind_box/delete.go index 7b9faa5..dbca318 100644 --- a/validator/kind_box/update.go +++ b/validator/admin/kind_box/delete.go @@ -1,25 +1,23 @@ -package kindbox +package adminkindbox import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/admin/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" +func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[string]string, error) { + const op = "adminkindbox.ValidateDeleteRequest" if err := validation.ValidateStruct(&req, - validation.Field(&req.KindBox.SerialNumber, validation.Required), - - validation.Field(&req.KindBox.ReceiverID, + validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesReceiverUserExist)), + validation.By(v.doesBenefactorExist(&req.BenefactorID))), - validation.Field(&req.KindBox.SenderID, + validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesSenderUserExist)), + validation.By(v.doesKindBoxExist(&req.KindBoxID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/get.go b/validator/admin/kind_box/get.go new file mode 100644 index 0000000..2019dd8 --- /dev/null +++ b/validator/admin/kind_box/get.go @@ -0,0 +1,41 @@ +package adminkindbox + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/admin/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.KindBoxGetRequest) (map[string]string, error) { + const op = "adminkindbox.ValidateGetRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxID, + validation.Required, + validation.By(v.doesKindBoxExist(&req.KindBoxID))), + ); 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/admin/kind_box/update.go b/validator/admin/kind_box/update.go new file mode 100644 index 0000000..24a1aa6 --- /dev/null +++ b/validator/admin/kind_box/update.go @@ -0,0 +1,54 @@ +package adminkindbox + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/admin/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 = "adminkindbox.ValidateUpdateRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxID, + validation.Required, + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), + + validation.Field(&req.SerialNumber, validation.EnsureString), + + validation.Field(&req.TotalAmount, validation.Min(0)), + + validation.Field(&req.SenderID, + validation.By(v.doesEmployeeExist(req.SenderID))), + + validation.Field(&req.ReceiverID, + validation.By(v.doesEmployeeExist(req.ReceiverID))), + + validation.Field(&req.Status, + validation.By(v.hasCorrectStatus(req.Status.String))), + ); 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/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go new file mode 100644 index 0000000..7d24b2e --- /dev/null +++ b/validator/admin/kind_box/validator.go @@ -0,0 +1,85 @@ +package adminkindbox + +import ( + "fmt" + + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" +) + +type Repository interface { + KindBoxRequestExist(id uint) (bool, error) + EmployeeExist(id uint) (bool, error) + BenefactorExist(id uint) (bool, error) + KindBoxExist(id uint) (bool, error) + KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) + CheckStatus(status string) (bool, error) +} + +type Validator struct { + repo Repository +} + +func New(repo Repository) Validator { + return Validator{repo: repo} +} + +func (v Validator) doesKindBoxRequestExist(value interface{}) error { + receiverID := value.(uint) + _, err := v.repo.KindBoxRequestExist(receiverID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesEmployeeExist(value interface{}) error { + senderID := value.(uint) + _, err := v.repo.EmployeeExist(senderID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesBenefactorExist(value interface{}) error { + benefactorID := value.(uint) + _, err := v.repo.BenefactorExist(benefactorID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesKindBoxExist(value interface{}) error { + kindboxID := value.(uint) + _, err := v.repo.KindBoxExist(kindboxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { + kbId := kindBoxID.(uint) + bId := benefactorID.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) hasCorrectStatus(value interface{}) error { + status := value.(string) + _, err := v.repo.CheckStatus(status) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} diff --git a/validator/kind_box_req/add.go b/validator/admin/kind_box_req/add.go similarity index 85% rename from validator/kind_box_req/add.go rename to validator/admin/kind_box_req/add.go index cc9b454..9a171ce 100644 --- a/validator/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -1,14 +1,14 @@ -package kindboxreq +package adminkindboxreq import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/admin/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" + const op = "adminkindboxreq.ValidateAddRequest" if err := validation.ValidateStruct(&req, @@ -16,7 +16,7 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBeneFactorExist)), + validation.By(v.doesBenefactorExist)), validation.Field(&req.TypeID, validation.Required, diff --git a/validator/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go similarity index 60% rename from validator/kind_box_req/delete.go rename to validator/admin/kind_box_req/delete.go index e218a38..d0f2caa 100644 --- a/validator/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -1,16 +1,24 @@ -package kindboxreq +package adminkindboxreq import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/admin/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" + const op = "adminkindboxreq.ValidateDeleteRequest" - if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil { + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(req.BenefactorID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), + ); err != nil { fieldErrors := make(map[string]string) errV, ok := err.(validation.Errors) diff --git a/validator/kind_box_req/get_by_id.go b/validator/admin/kind_box_req/get.go similarity index 57% rename from validator/kind_box_req/get_by_id.go rename to validator/admin/kind_box_req/get.go index 9f88708..0ce8b4c 100644 --- a/validator/kind_box_req/get_by_id.go +++ b/validator/admin/kind_box_req/get.go @@ -1,16 +1,24 @@ -package kindboxreq +package adminkindboxreq import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/admin/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" +func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { + const op = "adminkindboxreq.ValidateGetRequest" - if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil { + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(req.BenefactorID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), + ); err != nil { fieldErrors := make(map[string]string) errV, ok := err.(validation.Errors) diff --git a/validator/kind_box_req/update.go b/validator/admin/kind_box_req/update.go similarity index 59% rename from validator/kind_box_req/update.go rename to validator/admin/kind_box_req/update.go index 4428878..adce29f 100644 --- a/validator/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -1,25 +1,28 @@ -package kindboxreq +package adminkindboxreq import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/admin/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" + const op = "adminkindboxreq.ValidateUpdateRequest" if err := validation.ValidateStruct(&req, - validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), + validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), - validation.Field(&req.KindBoxReq.BenefactorID, + validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBeneFactorExist)), + validation.By(v.doesBenefactorExist(req.BenefactorID))), - validation.Field(&req.KindBoxReq.TypeID, + validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesTypeExist)), + validation.By(v.doesKindBoxBelongToBenefactor(req.KindBoxReqID, req.BenefactorID))), + + validation.Field(&req.TypeID, + validation.By(v.doesTypeExist(req.TypeID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go new file mode 100644 index 0000000..dd00411 --- /dev/null +++ b/validator/admin/kind_box_req/validator.go @@ -0,0 +1,68 @@ +package adminkindboxreq + +import ( + "fmt" + + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" +) + +const ( + MinKindBoxReq = 1 + MaxKindBoxReq = 100 +) + +type Repository interface { + BenefactorExist(id int) (bool, error) + KindBoxRequestExist(id int) (bool, error) + TypeExist(id int) (bool, error) + KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (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) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error { + kbId := kindBoxID.(uint) + bId := benefactorID.(uint) + _, err := v.repo.KindBoxBelongToBenefactor(bId, kbId) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} + +func (v Validator) doesKindBoxRequestExist(value interface{}) error { + kindboxreqID := value.(int) + _, err := v.repo.KindBoxRequestExist(kindboxreqID) + 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 +} diff --git a/validator/benefactor/kind_box/get.go b/validator/benefactor/kind_box/get.go new file mode 100644 index 0000000..1021bb4 --- /dev/null +++ b/validator/benefactor/kind_box/get.go @@ -0,0 +1,41 @@ +package userkindbox + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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.KindBoxGetRequest) (map[string]string, error) { + const op = "userkindbox.ValidateGetRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxID, + validation.Required, + validation.By(v.doesKindBoxExist(&req.KindBoxID))), + ); 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/delete.go b/validator/benefactor/kind_box/get_all.go similarity index 62% rename from validator/kind_box/delete.go rename to validator/benefactor/kind_box/get_all.go index e9838e8..30225af 100644 --- a/validator/kind_box/delete.go +++ b/validator/benefactor/kind_box/get_all.go @@ -1,16 +1,20 @@ -package kindbox +package userkindbox import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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" +func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[string]string, error) { + const op = "userkindbox.ValidateGetAllRequest" - if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil { + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + ); err != nil { fieldErrors := make(map[string]string) errV, ok := err.(validation.Errors) diff --git a/validator/kind_box/validator.go b/validator/benefactor/kind_box/validator.go similarity index 50% rename from validator/kind_box/validator.go rename to validator/benefactor/kind_box/validator.go index a911681..8aa06f5 100644 --- a/validator/kind_box/validator.go +++ b/validator/benefactor/kind_box/validator.go @@ -1,4 +1,4 @@ -package kindbox +package userkindbox import ( "fmt" @@ -7,8 +7,8 @@ import ( ) type Repository interface { - ReceiverUserExist(id int) (bool, error) - SenderUserExist(id int) (bool, error) + BenefactorExist(id int) (bool, error) + KindBoxExist(id int) (bool, error) } type Validator struct { @@ -19,9 +19,9 @@ func New(repo Repository) Validator { return Validator{repo: repo} } -func (v Validator) doesReceiverUserExist(value interface{}) error { - receiverID := value.(int) - _, err := v.repo.ReceiverUserExist(receiverID) +func (v Validator) doesBenefactorExist(value interface{}) error { + benefactorID := value.(int) + _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -29,9 +29,9 @@ func (v Validator) doesReceiverUserExist(value interface{}) error { return nil } -func (v Validator) doesSenderUserExist(value interface{}) error { - senderID := value.(int) - _, err := v.repo.SenderUserExist(senderID) +func (v Validator) doesKindBoxExist(value interface{}) error { + kindBoxID := value.(int) + _, err := v.repo.KindBoxExist(kindBoxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go new file mode 100644 index 0000000..c50f975 --- /dev/null +++ b/validator/benefactor/kind_box_req/add.go @@ -0,0 +1,44 @@ +package userkindboxreq + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 = "userkindboxreq.ValidateAddRequest" + + if err := validation.ValidateStruct(&req, + + validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), + + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.TypeID, + validation.Required, + validation.By(v.doesTypeExist(&req.TypeID))), + ); 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/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go new file mode 100644 index 0000000..11f4e3a --- /dev/null +++ b/validator/benefactor/kind_box_req/delete.go @@ -0,0 +1,41 @@ +package userkindboxreq + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 = "userkindboxreq.ValidateDeleteRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + ); 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/benefactor/kind_box_req/get.go b/validator/benefactor/kind_box_req/get.go new file mode 100644 index 0000000..41cc13e --- /dev/null +++ b/validator/benefactor/kind_box_req/get.go @@ -0,0 +1,41 @@ +package userkindboxreq + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { + const op = "userkindboxreq.ValidateGetRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + ); 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/benefactor/kind_box_req/get_all.go similarity index 57% rename from validator/kind_box/get_by_id.go rename to validator/benefactor/kind_box_req/get_all.go index 1223f02..d1e77cb 100644 --- a/validator/kind_box/get_by_id.go +++ b/validator/benefactor/kind_box_req/get_all.go @@ -1,16 +1,20 @@ -package kindbox +package userkindboxreq import ( - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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.KindBoxGetByIDRequest) (map[string]string, error) { - const op = "kindbox.ValidateGetByIDRequest" +func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map[string]string, error) { + const op = "userkindboxreq.ValidateGetAllRequest" - if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil { + if err := validation.ValidateStruct(&req, + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + ); err != nil { fieldErrors := make(map[string]string) errV, ok := err.(validation.Errors) diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go new file mode 100644 index 0000000..5817909 --- /dev/null +++ b/validator/benefactor/kind_box_req/update.go @@ -0,0 +1,47 @@ +package userkindboxreq + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 = "userkindboxreq.ValidateUpdateRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), + + validation.Field(&req.BenefactorID, + validation.Required, + validation.By(v.doesBenefactorExist(&req.BenefactorID))), + + validation.Field(&req.KindBoxReqID, + validation.Required, + validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + + validation.Field(&req.TypeID, + validation.Required, + validation.By(v.doesTypeExist(&req.TypeID))), + ); 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/benefactor/kind_box_req/validator.go similarity index 58% rename from validator/kind_box_req/validator.go rename to validator/benefactor/kind_box_req/validator.go index 574d10c..ae73c3d 100644 --- a/validator/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -1,4 +1,4 @@ -package kindboxreq +package userkindboxreq import ( "fmt" @@ -12,7 +12,8 @@ const ( ) type Repository interface { - BeneFactorExist(id int) (bool, error) + BenefactorExist(id int) (bool, error) + KindBoxReqExist(id int) (bool, error) TypeExist(id int) (bool, error) } @@ -24,9 +25,9 @@ func New(repo Repository) Validator { return Validator{repo: repo} } -func (v Validator) doesBeneFactorExist(value interface{}) error { +func (v Validator) doesBenefactorExist(value interface{}) error { benefactorID := value.(int) - _, err := v.repo.BeneFactorExist(benefactorID) + _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -43,3 +44,13 @@ func (v Validator) doesTypeExist(value interface{}) error { return nil } + +func (v Validator) doesKindBoxRequestExist(value interface{}) error { + kindBoxReqID := value.(int) + _, err := v.repo.KindBoxReqExist(kindBoxReqID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +}