From d49f1277cc9e050c33f373294046abd585d2063c Mon Sep 17 00:00:00 2001 From: imirazimi Date: Wed, 27 Dec 2023 01:12:33 +0330 Subject: [PATCH] fix(niki): complete admin kind_box service --- param/admin/kind_box/add.go | 1 + param/admin/kind_box/delete.go | 3 ++- param/admin/kind_box/get.go | 3 ++- param/admin/kind_box/update.go | 2 ++ service/admin/kind_box/add.go | 11 ++++++++ service/admin/kind_box/delete.go | 27 +++++++++++++++++--- service/admin/kind_box/get.go | 9 ++++++- service/admin/kind_box/service.go | 7 ++++- service/admin/kind_box/update.go | 31 +++++++++++++++++++++-- service/admin/kind_box_req/add.go | 15 +++++------ service/admin/kind_box_req/delete.go | 6 ++--- service/admin/kind_box_req/update.go | 3 +++ service/benefactor/kind_box_req/delete.go | 6 ++--- service/benefactor/kind_box_req/update.go | 3 +++ 14 files changed, 104 insertions(+), 23 deletions(-) diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 8e6de04..d1a2705 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -3,6 +3,7 @@ package adminkindboxparam import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxAddRequest struct { + BenefactorID uint KindBoxReqID uint SenderID uint SerialNumber string diff --git a/param/admin/kind_box/delete.go b/param/admin/kind_box/delete.go index b60be34..72c4c03 100644 --- a/param/admin/kind_box/delete.go +++ b/param/admin/kind_box/delete.go @@ -1,7 +1,8 @@ package adminkindboxparam type KindBoxDeleteRequest struct { - KindBoxID uint + BenefactorID uint + KindBoxID uint } type KindBoxDeleteResponse struct{} diff --git a/param/admin/kind_box/get.go b/param/admin/kind_box/get.go index eca9058..3893ad0 100644 --- a/param/admin/kind_box/get.go +++ b/param/admin/kind_box/get.go @@ -3,7 +3,8 @@ package adminkindboxparam import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxGetRequest struct { - KindBoxID uint + BenefactorID uint + KindBoxID uint } type KindBoxGetResponse struct { diff --git a/param/admin/kind_box/update.go b/param/admin/kind_box/update.go index ea69a25..f78fdf5 100644 --- a/param/admin/kind_box/update.go +++ b/param/admin/kind_box/update.go @@ -3,6 +3,8 @@ package adminkindboxparam import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxUpdateRequest struct { + BenefactorID uint + KindBoxID uint TotalAmount uint ReceiverID uint SenderID uint diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index f4824d7..934af0a 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -10,7 +10,18 @@ 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, SenderID: req.SenderID, SerialNumber: req.SerialNumber, diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index a38e1a8..6294c80 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -3,10 +3,31 @@ 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" ) -func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) error { - // some code - panic("not implemented") +func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { + // 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) + } + + return param.KindBoxDeleteResponse{}, nil } diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go index 75eb606..45dbbf7 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -3,15 +3,22 @@ package adminkindboxservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/admin/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" ) func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { const op = "adminkindboxservice.Get" + kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) 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/service.go b/service/admin/kind_box/service.go index 6522375..842a171 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -8,7 +8,7 @@ import ( type Repository interface { AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) - EditKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) + UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) DeleteKindBox(ctx context.Context, kindBoxID uint) error GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) @@ -18,6 +18,11 @@ type Service struct { repo Repository } +// TODO: check validation. +// type BenefactorService interface { +// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error) +// } + func New(repository Repository) Service { return Service{ repo: repository, diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index f682ae6..b806221 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -3,10 +3,37 @@ 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" ) func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { - // some code - panic("not implement") + // 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, + SenderID: req.SenderID, + SerialNumber: req.SerialNumber, + Status: req.Status, + }) + if uErr != nil { + return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxUpdateResponse{KindBox: kindBox}, nil } diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index d16ad77..e82766d 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -5,7 +5,6 @@ import ( 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,13 +12,13 @@ func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param const op = "adminkindboxreqservice.Add" // TODO: check validation - kbr, err := s.repo.GetKindBoxReq(ctx, req.BenefactorID) - if err != nil { - return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - if kbr.BenefactorID != req.BenefactorID { - return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid) - } + // 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, diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go index d50a06f..bfda06b 100644 --- a/service/admin/kind_box_req/delete.go +++ b/service/admin/kind_box_req/delete.go @@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) if gErr != nil { return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) } - if kbr.Status != entity.KindBoxReqPendingStatus { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } 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 { diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go index fc53bd3..d3bf686 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -20,6 +20,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) 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) } diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go index c8bf1a6..c7873bd 100644 --- a/service/benefactor/kind_box_req/delete.go +++ b/service/benefactor/kind_box_req/delete.go @@ -17,12 +17,12 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) if gErr != nil { return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) } - if kbr.Status != entity.KindBoxReqPendingStatus { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) - } 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 { diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go index b5dd9f2..595a5c4 100644 --- a/service/benefactor/kind_box_req/update.go +++ b/service/benefactor/kind_box_req/update.go @@ -18,6 +18,9 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) 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) }