minor change

This commit is contained in:
Abolfazl Nourzad 2024-02-12 11:26:28 +03:30
parent 430bd9a95c
commit bc9a625c03
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
6 changed files with 15 additions and 78 deletions

View File

@ -58,5 +58,5 @@ func InitAdminKindBoxService(db *mysql.DB) adminkindboxservice.Service {
} }
func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service { func InitAdminKindBoxReqService(db *mysql.DB) adminkindboxreqservice.Service {
return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db), InitAdminKindBoxService(db)) return adminkindboxreqservice.New(InitBenefactorKindBoxReqDB(db))
} }

View File

@ -1,12 +1 @@
package adminkindboxparam 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{}

View File

@ -1,6 +1,8 @@
package mysqlkindboxreq package mysqlkindboxreq
import ( import (
"database/sql"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/repository/mysql" "git.gocasts.ir/ebhomengo/niki/repository/mysql"
) )
@ -9,10 +11,18 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
var kindBoxReq entity.KindBoxReq var kindBoxReq entity.KindBoxReq
var kindBoxStatus string var kindBoxStatus string
var kindBoxType string var kindBoxType string
err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &kindBoxReq.CountAccepted, var countAccept sql.NullInt64
&kindBoxReq.Description, var desc sql.NullString
err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &countAccept,
&desc,
&kindBoxReq.ReferDate, &kindBoxStatus, &kindBoxReq.CreatedAt) &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.Status = entity.MapToKindBoxReqStatus(kindBoxStatus)
kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType) kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType)

View File

@ -1,31 +1 @@
package adminkindboxservice 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
}

View File

@ -3,8 +3,6 @@ package adminkindboxreqservice
import ( import (
"context" "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" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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) 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{ return param.KindBoxReqAcceptResponse{
KindBoxReqID: kindBoxReq.ID, KindBoxReqID: kindBoxReq.ID,
KindBoxReqStatus: kindBoxReq.Status, KindBoxReqStatus: kindBoxReq.Status,

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
) )
type Repository interface { type Repository interface {
@ -15,24 +14,12 @@ type Repository interface {
GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) 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 { type Service struct {
repo Repository repo Repository
// benefactorService BenefactorService
kindBoxClient KindBoxClient
} }
func New(repository Repository, kindBoxClient KindBoxClient) Service { func New(repository Repository) Service {
return Service{ return Service{
repo: repository, repo: repository,
kindBoxClient: kindBoxClient,
} }
} }