From dc9d9312275ac82139ed29f34bd7f313c3452498 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Tue, 23 Jan 2024 11:09:58 +0330 Subject: [PATCH] add kind box migration and service --- .../http_server/admin/kind_box_req/route.go | 1 + entity/kind_box.go | 9 ++-- param/admin/kind_box/add.go | 2 - param/admin/kind_box_req/accept.go | 24 +++++---- repository/mysql/kind_box/kind_box.go | 9 ++++ .../1705992494_add_kind_boxes_table.sql | 24 +++++++++ service/admin/kind_box/add.go | 2 - service/admin/kind_box/delete.go | 36 ++++++------- service/admin/kind_box/get.go | 34 ++++++------ service/admin/kind_box/get_all.go | 32 +++++------ service/admin/kind_box/service.go | 8 +-- service/admin/kind_box/update.go | 54 +++++++++---------- service/admin/kind_box_req/accept.go | 33 +++++++----- service/admin/kind_box_req/service.go | 4 +- 14 files changed, 158 insertions(+), 114 deletions(-) create mode 100644 repository/mysql/migration/1705992494_add_kind_boxes_table.sql diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index c0bcc53..578dd01 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -13,6 +13,7 @@ func (h Handler) SetRoutes(e *echo.Echo) { //r.GET("/", h.GetAll).Name = "admin-getallkindboxreq" //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" + // todo - add acl r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/reject-kind-box-req/:id", h.Reject) } diff --git a/entity/kind_box.go b/entity/kind_box.go index 6656fbf..69d3bb8 100644 --- a/entity/kind_box.go +++ b/entity/kind_box.go @@ -4,12 +4,13 @@ import "time" type KindBox struct { ID uint - KindBoxReqID uint // TODO like database model - BenefactorID uint // TODO need in business entity + KindBoxReqID uint + BenefactorID uint + Type KindBoxType TotalAmount uint - ReceiverID uint - SenderID uint SerialNumber string Status KindBoxStatus + SenderID uint + ReceiverID uint StatusChangedAt time.Time } diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index d1a2705..408e55c 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -5,8 +5,6 @@ import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxAddRequest struct { BenefactorID uint KindBoxReqID uint - SenderID uint - SerialNumber string } type KindBoxAddResponse struct { diff --git a/param/admin/kind_box_req/accept.go b/param/admin/kind_box_req/accept.go index 60573b9..01b42d1 100644 --- a/param/admin/kind_box_req/accept.go +++ b/param/admin/kind_box_req/accept.go @@ -1,9 +1,8 @@ package adminkindboxreqparam import ( - "time" - "git.gocasts.ir/ebhomengo/niki/entity" + "time" ) type KindBoxReqAcceptRequest struct { @@ -12,13 +11,16 @@ type KindBoxReqAcceptRequest struct { } type KindBoxReqAcceptResponse struct { - ID uint `json:"id"` - KindBoxType entity.KindBoxType `json:"kind_box_type"` - CountRequested uint `json:"count_requested"` - CountAccepted uint `json:"count_accepted"` - BenefactorID uint `json:"benefactor_id"` - Status entity.KindBoxReqStatus `json:"status"` - Description string `json:"description"` - ReferDate time.Time `json:"refer_date"` - AddressID uint `json:"address_id"` + KindBoxReqID uint `json:"kind_box_req_id"` + KindBoxReqStatus entity.KindBoxReqStatus `json:"kind_box_req_status"` + CountRequested uint `json:"count_requested"` + CountAccepted uint `json:"count_accepted"` + ReferDate time.Time `json:"refer_date"` + AddressID uint `json:"address_id"` + + KindBoxID uint `json:"kind_box_id"` + KindBoxType entity.KindBoxType `json:"kind_box_type"` + BenefactorID uint `json:"benefactor_id"` + KindBoxStatus entity.KindBoxStatus `json:"kind_box_status"` + KindBoxSerialNumber string `json:"kind_box_serial_number"` } diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 6dcf146..aae17ef 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -1 +1,10 @@ package mysqlkindbox + +import ( + "context" + "git.gocasts.ir/ebhomengo/niki/entity" +) + +func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) { + return entity.KindBox{}, nil +} diff --git a/repository/mysql/migration/1705992494_add_kind_boxes_table.sql b/repository/mysql/migration/1705992494_add_kind_boxes_table.sql new file mode 100644 index 0000000..8f7fd48 --- /dev/null +++ b/repository/mysql/migration/1705992494_add_kind_boxes_table.sql @@ -0,0 +1,24 @@ +-- +migrate Up +CREATE TABLE `kind_boxes` +( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `kind_box_req_id` INT NOT NULL, + `benefactor_id` INT NOT NULL, + `type` ENUM ('on-table','cylindrical','stand-up') NOT NULL, + `total_amount` INT UNSIGNED NULL NULL, + `serial_number` varchar(255), + `status` varchar(255), + `sender_id` INT NULL, + `receiver_id` INT NULL, + `status_changed_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`kind_box_req_id`) REFERENCES `kind_box_reqs` (`id`), + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`), + FOREIGN KEY (`sender_id`) REFERENCES `admins` (`id`), + FOREIGN KEY (`receiver_id`) REFERENCES `admins` (`id`), + index `index_serial_number` (`serial_number`) + +); + +-- +migrate Down +DROP TABLE `kind_boxes`; \ No newline at end of file diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index bff284e..98390a2 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -14,8 +14,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ BenefactorID: req.BenefactorID, KindBoxReqID: req.KindBoxReqID, - SenderID: req.SenderID, - SerialNumber: req.SerialNumber, Status: entity.KindBoxPendingSendStatus, }) if err != nil { diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index b4f04ef..f71270a 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -1,20 +1,20 @@ 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) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { - // TODO: Does business domain need to delete an kindbox ? - const op = "adminkindboxservice.Delete" - - 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 -} +//import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//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" +// +// 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 c0a80f0..0bfca1d 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -1,19 +1,19 @@ 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 -} +//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/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index 4030d34..9832357 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -1,18 +1,18 @@ 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) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { - const op = "adminkindboxservice.GetAll" - allKindBox, err := s.repo.GetAllKindBox(ctx) - if err != nil { - return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil -} +//import ( +// "context" +// +// 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.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { +// const op = "adminkindboxservice.GetAll" +// allKindBox, err := s.repo.GetAllKindBox(ctx) +// if err != nil { +// return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil +//} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 842a171..b49506d 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -8,10 +8,10 @@ import ( type Repository interface { AddKindBox(ctx context.Context, kindBox 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) + //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) } type Service struct { diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index eba5087..0778752 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -1,29 +1,29 @@ package adminkindboxservice -import ( - "context" - - entity "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, 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 = "adminkindboxservice.Update" - - 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 -} +//import ( +// "context" +// +// entity "git.gocasts.ir/ebhomengo/niki/entity" +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, 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 = "adminkindboxservice.Update" +// +// 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/accept.go b/service/admin/kind_box_req/accept.go index f25116e..e3cc2bc 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,7 +2,7 @@ package adminkindboxreqservice import ( "context" - + adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -15,22 +15,31 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) } // fire new event to create a kind-box. - // get kind box req kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { - return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) + } + + res, kErr := s.kindBoxSvc.Add(ctx, adminkindboxparam.KindBoxAddRequest{ + BenefactorID: kindBoxReq.BenefactorID, + KindBoxReqID: kindBoxReq.ID, + }) + if kErr != nil { + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } return param.KindBoxReqAcceptResponse{ - ID: kindBoxReq.ID, - KindBoxType: kindBoxReq.KindBoxType, - CountRequested: kindBoxReq.CountRequested, - CountAccepted: kindBoxReq.CountAccepted, - BenefactorID: kindBoxReq.BenefactorID, - Status: kindBoxReq.Status, - Description: kindBoxReq.Description, - ReferDate: kindBoxReq.ReferDate, - AddressID: kindBoxReq.AddressID, + KindBoxReqID: kindBoxReq.ID, + KindBoxReqStatus: kindBoxReq.Status, + CountRequested: kindBoxReq.CountRequested, + CountAccepted: kindBoxReq.CountAccepted, + ReferDate: kindBoxReq.ReferDate, + AddressID: kindBoxReq.AddressID, + KindBoxID: res.KindBox.ID, + KindBoxType: kindBoxReq.KindBoxType, + BenefactorID: kindBoxReq.BenefactorID, + KindBoxStatus: res.KindBox.Status, + KindBoxSerialNumber: res.KindBox.SerialNumber, }, nil } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 80a415b..172feb5 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" "git.gocasts.ir/ebhomengo/niki/entity" ) @@ -24,7 +25,8 @@ type Repository interface { // } type Service struct { - repo Repository + repo Repository + kindBoxSvc adminkindboxservice.Service // benefactorService BenefactorService }