From 4a5cc910dbf502d2646260c07b7cb3ae6f09bb12 Mon Sep 17 00:00:00 2001 From: miaad shahrokhi Date: Thu, 28 Dec 2023 17:13:06 +0330 Subject: [PATCH] feat(niki): remove validation from service and add all validation to validation directory --- entity/kind_box_status.go | 64 +++++++++++++++++-- service/admin/kind_box/add.go | 9 --- service/admin/kind_box/delete.go | 13 ---- service/admin/kind_box/get.go | 4 -- service/admin/kind_box/update.go | 9 --- service/admin/kind_box_req/add.go | 9 --- service/admin/kind_box_req/delete.go | 13 ---- service/admin/kind_box_req/get.go | 4 -- service/benefactor/kind_box/get.go | 5 +- service/benefactor/kind_box_req/delete.go | 13 ---- service/benefactor/kind_box_req/get.go | 5 -- service/benefactor/kind_box_req/update.go | 12 ---- validator/admin/kind_box/add.go | 9 +-- validator/admin/kind_box/delete.go | 6 +- validator/admin/kind_box/get.go | 1 + validator/admin/kind_box/update.go | 9 ++- validator/admin/kind_box/validator.go | 11 ++++ validator/admin/kind_box_req/add.go | 4 +- validator/admin/kind_box_req/delete.go | 4 +- validator/admin/kind_box_req/get.go | 3 +- validator/admin/kind_box_req/update.go | 4 +- validator/admin/kind_box_req/validator.go | 11 ++++ validator/benefactor/kind_box/get.go | 5 +- validator/benefactor/kind_box/get_all.go | 2 +- validator/benefactor/kind_box/validator.go | 20 ++++-- validator/benefactor/kind_box_req/add.go | 4 +- validator/benefactor/kind_box_req/delete.go | 6 +- validator/benefactor/kind_box_req/get.go | 5 +- validator/benefactor/kind_box_req/get_all.go | 2 +- validator/benefactor/kind_box_req/update.go | 8 ++- .../benefactor/kind_box_req/validator.go | 35 ++++++++-- 31 files changed, 171 insertions(+), 138 deletions(-) diff --git a/entity/kind_box_status.go b/entity/kind_box_status.go index ab3d0a2..f745d38 100644 --- a/entity/kind_box_status.go +++ b/entity/kind_box_status.go @@ -6,7 +6,7 @@ const ( KindBoxPendingSendStatus KindBoxStatus = iota + 1 KindBoxSentStatus KindBoxPendingReceivedStatus - KindBoxRecievedStatus + KindBoxReceivedStatus KindBoxEnumeratedStatus ) @@ -14,7 +14,7 @@ const ( kindBoxPendingSendStatus = "pending-send" kindBoxSentStatus = "sent" kindBoxPendingReceivedStatus = "pending-received" - kindBoxRecievedStatus = "received" + kindBoxReceivedStatus = "received" kindBoxEnumeratedStatus = "enumerated" ) @@ -26,8 +26,8 @@ func (s KindBoxStatus) String() string { return kindBoxSentStatus case KindBoxPendingReceivedStatus: return kindBoxPendingReceivedStatus - case KindBoxRecievedStatus: - return kindBoxRecievedStatus + case KindBoxReceivedStatus: + return kindBoxReceivedStatus case KindBoxEnumeratedStatus: return kindBoxEnumeratedStatus } @@ -43,11 +43,63 @@ func MapToKindBoxStatus(statusStr string) KindBoxStatus { return KindBoxSentStatus case kindBoxPendingReceivedStatus: return KindBoxPendingReceivedStatus - case kindBoxRecievedStatus: - return KindBoxRecievedStatus + case kindBoxReceivedStatus: + return KindBoxReceivedStatus case kindBoxEnumeratedStatus: return KindBoxEnumeratedStatus } return KindBoxStatus(0) } + +package entity + +type KindBoxStatus uint + +const ( + KindBoxPendingSendStatus KindBoxStatus = iota + 1 + KindBoxSentStatus + KindBoxPendingReceivedStatus + KindBoxReceivedStatus + KindBoxEnumeratedStatus +) + +var kindBoxStatusStrings = map[KindBoxStatus]string{ + KindBoxPendingSendStatus: "pending-send", + KindBoxSentStatus: "sent", + KindBoxPendingReceivedStatus: "pending-received", + KindBoxReceivedStatus: "received", + KindBoxEnumeratedStatus: "enumerated", +} + +func (s KindBoxStatus) String() string { + return kindBoxStatusStrings[s] +} + +// AllKindBoxStatus returns a slice containing all string values of KindBoxStatus. +func AllKindBoxStatus() []string { + statusStrings := make([]string, len(kindBoxStatusStrings)) + for status, str := range kindBoxStatusStrings { + statusStrings[int(status)-1] = str + } + return statusStrings +} + +// MapToKindBoxStatus converts a string to the corresponding KindBoxStatus value. +func MapToKindBoxStatus(statusStr string) KindBoxStatus { + for status, str := range kindBoxStatusStrings { + if str == statusStr { + return status + } + } + return KindBoxStatus(0) +} + +// AllKindBoxStatus returns a slice containing all string values of KindBoxStatus. +func AllKindBoxStatus() []string { + statusStrings := make([]string, len(kindBoxStatusStrings)) + for status, str := range kindBoxStatusStrings { + statusStrings[int(status)-1] = str + } + return statusStrings +} \ No newline at end of file diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 934af0a..288b059 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -11,15 +11,6 @@ import ( func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { const op = "adminkindboxservice.Add" - // TODO: check validation - // exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID) - // if err != nil { - // return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - //} - //if !exist { - // return param.KindBoxAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid) - //} - kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ BenefactorID: req.BenefactorID, KindBoxReqID: req.KindBoxReqID, diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index 6294c80..b4f04ef 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -3,9 +3,7 @@ package adminkindboxservice import ( "context" - entity "git.gocasts.ir/ebhomengo/niki/entity" 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" ) @@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (pa // TODO: Does business domain need to delete an kindbox ? const op = "adminkindboxservice.Delete" - kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID) - if gErr != nil { - return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) - } - if kb.Status != entity.KindBoxPendingSendStatus { - return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } - if kb.BenefactorID != req.BenefactorID { - return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } - dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID) if dErr != nil { return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go index 45dbbf7..380ba3f 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -15,10 +15,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.Ki if err != nil { return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - // TODO : ref to service.Update() - if kindBox.BenefactorID != req.BenefactorID { - return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } return param.KindBoxGetResponse{KindBox: kindBox}, nil } diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index b806221..0838264 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -13,17 +13,8 @@ func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (pa // TODO: can benefactor update its Request ? // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected - const op = "adminkindboxservice.Update" - kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID) - if gErr != nil { - return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) - } - if kb.BenefactorID != req.BenefactorID { - return param.KindBoxUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } - kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{ TotalAmount: req.TotalAmount, ReceiverID: req.ReceiverID, diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index e82766d..bc44260 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -11,15 +11,6 @@ import ( func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { const op = "adminkindboxreqservice.Add" - // TODO: check validation - // exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID) - // if err != nil { - // return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - //} - // if !exist { - // return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid) - //} - kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ BenefactorID: req.BenefactorID, TypeID: req.TypeID, diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go index bfda06b..7c99b55 100644 --- a/service/admin/kind_box_req/delete.go +++ b/service/admin/kind_box_req/delete.go @@ -3,9 +3,7 @@ package adminkindboxreqservice import ( "context" - entity "git.gocasts.ir/ebhomengo/niki/entity" 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" ) @@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) // TODO: Does business domain need to delete an kindboxreq ? const op = "adminkindboxreqservice.Delete" - kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) - if gErr != nil { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) - } - if kbr.BenefactorID != req.BenefactorID { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } - if kbr.Status != entity.KindBoxReqPendingStatus { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } - dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) if dErr != nil { return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) diff --git a/service/admin/kind_box_req/get.go b/service/admin/kind_box_req/get.go index b197da1..c1d52b8 100644 --- a/service/admin/kind_box_req/get.go +++ b/service/admin/kind_box_req/get.go @@ -4,7 +4,6 @@ import ( "context" 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" ) @@ -16,9 +15,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param if err != nil { return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - if kindBoxReq.BenefactorID != req.BenefactorID { - return param.KindBoxReqGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil } diff --git a/service/benefactor/kind_box/get.go b/service/benefactor/kind_box/get.go index f4ee87e..e33ae93 100644 --- a/service/benefactor/kind_box/get.go +++ b/service/benefactor/kind_box/get.go @@ -15,10 +15,7 @@ func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.Ki if err != nil { return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - // TODO : ref to service.Update() - if kindBox.BenefactorID != req.BenefactorID { - return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } + return param.KindBoxGetResponse{KindBox: kindBox}, nil } diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go index c7873bd..a85ebdb 100644 --- a/service/benefactor/kind_box_req/delete.go +++ b/service/benefactor/kind_box_req/delete.go @@ -3,9 +3,7 @@ package userkindboxreqservice import ( "context" - entity "git.gocasts.ir/ebhomengo/niki/entity" 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" ) @@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) // TODO: Does business domain need to delete an kindboxreq ? const op = "userkindboxreqservice.Delete" - kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) - if gErr != nil { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) - } - if kbr.BenefactorID != req.BenefactorID { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } - if kbr.Status != entity.KindBoxReqPendingStatus { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } - dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) if dErr != nil { return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) diff --git a/service/benefactor/kind_box_req/get.go b/service/benefactor/kind_box_req/get.go index 8ad0d20..9053f84 100644 --- a/service/benefactor/kind_box_req/get.go +++ b/service/benefactor/kind_box_req/get.go @@ -4,7 +4,6 @@ import ( "context" 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" ) @@ -15,10 +14,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param if err != nil { return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - // TODO : ref to service.Update() - if kindBoxReq.BenefactorID != req.BenefactorID { - return param.KindBoxReqGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil } diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go index 595a5c4..e3cadbe 100644 --- a/service/benefactor/kind_box_req/update.go +++ b/service/benefactor/kind_box_req/update.go @@ -5,7 +5,6 @@ import ( entity "git.gocasts.ir/ebhomengo/niki/entity" 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" ) @@ -14,17 +13,6 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? const op = "userkindboxreqservice.Update" - kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) - if gErr != nil { - return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) - } - if kbr.BenefactorID != req.BenefactorID { - return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) - } - if kbr.Status != entity.KindBoxReqPendingStatus { - return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } - kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{ BenefactorID: req.BenefactorID, TypeID: req.TypeID, diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go index 07a1e4d..9b3909a 100644 --- a/validator/admin/kind_box/add.go +++ b/validator/admin/kind_box/add.go @@ -4,6 +4,7 @@ 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" + "github.com/go-ozzo/ozzo-validation/is" validation "github.com/go-ozzo/ozzo-validation/v4" ) @@ -11,18 +12,18 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str const op = "adminkindbox.KindBoxAddRequest" if err := validation.ValidateStruct(&req, - validation.Field(&req.SerialNumber, validation.Required), + validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.SenderID, validation.Required, - validation.By(v.doesEmployeeExist(&req.SenderID))), + validation.By(v.doesEmployeeExist(req.SenderID))), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/delete.go b/validator/admin/kind_box/delete.go index dbca318..8878b67 100644 --- a/validator/admin/kind_box/delete.go +++ b/validator/admin/kind_box/delete.go @@ -13,11 +13,13 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(&req.KindBoxID))), + validation.By(v.hasPendingStatus(req.KindBoxID)), + validation.By(v.doesKindBoxExist(req.KindBoxID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, 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 index 2019dd8..78ca1f9 100644 --- a/validator/admin/kind_box/get.go +++ b/validator/admin/kind_box/get.go @@ -17,6 +17,7 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri validation.Field(&req.KindBoxID, validation.Required, + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID)), validation.By(v.doesKindBoxExist(&req.KindBoxID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/update.go b/validator/admin/kind_box/update.go index 24a1aa6..80d7956 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -1,9 +1,11 @@ package adminkindbox import ( + "git.gocasts.ir/ebhomengo/niki/entity" 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" + "github.com/go-ozzo/ozzo-validation/is" validation "github.com/go-ozzo/ozzo-validation/v4" ) @@ -13,13 +15,14 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxID, validation.Required, + validation.By(v.doesKindBoxExist(req.BenefactorID)), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), - validation.Field(&req.SerialNumber, validation.EnsureString), + validation.Field(&req.SerialNumber, is.Alphanumeric), validation.Field(&req.TotalAmount, validation.Min(0)), @@ -30,7 +33,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st validation.By(v.doesEmployeeExist(req.ReceiverID))), validation.Field(&req.Status, - validation.By(v.hasCorrectStatus(req.Status.String))), + validation.In(entity.AllKindBoxStatus())), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 7d24b2e..de7d8d3 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -12,6 +12,7 @@ type Repository interface { BenefactorExist(id uint) (bool, error) KindBoxExist(id uint) (bool, error) KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) + PendingStatus(id uint) (bool, error) CheckStatus(status string) (bool, error) } @@ -83,3 +84,13 @@ func (v Validator) hasCorrectStatus(value interface{}) error { return nil } + +func (v Validator) hasPendingStatus(value interface{}) error { + kindboxID := value.(uint) + _, err := v.repo.PendingStatus(kindboxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +} diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index 9a171ce..d20ad22 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist)), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist)), + validation.By(v.doesTypeExist(req.TypeID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go index d0f2caa..b47a556 100644 --- a/validator/admin/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -17,7 +17,9 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), + validation.By(v.hasPendingStatus(req.KindBoxReqID)), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go index 0ce8b4c..debff13 100644 --- a/validator/admin/kind_box_req/get.go +++ b/validator/admin/kind_box_req/get.go @@ -17,7 +17,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index adce29f..baf6c34 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -19,7 +19,9 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxBelongToBenefactor(req.KindBoxReqID, req.BenefactorID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.hasPendingStatus(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.Field(&req.TypeID, validation.By(v.doesTypeExist(req.TypeID))), diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index dd00411..1729a15 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -16,6 +16,7 @@ type Repository interface { KindBoxRequestExist(id int) (bool, error) TypeExist(id int) (bool, error) KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) + PendingStatus(id uint) (bool, error) } type Validator struct { @@ -66,3 +67,13 @@ func (v Validator) doesTypeExist(value interface{}) error { return nil } + +func (v Validator) hasPendingStatus(value interface{}) error { + kindboxID := value.(uint) + _, err := v.repo.PendingStatus(kindboxID) + 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 index 1021bb4..35e9978 100644 --- a/validator/benefactor/kind_box/get.go +++ b/validator/benefactor/kind_box/get.go @@ -13,11 +13,12 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(&req.KindBoxID))), + validation.By(v.doesKindBoxExist(req.KindBoxID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box/get_all.go b/validator/benefactor/kind_box/get_all.go index 30225af..b49593e 100644 --- a/validator/benefactor/kind_box/get_all.go +++ b/validator/benefactor/kind_box/get_all.go @@ -13,7 +13,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box/validator.go b/validator/benefactor/kind_box/validator.go index 8aa06f5..6fede4f 100644 --- a/validator/benefactor/kind_box/validator.go +++ b/validator/benefactor/kind_box/validator.go @@ -7,8 +7,9 @@ import ( ) type Repository interface { - BenefactorExist(id int) (bool, error) - KindBoxExist(id int) (bool, error) + BenefactorExist(id uint) (bool, error) + KindBoxExist(id uint) (bool, error) + KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) } type Validator struct { @@ -20,7 +21,7 @@ func New(repo Repository) Validator { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(int) + benefactorID := value.(uint) _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -30,7 +31,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesKindBoxExist(value interface{}) error { - kindBoxID := value.(int) + kindBoxID := value.(uint) _, err := v.repo.KindBoxExist(kindBoxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -38,3 +39,14 @@ func (v Validator) doesKindBoxExist(value interface{}) error { 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 +} diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index c50f975..0e08c26 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist(&req.TypeID))), + validation.By(v.doesTypeExist(req.TypeID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go index 11f4e3a..bae2a01 100644 --- a/validator/benefactor/kind_box_req/delete.go +++ b/validator/benefactor/kind_box_req/delete.go @@ -13,11 +13,13 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.hasPendingStatus(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/get.go b/validator/benefactor/kind_box_req/get.go index 41cc13e..c8e2aa2 100644 --- a/validator/benefactor/kind_box_req/get.go +++ b/validator/benefactor/kind_box_req/get.go @@ -13,11 +13,12 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/get_all.go b/validator/benefactor/kind_box_req/get_all.go index d1e77cb..15de7ca 100644 --- a/validator/benefactor/kind_box_req/get_all.go +++ b/validator/benefactor/kind_box_req/get_all.go @@ -13,7 +13,7 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map if err := validation.ValidateStruct(&req, validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 5817909..603f5ec 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -15,15 +15,17 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map validation.Field(&req.BenefactorID, validation.Required, - validation.By(v.doesBenefactorExist(&req.BenefactorID))), + validation.By(v.doesBenefactorExist(req.BenefactorID))), validation.Field(&req.KindBoxReqID, validation.Required, - validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), + validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)), + validation.By(v.hasPendingStatus(req.KindBoxReqID)), + validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))), validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist(&req.TypeID))), + validation.By(v.doesTypeExist(req.TypeID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index ae73c3d..be7e6f1 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -12,9 +12,11 @@ const ( ) type Repository interface { - BenefactorExist(id int) (bool, error) - KindBoxReqExist(id int) (bool, error) - TypeExist(id int) (bool, error) + BenefactorExist(id uint) (bool, error) + KindBoxReqExist(id uint) (bool, error) + TypeExist(id uint) (bool, error) + KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error) + PendingStatus(id uint) (bool, error) } type Validator struct { @@ -26,7 +28,7 @@ func New(repo Repository) Validator { } func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID := value.(int) + benefactorID := value.(uint) _, err := v.repo.BenefactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -36,7 +38,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesTypeExist(value interface{}) error { - typeID := value.(int) + typeID := value.(uint) _, err := v.repo.TypeExist(typeID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -46,7 +48,7 @@ func (v Validator) doesTypeExist(value interface{}) error { } func (v Validator) doesKindBoxRequestExist(value interface{}) error { - kindBoxReqID := value.(int) + kindBoxReqID := value.(uint) _, err := v.repo.KindBoxReqExist(kindBoxReqID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -54,3 +56,24 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { 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) hasPendingStatus(value interface{}) error { + kindboxID := value.(uint) + _, err := v.repo.PendingStatus(kindboxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgNotFound) + } + + return nil +}