From 7d76ea2d5094c1d8be96d010df2943efc395b43c Mon Sep 17 00:00:00 2001 From: imirazimi Date: Sat, 23 Dec 2023 00:55:16 +0330 Subject: [PATCH] feat(service): complete add | getByID | getAll for kind_box and kind_box_req services --- entity/kind_box.go | 5 +-- entity/kind_box_req.go | 2 +- entity/kind_box_req_status.go | 41 ++++++++++++++++++++++ entity/kind_box_status.go | 53 +++++++++++++++++++++++++++++ param/kind_box/add.go | 4 ++- param/kind_box_req/add.go | 4 ++- service/kind_box/add.go | 17 +++++++-- service/kind_box/get_all.go | 13 +++++-- service/kind_box/get_by_id.go | 15 +++++--- service/kind_box_req/add.go | 17 +++++++-- service/kind_box_req/get_all.go | 13 +++++-- service/kind_box_req/get_by_id.go | 15 +++++--- service/kind_box_req/update.go | 12 +++++-- validator/kind_box/get_by_id.go | 4 +-- validator/kind_box/update.go | 2 +- validator/kind_box/validator.go | 8 ++--- validator/kind_box_req/add.go | 6 ++-- validator/kind_box_req/get_by_id.go | 4 +-- validator/kind_box_req/validator.go | 8 ++--- 19 files changed, 199 insertions(+), 44 deletions(-) create mode 100644 entity/kind_box_req_status.go create mode 100644 entity/kind_box_status.go diff --git a/entity/kind_box.go b/entity/kind_box.go index 2e27448..88135dd 100644 --- a/entity/kind_box.go +++ b/entity/kind_box.go @@ -4,10 +4,11 @@ import "time" type KindBox struct { ID uint + KindBoxReqID uint TotalAmount uint - ReceiverId uint + ReceiverID uint SenderID uint SerialNumber string - Status uint + Status KindBoxStatus StatusChangedAt *time.Time } diff --git a/entity/kind_box_req.go b/entity/kind_box_req.go index 3a901ed..dfdd93a 100644 --- a/entity/kind_box_req.go +++ b/entity/kind_box_req.go @@ -8,7 +8,7 @@ type KindBoxReq struct { CountRequested uint CountAccepted uint BenefactorID uint - Status uint + Status KindBoxReqStatus Description string StatusChangedAt *time.Time } diff --git a/entity/kind_box_req_status.go b/entity/kind_box_req_status.go new file mode 100644 index 0000000..a6cc795 --- /dev/null +++ b/entity/kind_box_req_status.go @@ -0,0 +1,41 @@ +package entity + +type KindBoxReqStatus uint + +const ( + KindBoxReqPendingStatus KindBoxReqStatus = iota + 1 + KindBoxReqAcceptedStatus + KindBoxReqRejectedStatus +) + +const ( + kindBoxReqPendingStatusStr = "pending" + kindBoxReqAcceptedStatusStr = "accepted" + kindBoxReqRejectedStatusStr = "rejected" +) + +func (s KindBoxReqStatus) String() string { + switch s { + case KindBoxReqPendingStatus: + return kindBoxReqPendingStatusStr + case KindBoxReqAcceptedStatus: + return kindBoxReqAcceptedStatusStr + case KindBoxReqRejectedStatus: + return kindBoxReqRejectedStatusStr + } + + return "" +} + +func MapToKindBoxReqStatus(statusStr string) KindBoxReqStatus { + switch statusStr { + case kindBoxReqPendingStatusStr: + return KindBoxReqPendingStatus + case kindBoxReqAcceptedStatusStr: + return KindBoxReqAcceptedStatus + case kindBoxReqRejectedStatusStr: + return KindBoxReqRejectedStatus + } + + return KindBoxReqStatus(0) +} diff --git a/entity/kind_box_status.go b/entity/kind_box_status.go new file mode 100644 index 0000000..ab3d0a2 --- /dev/null +++ b/entity/kind_box_status.go @@ -0,0 +1,53 @@ +package entity + +type KindBoxStatus uint + +const ( + KindBoxPendingSendStatus KindBoxStatus = iota + 1 + KindBoxSentStatus + KindBoxPendingReceivedStatus + KindBoxRecievedStatus + KindBoxEnumeratedStatus +) + +const ( + kindBoxPendingSendStatus = "pending-send" + kindBoxSentStatus = "sent" + kindBoxPendingReceivedStatus = "pending-received" + kindBoxRecievedStatus = "received" + kindBoxEnumeratedStatus = "enumerated" +) + +func (s KindBoxStatus) String() string { + switch s { + case KindBoxPendingSendStatus: + return kindBoxPendingSendStatus + case KindBoxSentStatus: + return kindBoxSentStatus + case KindBoxPendingReceivedStatus: + return kindBoxPendingReceivedStatus + case KindBoxRecievedStatus: + return kindBoxRecievedStatus + case KindBoxEnumeratedStatus: + return kindBoxEnumeratedStatus + } + + return "" +} + +func MapToKindBoxStatus(statusStr string) KindBoxStatus { + switch statusStr { + case kindBoxPendingSendStatus: + return KindBoxPendingSendStatus + case kindBoxSentStatus: + return KindBoxSentStatus + case kindBoxPendingReceivedStatus: + return KindBoxPendingReceivedStatus + case kindBoxRecievedStatus: + return KindBoxRecievedStatus + case kindBoxEnumeratedStatus: + return KindBoxEnumeratedStatus + } + + return KindBoxStatus(0) +} diff --git a/param/kind_box/add.go b/param/kind_box/add.go index c636d5d..f095af5 100644 --- a/param/kind_box/add.go +++ b/param/kind_box/add.go @@ -3,7 +3,9 @@ package param import "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxAddRequest struct { - KindBox entity.KindBox + KindBoxReqID uint + SenderID uint + SerialNumber string } type KindBoxAddResponse struct { diff --git a/param/kind_box_req/add.go b/param/kind_box_req/add.go index bdaeb93..abc4024 100644 --- a/param/kind_box_req/add.go +++ b/param/kind_box_req/add.go @@ -3,7 +3,9 @@ package param import "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxReqAddRequest struct { - KindBoxReq entity.KindBoxReq + CountRequested uint + BenefactorID uint + TypeID uint } type KindBoxReqAddResponse struct { diff --git a/service/kind_box/add.go b/service/kind_box/add.go index 0aaf045..82a6582 100644 --- a/service/kind_box/add.go +++ b/service/kind_box/add.go @@ -1,10 +1,21 @@ package kindboxservice import ( + entity "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (s Service) Add(newKindBoxReq param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { - // some code - panic("not implemented") +func (s Service) Add(req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { + const op = "kindboxservice.Add" + kindBox, err := s.repo.AddKindBox(entity.KindBox{ + KindBoxReqID: req.KindBoxReqID, + SenderID: req.SenderID, + SerialNumber: req.SerialNumber, + Status: entity.KindBoxPendingSendStatus, + }) + if err != nil { + return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + return param.KindBoxAddResponse{KindBox: kindBox}, nil } diff --git a/service/kind_box/get_all.go b/service/kind_box/get_all.go index 72a8cda..a1f10f7 100644 --- a/service/kind_box/get_all.go +++ b/service/kind_box/get_all.go @@ -1,8 +1,15 @@ package kindboxservice -import param "git.gocasts.ir/ebhomengo/niki/param/kind_box" +import ( + param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) func (s Service) GetAll() (param.KindBoxGetAllResponse, error) { - // some code - panic("not implement") + const op = "kindboxservice.GetAll" + allKindBox, err := s.repo.GetAllKindBox() + if err != nil { + return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil } diff --git a/service/kind_box/get_by_id.go b/service/kind_box/get_by_id.go index 07582af..5d68dd7 100644 --- a/service/kind_box/get_by_id.go +++ b/service/kind_box/get_by_id.go @@ -1,8 +1,15 @@ package kindboxservice -import param "git.gocasts.ir/ebhomengo/niki/param/kind_box" +import ( + param "git.gocasts.ir/ebhomengo/niki/param/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) -func (s Service) GetByID(request param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) { - // some code - panic("not implement") +func (s Service) GetByID(req param.KindBoxGetByIDRequest) (param.KindBoxGetByIDResponse, error) { + const op = "kindboxservice.GetByID" + kindBox, err := s.repo.GetKindBoxByID(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/add.go b/service/kind_box_req/add.go index 5f29a56..0b715a0 100644 --- a/service/kind_box_req/add.go +++ b/service/kind_box_req/add.go @@ -1,10 +1,21 @@ package kindboxreqservice import ( + entity "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (s Service) Add(newKindBoxReq param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { - // some code - panic("not implemented") +func (s Service) Add(req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { + const op = "kindboxreqservice.Add" + kindBoxReq, err := s.repo.AddKindBoxReq(entity.KindBoxReq{ + 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/kind_box_req/get_all.go b/service/kind_box_req/get_all.go index adaac16..5cf8106 100644 --- a/service/kind_box_req/get_all.go +++ b/service/kind_box_req/get_all.go @@ -1,8 +1,15 @@ package kindboxreqservice -import param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" +import ( + param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) func (s Service) GetAll() (param.KindBoxReqGetAllResponse, error) { - // some code - panic("not implement") + const op = "kindboxreqservice.GetAll" + allKindBoxReq, err := s.repo.GetAllKindBoxReq() + 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/get_by_id.go b/service/kind_box_req/get_by_id.go index 0846fe4..11b1170 100644 --- a/service/kind_box_req/get_by_id.go +++ b/service/kind_box_req/get_by_id.go @@ -1,8 +1,15 @@ package kindboxreqservice -import param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" +import ( + param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) -func (s Service) GetByID(request param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) { - // some code - panic("not implement") +func (s Service) GetByID(req param.KindBoxReqGetByIDRequest) (param.KindBoxReqGetByIDResponse, error) { + const op = "kindboxreqservice.GetByID" + kindBoxReq, err := s.repo.GetKindBoxReqByID(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 index b5026bd..e410256 100644 --- a/service/kind_box_req/update.go +++ b/service/kind_box_req/update.go @@ -2,9 +2,15 @@ package kindboxreqservice import ( param "git.gocasts.ir/ebhomengo/niki/param/kind_box_req" + // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error". ) -func (s Service) Update(updatedKindBoxReq param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { - // some code - panic("not implement") +func (s Service) Update(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") } diff --git a/validator/kind_box/get_by_id.go b/validator/kind_box/get_by_id.go index 2df433a..1223f02 100644 --- a/validator/kind_box/get_by_id.go +++ b/validator/kind_box/get_by_id.go @@ -7,8 +7,8 @@ import ( 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) ValidateGetByIDRequest(req param.KindBoxGetByIDRequest) (map[string]string, error) { + const op = "kindbox.ValidateGetByIDRequest" if err := validation.Validate(&req.KindBoxID, validation.Required); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/kind_box/update.go b/validator/kind_box/update.go index c6dde7f..7b9faa5 100644 --- a/validator/kind_box/update.go +++ b/validator/kind_box/update.go @@ -13,7 +13,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st if err := validation.ValidateStruct(&req, validation.Field(&req.KindBox.SerialNumber, validation.Required), - validation.Field(&req.KindBox.ReceiverId, + validation.Field(&req.KindBox.ReceiverID, validation.Required, validation.By(v.doesReceiverUserExist)), diff --git a/validator/kind_box/validator.go b/validator/kind_box/validator.go index ca9b354..a911681 100644 --- a/validator/kind_box/validator.go +++ b/validator/kind_box/validator.go @@ -20,8 +20,8 @@ func New(repo Repository) Validator { } func (v Validator) doesReceiverUserExist(value interface{}) error { - receiverId := value.(int) - _, err := v.repo.ReceiverUserExist(receiverId) + receiverID := value.(int) + _, err := v.repo.ReceiverUserExist(receiverID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -30,8 +30,8 @@ func (v Validator) doesReceiverUserExist(value interface{}) error { } func (v Validator) doesSenderUserExist(value interface{}) error { - senderId := value.(int) - _, err := v.repo.SenderUserExist(senderId) + senderID := value.(int) + _, err := v.repo.SenderUserExist(senderID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } diff --git a/validator/kind_box_req/add.go b/validator/kind_box_req/add.go index d00ecb4..cc9b454 100644 --- a/validator/kind_box_req/add.go +++ b/validator/kind_box_req/add.go @@ -12,13 +12,13 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin if err := validation.ValidateStruct(&req, - validation.Field(&req.KindBoxReq.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), + validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), - validation.Field(&req.KindBoxReq.BenefactorID, + validation.Field(&req.BenefactorID, validation.Required, validation.By(v.doesBeneFactorExist)), - validation.Field(&req.KindBoxReq.TypeID, + validation.Field(&req.TypeID, validation.Required, validation.By(v.doesTypeExist)), ); err != nil { diff --git a/validator/kind_box_req/get_by_id.go b/validator/kind_box_req/get_by_id.go index b461863..9f88708 100644 --- a/validator/kind_box_req/get_by_id.go +++ b/validator/kind_box_req/get_by_id.go @@ -7,8 +7,8 @@ import ( 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.KindBoxReqGetByIDRequest) (map[string]string, error) { + const op = "kindboxreq.ValidateGetByIDRequest" if err := validation.Validate(req.KindBoxReqID, validation.Required); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/kind_box_req/validator.go b/validator/kind_box_req/validator.go index dcc99fa..574d10c 100644 --- a/validator/kind_box_req/validator.go +++ b/validator/kind_box_req/validator.go @@ -25,8 +25,8 @@ func New(repo Repository) Validator { } func (v Validator) doesBeneFactorExist(value interface{}) error { - benefactorId := value.(int) - _, err := v.repo.BeneFactorExist(benefactorId) + benefactorID := value.(int) + _, err := v.repo.BeneFactorExist(benefactorID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -35,8 +35,8 @@ func (v Validator) doesBeneFactorExist(value interface{}) error { } func (v Validator) doesTypeExist(value interface{}) error { - typeId := value.(int) - _, err := v.repo.TypeExist(typeId) + typeID := value.(int) + _, err := v.repo.TypeExist(typeID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) }