From 1a093a6d699f3df3d75e9e8fd9713ac21e565da1 Mon Sep 17 00:00:00 2001 From: imirazimi Date: Mon, 25 Dec 2023 20:38:19 +0330 Subject: [PATCH] fix(niki): refactor folder structure --- param/{ => admin}/kind_box/add.go | 4 +-- param/{ => admin}/kind_box/delete.go | 2 +- param/admin/kind_box/get.go | 11 ++++++ param/{ => admin}/kind_box/get_all.go | 4 +-- param/admin/kind_box/update.go | 15 ++++++++ param/admin/kind_box_req/add.go | 13 +++++++ param/{ => admin}/kind_box_req/delete.go | 2 +- param/admin/kind_box_req/get.go | 11 ++++++ param/admin/kind_box_req/get_all.go | 9 +++++ param/admin/kind_box_req/update.go | 14 ++++++++ param/{ => benefactor}/kind_box_req/add.go | 6 ++-- param/benefactor/kind_box_req/delete.go | 8 +++++ param/benefactor/kind_box_req/get.go | 12 +++++++ param/benefactor/kind_box_req/get_all.go | 11 ++++++ param/benefactor/kind_box_req/update.go | 14 ++++++++ param/kind_box/get_by_id.go | 11 ------ param/kind_box/update.go | 11 ------ param/kind_box_req/get_all.go | 9 ----- param/kind_box_req/get_by_id.go | 11 ------ param/kind_box_req/update.go | 11 ------ pkg/err_msg/message.go | 14 ++++++-- service/{ => admin}/kind_box/add.go | 6 ++-- service/{ => admin}/kind_box/delete.go | 4 +-- service/admin/kind_box/get.go | 17 +++++++++ service/{ => admin}/kind_box/get_all.go | 6 ++-- service/{ => admin}/kind_box/service.go | 6 ++-- service/{ => admin}/kind_box/update.go | 4 +-- service/admin/kind_box_req/add.go | 26 ++++++++++++++ service/admin/kind_box_req/delete.go | 21 +++++++++++ service/admin/kind_box_req/get.go | 21 +++++++++++ service/{ => admin}/kind_box_req/get_all.go | 10 ++++-- service/{ => admin}/kind_box_req/service.go | 9 ++--- service/admin/kind_box_req/update.go | 37 ++++++++++++++++++++ service/{ => benefactor}/kind_box_req/add.go | 10 +++--- service/benefactor/kind_box_req/delete.go | 32 +++++++++++++++++ service/benefactor/kind_box_req/get.go | 25 +++++++++++++ service/benefactor/kind_box_req/get_all.go | 21 +++++++++++ service/benefactor/kind_box_req/service.go | 26 ++++++++++++++ service/benefactor/kind_box_req/update.go | 35 ++++++++++++++++++ service/kind_box/get_by_id.go | 17 --------- service/kind_box_req/delete.go | 12 ------- service/kind_box_req/get_by_id.go | 17 --------- service/kind_box_req/update.go | 17 --------- 43 files changed, 430 insertions(+), 152 deletions(-) rename param/{ => admin}/kind_box/add.go (66%) rename param/{ => admin}/kind_box/delete.go (77%) create mode 100644 param/admin/kind_box/get.go rename param/{ => admin}/kind_box/get_all.go (57%) create mode 100644 param/admin/kind_box/update.go create mode 100644 param/admin/kind_box_req/add.go rename param/{ => admin}/kind_box_req/delete.go (77%) create mode 100644 param/admin/kind_box_req/get.go create mode 100644 param/admin/kind_box_req/get_all.go create mode 100644 param/admin/kind_box_req/update.go rename param/{ => benefactor}/kind_box_req/add.go (67%) create mode 100644 param/benefactor/kind_box_req/delete.go create mode 100644 param/benefactor/kind_box_req/get.go create mode 100644 param/benefactor/kind_box_req/get_all.go create mode 100644 param/benefactor/kind_box_req/update.go delete mode 100644 param/kind_box/get_by_id.go delete mode 100644 param/kind_box/update.go delete mode 100644 param/kind_box_req/get_all.go delete mode 100644 param/kind_box_req/get_by_id.go delete mode 100644 param/kind_box_req/update.go rename service/{ => admin}/kind_box/add.go (83%) rename service/{ => admin}/kind_box/delete.go (63%) create mode 100644 service/admin/kind_box/get.go rename service/{ => admin}/kind_box/get_all.go (75%) rename service/{ => admin}/kind_box/service.go (76%) rename service/{ => admin}/kind_box/update.go (67%) create mode 100644 service/admin/kind_box_req/add.go create mode 100644 service/admin/kind_box_req/delete.go create mode 100644 service/admin/kind_box_req/get.go rename service/{ => admin}/kind_box_req/get_all.go (69%) rename service/{ => admin}/kind_box_req/service.go (55%) create mode 100644 service/admin/kind_box_req/update.go rename service/{ => benefactor}/kind_box_req/add.go (82%) create mode 100644 service/benefactor/kind_box_req/delete.go create mode 100644 service/benefactor/kind_box_req/get.go create mode 100644 service/benefactor/kind_box_req/get_all.go create mode 100644 service/benefactor/kind_box_req/service.go create mode 100644 service/benefactor/kind_box_req/update.go delete mode 100644 service/kind_box/get_by_id.go delete mode 100644 service/kind_box_req/delete.go delete mode 100644 service/kind_box_req/get_by_id.go delete mode 100644 service/kind_box_req/update.go diff --git a/param/kind_box/add.go b/param/admin/kind_box/add.go similarity index 66% rename from param/kind_box/add.go rename to param/admin/kind_box/add.go index f095af5..8e6de04 100644 --- a/param/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -1,6 +1,6 @@ -package param +package adminkindboxparam -import "git.gocasts.ir/ebhomengo/niki/entity" +import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxAddRequest struct { KindBoxReqID uint diff --git a/param/kind_box/delete.go b/param/admin/kind_box/delete.go similarity index 77% rename from param/kind_box/delete.go rename to param/admin/kind_box/delete.go index 82e56c9..b60be34 100644 --- a/param/kind_box/delete.go +++ b/param/admin/kind_box/delete.go @@ -1,4 +1,4 @@ -package param +package adminkindboxparam type KindBoxDeleteRequest struct { KindBoxID uint diff --git a/param/admin/kind_box/get.go b/param/admin/kind_box/get.go new file mode 100644 index 0000000..eca9058 --- /dev/null +++ b/param/admin/kind_box/get.go @@ -0,0 +1,11 @@ +package adminkindboxparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxGetRequest struct { + KindBoxID uint +} + +type KindBoxGetResponse struct { + entity.KindBox +} diff --git a/param/kind_box/get_all.go b/param/admin/kind_box/get_all.go similarity index 57% rename from param/kind_box/get_all.go rename to param/admin/kind_box/get_all.go index e200ef0..bd4f255 100644 --- a/param/kind_box/get_all.go +++ b/param/admin/kind_box/get_all.go @@ -1,6 +1,6 @@ -package param +package adminkindboxparam -import "git.gocasts.ir/ebhomengo/niki/entity" +import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxGetAllRequest struct{} diff --git a/param/admin/kind_box/update.go b/param/admin/kind_box/update.go new file mode 100644 index 0000000..b2358ce --- /dev/null +++ b/param/admin/kind_box/update.go @@ -0,0 +1,15 @@ +package adminkindboxparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxUpdateRequest struct { + TotalAmount uint + ReceiverID uint + SenderID uint + SerialNumber string + Status KindBoxStatus +} + +type KindBoxUpdateResponse struct { + KindBox entity.KindBox +} diff --git a/param/admin/kind_box_req/add.go b/param/admin/kind_box_req/add.go new file mode 100644 index 0000000..01b5f73 --- /dev/null +++ b/param/admin/kind_box_req/add.go @@ -0,0 +1,13 @@ +package adminkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqAddRequest struct { + BenefactorID uint + TypeID uint + CountRequested uint +} + +type KindBoxReqAddResponse struct { + KindBoxReq entity.KindBoxReq +} diff --git a/param/kind_box_req/delete.go b/param/admin/kind_box_req/delete.go similarity index 77% rename from param/kind_box_req/delete.go rename to param/admin/kind_box_req/delete.go index a2c7b30..1126e74 100644 --- a/param/kind_box_req/delete.go +++ b/param/admin/kind_box_req/delete.go @@ -1,4 +1,4 @@ -package param +package adminkindboxreqparam type KindBoxReqDeleteRequest struct { KindBoxReqID uint diff --git a/param/admin/kind_box_req/get.go b/param/admin/kind_box_req/get.go new file mode 100644 index 0000000..2fdc4e6 --- /dev/null +++ b/param/admin/kind_box_req/get.go @@ -0,0 +1,11 @@ +package adminkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqGetRequest struct { + KindBoxReqID uint +} + +type KindBoxReqGetResponse struct { + entity.KindBoxReq +} diff --git a/param/admin/kind_box_req/get_all.go b/param/admin/kind_box_req/get_all.go new file mode 100644 index 0000000..98f3eb8 --- /dev/null +++ b/param/admin/kind_box_req/get_all.go @@ -0,0 +1,9 @@ +package adminkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqGetAllRequest struct {} + +type KindBoxReqGetAllResponse struct { + AllKindBoxReq []entity.KindBoxReq +} diff --git a/param/admin/kind_box_req/update.go b/param/admin/kind_box_req/update.go new file mode 100644 index 0000000..fc17f22 --- /dev/null +++ b/param/admin/kind_box_req/update.go @@ -0,0 +1,14 @@ +package adminkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqUpdateRequest struct { + BenefactorID uint + KindBoxReqID uint + TypeID uint + CountRequested uint +} + +type KindBoxReqUpdateResponse struct { + KindBoxReq entity.KindBoxReq +} diff --git a/param/kind_box_req/add.go b/param/benefactor/kind_box_req/add.go similarity index 67% rename from param/kind_box_req/add.go rename to param/benefactor/kind_box_req/add.go index abc4024..1e5b85a 100644 --- a/param/kind_box_req/add.go +++ b/param/benefactor/kind_box_req/add.go @@ -1,11 +1,11 @@ -package param +package userkindboxreqparam -import "git.gocasts.ir/ebhomengo/niki/entity" +import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxReqAddRequest struct { - CountRequested uint BenefactorID uint TypeID uint + CountRequested uint } type KindBoxReqAddResponse struct { diff --git a/param/benefactor/kind_box_req/delete.go b/param/benefactor/kind_box_req/delete.go new file mode 100644 index 0000000..51eabfc --- /dev/null +++ b/param/benefactor/kind_box_req/delete.go @@ -0,0 +1,8 @@ +package userkindboxreqparam + +type KindBoxReqDeleteRequest struct { + BenfactorID uint + KindBoxReqID uint +} + +type KindBoxReqDeleteResponse struct{} diff --git a/param/benefactor/kind_box_req/get.go b/param/benefactor/kind_box_req/get.go new file mode 100644 index 0000000..3feb856 --- /dev/null +++ b/param/benefactor/kind_box_req/get.go @@ -0,0 +1,12 @@ +package userkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqGetRequest struct { + BenefactorID uint + KindBoxReqID uint +} + +type KindBoxReqGetResponse struct { + entity.KindBoxReq +} diff --git a/param/benefactor/kind_box_req/get_all.go b/param/benefactor/kind_box_req/get_all.go new file mode 100644 index 0000000..be4b653 --- /dev/null +++ b/param/benefactor/kind_box_req/get_all.go @@ -0,0 +1,11 @@ +package userkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqGetAllRequest struct { + BenefactorID uint +} + +type KindBoxReqGetAllResponse struct { + AllKindBoxReq []entity.KindBoxReq +} diff --git a/param/benefactor/kind_box_req/update.go b/param/benefactor/kind_box_req/update.go new file mode 100644 index 0000000..d587a56 --- /dev/null +++ b/param/benefactor/kind_box_req/update.go @@ -0,0 +1,14 @@ +package userkindboxreqparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type KindBoxReqUpdateRequest struct { + BenefactorID uint + KindBoxReqID uint + TypeID uint + CountRequested uint +} + +type KindBoxReqUpdateResponse struct { + KindBoxReq entity.KindBoxReq +} diff --git a/param/kind_box/get_by_id.go b/param/kind_box/get_by_id.go deleted file mode 100644 index 98da018..0000000 --- a/param/kind_box/get_by_id.go +++ /dev/null @@ -1,11 +0,0 @@ -package param - -import "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxGetByIDRequest struct { - KindBoxID uint -} - -type KindBoxGetByIDResponse struct { - entity.KindBox -} diff --git a/param/kind_box/update.go b/param/kind_box/update.go deleted file mode 100644 index 945adfa..0000000 --- a/param/kind_box/update.go +++ /dev/null @@ -1,11 +0,0 @@ -package param - -import "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxUpdateRequest struct { - KindBox entity.KindBox -} - -type KindBoxUpdateResponse struct { - KindBox entity.KindBox -} diff --git a/param/kind_box_req/get_all.go b/param/kind_box_req/get_all.go deleted file mode 100644 index c29bbe7..0000000 --- a/param/kind_box_req/get_all.go +++ /dev/null @@ -1,9 +0,0 @@ -package param - -import "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxReqGetAllRequest struct{} - -type KindBoxReqGetAllResponse struct { - AllKindBoxReq []entity.KindBoxReq -} diff --git a/param/kind_box_req/get_by_id.go b/param/kind_box_req/get_by_id.go deleted file mode 100644 index 8b9d19f..0000000 --- a/param/kind_box_req/get_by_id.go +++ /dev/null @@ -1,11 +0,0 @@ -package param - -import "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxReqGetByIDRequest struct { - KindBoxReqID uint -} - -type KindBoxReqGetByIDResponse struct { - entity.KindBoxReq -} diff --git a/param/kind_box_req/update.go b/param/kind_box_req/update.go deleted file mode 100644 index 124569e..0000000 --- a/param/kind_box_req/update.go +++ /dev/null @@ -1,11 +0,0 @@ -package param - -import "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxReqUpdateRequest struct { - KindBoxReq entity.KindBoxReq -} - -type KindBoxReqUpdateResponse struct { - KindBoxReq entity.KindBoxReq -} diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 6041a9b..20a4e62 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -1,7 +1,15 @@ package errmsg const ( - ErrorMsgNotFound = "record not found" - ErrorMsgSomethingWentWrong = "something went wrong" - ErrorMsgInvalidInput = "invalid input" + ErrorMsgNotFound = "record not found" + ErrorMsgSomethingWentWrong = "something went wrong" + ErrorMsgInvalidInput = "invalid input" + ErrorMsgInvalidStatus = "invalid status" + ErrorMsgPhoneNumberIsNotUnique = "phone number is not unique" + ErrorMsgPhoneNumberIsNotValid = "phone number is not valid" + ErrorMsgUserNotAllowed = "user not allowed" ) + +// const ( +// ErrorMsgCantScanQueryResult = "can't scan query result" +// ) diff --git a/service/kind_box/add.go b/service/admin/kind_box/add.go similarity index 83% rename from service/kind_box/add.go rename to service/admin/kind_box/add.go index 36c2836..f4824d7 100644 --- a/service/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -1,15 +1,15 @@ -package kindboxservice +package adminkindboxservice import ( "context" entity "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { - const op = "kindboxservice.Add" + const op = "adminkindboxservice.Add" kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ KindBoxReqID: req.KindBoxReqID, SenderID: req.SenderID, diff --git a/service/kind_box/delete.go b/service/admin/kind_box/delete.go similarity index 63% rename from service/kind_box/delete.go rename to service/admin/kind_box/delete.go index 3f03290..a38e1a8 100644 --- a/service/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -1,9 +1,9 @@ -package kindboxservice +package adminkindboxservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" ) func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) error { diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go new file mode 100644 index 0000000..75eb606 --- /dev/null +++ b/service/admin/kind_box/get.go @@ -0,0 +1,17 @@ +package adminkindboxservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + 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) + } + return param.KindBoxGetResponse{KindBox: kindBox}, nil +} diff --git a/service/kind_box/get_all.go b/service/admin/kind_box/get_all.go similarity index 75% rename from service/kind_box/get_all.go rename to service/admin/kind_box/get_all.go index e24a816..697cc79 100644 --- a/service/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -1,14 +1,14 @@ -package kindboxservice +package adminkindboxservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) GetAll(ctx context.Context) (param.KindBoxGetAllResponse, error) { - const op = "kindboxservice.GetAll" + const op = "adminkindboxservice.GetAll" allKindBox, err := s.repo.GetAllKindBox(ctx) if err != nil { return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) diff --git a/service/kind_box/service.go b/service/admin/kind_box/service.go similarity index 76% rename from service/kind_box/service.go rename to service/admin/kind_box/service.go index 6addb7a..6522375 100644 --- a/service/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -1,9 +1,9 @@ -package kindboxservice +package adminkindboxservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" + entity "git.gocasts.ir/ebhomengo/niki/entity" ) type Repository interface { @@ -11,7 +11,7 @@ type Repository interface { EditKindBox(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) - GetKindBoxByID(ctx context.Context, kindBox uint) (entity.KindBox, error) + GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) } type Service struct { diff --git a/service/kind_box/update.go b/service/admin/kind_box/update.go similarity index 67% rename from service/kind_box/update.go rename to service/admin/kind_box/update.go index e8f7505..f682ae6 100644 --- a/service/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -1,9 +1,9 @@ -package kindboxservice +package adminkindboxservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" ) func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go new file mode 100644 index 0000000..e3baf1c --- /dev/null +++ b/service/admin/kind_box_req/add.go @@ -0,0 +1,26 @@ +package adminkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { + + const op = "adminkindboxreqservice.Add" + + kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ + BenefactorID: req.BenefactorID, + TypeID: req.TypeID, + CountRequested: req.CountRequested, + Status: entity.KindBoxReqPendingStatus, + }) + if err != nil { + return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqAddResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go new file mode 100644 index 0000000..26891be --- /dev/null +++ b/service/admin/kind_box_req/delete.go @@ -0,0 +1,21 @@ +package adminkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { + //TODO: Does bussiness domain need to delete an kindboxreq ? + const op = "adminkindboxreqservice.Delete" + + dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) + if dErr != nil { + return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqDeleteResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/admin/kind_box_req/get.go b/service/admin/kind_box_req/get.go new file mode 100644 index 0000000..4cb5cc4 --- /dev/null +++ b/service/admin/kind_box_req/get.go @@ -0,0 +1,21 @@ +package adminkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { + + const op = "adminkindboxreqservice.Get" + + kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) + if err != nil { + return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go similarity index 69% rename from service/kind_box_req/get_all.go rename to service/admin/kind_box_req/get_all.go index 3552357..1ca290b 100644 --- a/service/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -1,17 +1,21 @@ -package kindboxreqservice +package adminkindboxreqservice import ( "context" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) +// TODO: Pagination, Filters, Sort func (s Service) GetAll(ctx context.Context) (param.KindBoxReqGetAllResponse, error) { - const op = "kindboxreqservice.GetAll" + + const op = "adminkindboxreqservice.GetAll" + allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) if err != nil { return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } + return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil } diff --git a/service/kind_box_req/service.go b/service/admin/kind_box_req/service.go similarity index 55% rename from service/kind_box_req/service.go rename to service/admin/kind_box_req/service.go index 8a1fd34..ab60a90 100644 --- a/service/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -1,17 +1,18 @@ -package kindboxreqservice +package adminkindboxreqservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" + entity "git.gocasts.ir/ebhomengo/niki/entity" ) type Repository interface { AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) - EditKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReqInput entity.KindBoxReq) (entity.KindBoxReq, error) + UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + // TODO: can benefactor cancel its request ? DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) - GetKindBoxReqByID(ctx context.Context, kindBoxReq uint) (entity.KindBoxReq, error) + GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) } type Service struct { diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go new file mode 100644 index 0000000..c530ab8 --- /dev/null +++ b/service/admin/kind_box_req/update.go @@ -0,0 +1,37 @@ +package adminkindboxreqservice + +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" +) + +func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { + // 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 = "adminkindboxreqservice.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.Status != 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, + CountRequested: req.CountRequested, + }) + if uErr != nil { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/kind_box_req/add.go b/service/benefactor/kind_box_req/add.go similarity index 82% rename from service/kind_box_req/add.go rename to service/benefactor/kind_box_req/add.go index 0825b2d..adf424c 100644 --- a/service/kind_box_req/add.go +++ b/service/benefactor/kind_box_req/add.go @@ -1,23 +1,25 @@ -package kindboxreqservice +package userkindboxreqservice import ( "context" entity "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { - const op = "kindboxreqservice.Add" + const op = "userkindboxreqservice.Add" + kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ + BenefactorID: req.BenefactorID, TypeID: req.TypeID, CountRequested: req.CountRequested, - BenefactorID: req.BenefactorID, Status: entity.KindBoxReqPendingStatus, }) if err != nil { return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } + return param.KindBoxReqAddResponse{KindBoxReq: kindBoxReq}, nil } diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go new file mode 100644 index 0000000..37c4a33 --- /dev/null +++ b/service/benefactor/kind_box_req/delete.go @@ -0,0 +1,32 @@ +package userkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { + //TODO: Does bussiness domain need to delete an kindboxreq ? + const op = "userkindboxreqservice.Delete" + + kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) + if gErr != nil { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected) + } + if kbr.Status != KindBoxReqPendingStatus { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid) + } + if kbr.BenefactorID != req.BenefactorID { + return param.KindBoxReqGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden) + } + + dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) + if dErr != nil { + return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqDeleteResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/benefactor/kind_box_req/get.go b/service/benefactor/kind_box_req/get.go new file mode 100644 index 0000000..bb3ee22 --- /dev/null +++ b/service/benefactor/kind_box_req/get.go @@ -0,0 +1,25 @@ +package userkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { + + const op = "userkindboxreqservice.Get" + + kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) + 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/get_all.go b/service/benefactor/kind_box_req/get_all.go new file mode 100644 index 0000000..4d3074e --- /dev/null +++ b/service/benefactor/kind_box_req/get_all.go @@ -0,0 +1,21 @@ +package userkindboxreqservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +// TODO: Pagination, Filters, Sort +func (s Service) GetAll(ctx context.Context) (param.KindBoxReqGetAllResponse, error) { + + const op = "userkindboxreqservice.GetAll" + + allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx, req.BenefactorID) + if err != nil { + return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil +} diff --git a/service/benefactor/kind_box_req/service.go b/service/benefactor/kind_box_req/service.go new file mode 100644 index 0000000..f552c9d --- /dev/null +++ b/service/benefactor/kind_box_req/service.go @@ -0,0 +1,26 @@ +package userkindboxreqservice + +import ( + "context" + + entity "git.gocasts.ir/ebhomengo/niki/entity" +) + +type Repository interface { + AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + // TODO: can benefactor cancel its request ? + DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error + GetAllKindBoxReq(ctx context.Context, benefactorID uint) ([]entity.KindBoxReq, error) + GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) +} + +type Service struct { + repo Repository +} + +func New(repository Repository) Service { + return Service{ + repo: repository, + } +} diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go new file mode 100644 index 0000000..da455c7 --- /dev/null +++ b/service/benefactor/kind_box_req/update.go @@ -0,0 +1,35 @@ +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" +) + +func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { + // TODO: can benefactor update its request ? + // 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.Status != 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, + CountRequested: req.CountRequested, + }) + if uErr != nil { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil +} diff --git a/service/kind_box/get_by_id.go b/service/kind_box/get_by_id.go deleted file mode 100644 index e069534..0000000 --- a/service/kind_box/get_by_id.go +++ /dev/null @@ -1,17 +0,0 @@ -package kindboxservice - -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) GetByID(ctx context.Context, req param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) { - const op = "kindboxservice.GetByID" - kindBox, err := s.repo.GetKindBoxByID(ctx, req.KindBoxID) - if err != nil { - return param.KindBoxGetByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - return param.KindBoxGetByIDResponse{KindBox: kindBox}, nil -} diff --git a/service/kind_box_req/delete.go b/service/kind_box_req/delete.go deleted file mode 100644 index a4cec7d..0000000 --- a/service/kind_box_req/delete.go +++ /dev/null @@ -1,12 +0,0 @@ -package kindboxreqservice - -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" -) - -func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) error { - // some code - panic("not implemented") -} diff --git a/service/kind_box_req/get_by_id.go b/service/kind_box_req/get_by_id.go deleted file mode 100644 index a038d27..0000000 --- a/service/kind_box_req/get_by_id.go +++ /dev/null @@ -1,17 +0,0 @@ -package kindboxreqservice - -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) GetByID(ctx context.Context, req param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) { - const op = "kindboxreqservice.GetByID" - kindBoxReq, err := s.repo.GetKindBoxReqByID(ctx, req.KindBoxReqID) - if err != nil { - return param.KindBoxReqGetByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - return param.KindBoxReqGetByIDResponse{KindBoxReq: kindBoxReq}, nil -} diff --git a/service/kind_box_req/update.go b/service/kind_box_req/update.go deleted file mode 100644 index c47d22f..0000000 --- a/service/kind_box_req/update.go +++ /dev/null @@ -1,17 +0,0 @@ -package kindboxreqservice - -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" -) - -func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { - // const op = "kindboxreqservice.Update" - // kindBoxReq, err := s.repo.EditKindBoxReq(req.KindBoxReq.ID, req.KindBoxReq) - // if err != nil { - // return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - // } - // return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil - panic("not implemented") -}