From bc9a625c036ef36769fe397710b07a3f8129a391 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Mon, 12 Feb 2024 11:26:28 +0330 Subject: [PATCH] minor change --- internal/initial/service.go | 2 +- param/admin/kind_box/add.go | 11 ---------- repository/mysql/kind_box_req/scan.go | 14 +++++++++++-- service/admin/kind_box/add.go | 30 --------------------------- service/admin/kind_box_req/accept.go | 19 ----------------- service/admin/kind_box_req/service.go | 17 ++------------- 6 files changed, 15 insertions(+), 78 deletions(-) diff --git a/internal/initial/service.go b/internal/initial/service.go index 596bd33..02f322b 100644 --- a/internal/initial/service.go +++ b/internal/initial/service.go @@ -58,5 +58,5 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service { } func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service { - return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db)) + return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db)) } diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 0227fb9..1a39b36 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -1,12 +1 @@ package adminkindboxparam - -import entity "git.gocasts.ir/ebhomengo/niki/entity" - -type KindBoxAddAfterAcceptingReqRequest struct { - BenefactorID uint - KindBoxReqID uint - Type entity.KindBoxType - Count uint -} - -type KindBoxAddAfterAcceptingReqResponse struct{} diff --git a/repository/mysql/kind_box_req/scan.go b/repository/mysql/kind_box_req/scan.go index 5a165d5..9dbd51e 100644 --- a/repository/mysql/kind_box_req/scan.go +++ b/repository/mysql/kind_box_req/scan.go @@ -1,6 +1,8 @@ package mysqlkindboxreq import ( + "database/sql" + "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/repository/mysql" ) @@ -9,10 +11,18 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) { var kindBoxReq entity.KindBoxReq var kindBoxStatus string var kindBoxType string - err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &kindBoxReq.CountAccepted, - &kindBoxReq.Description, + var countAccept sql.NullInt64 + var desc sql.NullString + err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &countAccept, + &desc, &kindBoxReq.ReferDate, &kindBoxStatus, &kindBoxReq.CreatedAt) + if countAccept.Valid { + kindBoxReq.CountAccepted = uint(countAccept.Int64) + } + if desc.Valid { + kindBoxReq.Description = desc.String + } kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus) kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType) diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 2cac8c5..9b0bb5d 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -1,31 +1 @@ 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" - "github.com/oklog/ulid/v2" -) - -func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.KindBoxAddAfterAcceptingReqRequest) (param.KindBoxAddAfterAcceptingReqResponse, error) { - const op = "adminkindboxservice.AddKindBoxAfterAcceptingRequest" - - var kindBoxes []entity.KindBox - for i := 0; i < int(req.Count); i++ { - kindBoxes = append(kindBoxes, entity.KindBox{ - KindBoxReqID: req.KindBoxReqID, - BenefactorID: req.BenefactorID, - Type: req.Type, - Status: entity.KindBoxPendingSendStatus, - SerialNumber: ulid.Make().String(), - }) - } - err := s.repo.AddBatchKindBox(ctx, kindBoxes) - if err != nil { - return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxAddAfterAcceptingReqResponse{}, nil -} diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index 03c5721..dc0bfcb 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -3,8 +3,6 @@ package adminkindboxreqservice import ( "context" - "git.gocasts.ir/ebhomengo/niki/logger" - 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" ) @@ -26,23 +24,6 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) } - _, aErr := s.kindBoxClient.AddKindBoxAfterAcceptingRequest(ctx, adminkindboxparam.KindBoxAddAfterAcceptingReqRequest{ - BenefactorID: kindBoxReq.BenefactorID, - KindBoxReqID: kindBoxReq.ID, - Type: kindBoxReq.KindBoxType, - Count: kindBoxReq.CountAccepted, - }) - if aErr != nil { - // rollback kind box request status - rErr := s.repo.RollbackKindBoxRequestStatus(ctx, req.ID) - if rErr != nil { - // log error - logger.L().Error(rErr.Error()) - } - - return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(aErr) - } - return param.KindBoxReqAcceptResponse{ KindBoxReqID: kindBoxReq.ID, KindBoxReqStatus: kindBoxReq.Status, diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index b0e5eed..8f010ee 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -4,7 +4,6 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" - adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" ) type Repository interface { @@ -15,24 +14,12 @@ type Repository interface { GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) } -type KindBoxClient interface { - AddKindBoxAfterAcceptingRequest(ctx context.Context, param adminkindboxparam.KindBoxAddAfterAcceptingReqRequest) (adminkindboxparam.KindBoxAddAfterAcceptingReqResponse, error) -} - -// TODO: check validation. -// type BenefactorService interface { -// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error) -// } - type Service struct { repo Repository - // benefactorService BenefactorService - kindBoxClient KindBoxClient } -func New(repository Repository, kindBoxClient KindBoxClient) Service { +func New(repository Repository) Service { return Service{ - repo: repository, - kindBoxClient: kindBoxClient, + repo: repository, } }