diff --git a/delivery/http_server/admin/kind_box/get_all.go b/delivery/http_server/admin/kind_box/get_all.go index 2a15b93f..e1a92750 100644 --- a/delivery/http_server/admin/kind_box/get_all.go +++ b/delivery/http_server/admin/kind_box/get_all.go @@ -4,6 +4,7 @@ import ( "net/http" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + arrayfunc "git.gocasts.ir/ebhomengo/niki/pkg/array_func" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" "github.com/labstack/echo/v4" @@ -50,18 +51,46 @@ func (h Handler) GetAll(c echo.Context) error { req.Filter = queryparam.GetFilterParams(c) - resp, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req) + kindboxes, sErr := h.adminKindBoxSvc.GetAll(c.Request().Context(), req) if sErr != nil { msg, code := httpmsg.Error(sErr) - if resp.FieldErrors != nil { + if kindboxes.FieldErrors != nil { return c.JSON(code, echo.Map{ "message": msg, - "errors": resp.FieldErrors, + "errors": kindboxes.FieldErrors, }) } return echo.NewHTTPError(code, msg) } + benefactors, bErr := h.adminBenefactorAggSvc.GetByIDs(c.Request().Context(), getBenefactorIDs(kindboxes.Data)) + if bErr != nil { + msg, code := httpmsg.Error(sErr) + if kindboxes.FieldErrors != nil { + return c.JSON(code, echo.Map{ + "message": msg, + "errors": kindboxes.FieldErrors, + }) + } + + return echo.NewHTTPError(code, msg) + } + + resp := param.KindBoxGetAllResponse{ + Data: kindboxes.Data, + Info: param.Info{ + Benefactors: benefactors, + }, + Pagination: kindboxes.Pagination, + } return c.JSON(http.StatusOK, resp) } + +func getBenefactorIDs(kindBoxes []param.Data) []any { + benefactorsMap := make(map[uint]bool) + for _, kindBox := range kindBoxes { + benefactorsMap[kindBox.BenefactorID] = true + } + return arrayfunc.MapToSlice(benefactorsMap) +} diff --git a/delivery/http_server/admin/kind_box/handler.go b/delivery/http_server/admin/kind_box/handler.go index 95e3f864..6a7f9171 100644 --- a/delivery/http_server/admin/kind_box/handler.go +++ b/delivery/http_server/admin/kind_box/handler.go @@ -2,27 +2,31 @@ package adminkindboxhandler import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" + adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { - authSvc authservice.Service - adminKindBoxSvc adminkindboxservice.Service - adminAuthorizeSvc adminauthorizationservice.Service - notificationSvc notification.Service + authSvc authservice.Service + adminKindBoxSvc adminkindboxservice.Service + adminBenefactorAggSvc adminbenefactoraggsvc.Service + adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, adminKindBoxSvc adminkindboxservice.Service, + adminBenefactorAggSvc adminbenefactoraggsvc.Service, adminAuthorizeSvc adminauthorizationservice.Service, notificationSvc notification.Service, ) Handler { return Handler{ - authSvc: authSvc, - adminKindBoxSvc: adminKindBoxSvc, - adminAuthorizeSvc: adminAuthorizeSvc, - notificationSvc: notificationSvc, + authSvc: authSvc, + adminKindBoxSvc: adminKindBoxSvc, + adminBenefactorAggSvc: adminBenefactorAggSvc, + adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/admin/kind_box_req/get_all.go b/delivery/http_server/admin/kind_box_req/get_all.go index 8db4e388..9712280d 100644 --- a/delivery/http_server/admin/kind_box_req/get_all.go +++ b/delivery/http_server/admin/kind_box_req/get_all.go @@ -4,6 +4,7 @@ import ( "net/http" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + arrayfunc "git.gocasts.ir/ebhomengo/niki/pkg/array_func" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param" echo "github.com/labstack/echo/v4" @@ -48,18 +49,46 @@ func (h Handler) GetAll(c echo.Context) error { req.Filter = queryparam.GetFilterParams(c) - resp, err := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) + kindBoxReqs, err := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) if err != nil { msg, code := httpmsg.Error(err) - if resp.FieldErrors != nil { + if kindBoxReqs.FieldErrors != nil { return c.JSON(code, httpmsg.ErrorResponse{ Message: msg, - Errors: resp.FieldErrors, + Errors: kindBoxReqs.FieldErrors, }) } return echo.NewHTTPError(code, msg) } + benefactors, bErr := h.adminBenefactorAggSvc.GetByIDs(c.Request().Context(), getBenefactorIDs(kindBoxReqs.Data)) + if bErr != nil { + msg, code := httpmsg.Error(bErr) + if kindBoxReqs.FieldErrors != nil { + return c.JSON(code, echo.Map{ + "message": msg, + "errors": kindBoxReqs.FieldErrors, + }) + } + + return echo.NewHTTPError(code, msg) + } + + resp := param.KindBoxReqGetAllResponse{ + Data: kindBoxReqs.Data, + Info: param.Info{ + Benefactors: benefactors, + }, + Pagination: kindBoxReqs.Pagination, + } return c.JSON(http.StatusOK, resp) } + +func getBenefactorIDs(kindBoxReqs []param.Data) []any { + benefactorsMap := make(map[uint]bool) + for _, kindBox := range kindBoxReqs { + benefactorsMap[kindBox.BenefactorID] = true + } + return arrayfunc.MapToSlice(benefactorsMap) +} diff --git a/delivery/http_server/admin/kind_box_req/handler.go b/delivery/http_server/admin/kind_box_req/handler.go index 74ee5960..062afea0 100644 --- a/delivery/http_server/admin/kind_box_req/handler.go +++ b/delivery/http_server/admin/kind_box_req/handler.go @@ -2,26 +2,31 @@ package adminkindboxreqhandler import ( adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" + adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" authservice "git.gocasts.ir/ebhomengo/niki/service/auth" "git.gocasts.ir/ebhomengo/niki/service/notification" ) type Handler struct { - authSvc authservice.Service - adminKindBoxReqSvc adminkindboxreqservice.Service - adminAuthorizeSvc adminauthorizationservice.Service - notificationSvc notification.Service + authSvc authservice.Service + adminKindBoxReqSvc adminkindboxreqservice.Service + adminBenefactorAggSvc adminbenefactoraggsvc.Service + adminAuthorizeSvc adminauthorizationservice.Service + notificationSvc notification.Service } func New(authSvc authservice.Service, adminKindBoxReqSvc adminkindboxreqservice.Service, - adminAuthorizeSvc adminauthorizationservice.Service, notificationSvc notification.Service, + adminBenefactorAggSvc adminbenefactoraggsvc.Service, + adminAuthorizeSvc adminauthorizationservice.Service, + notificationSvc notification.Service, ) Handler { return Handler{ - authSvc: authSvc, - adminKindBoxReqSvc: adminKindBoxReqSvc, - adminAuthorizeSvc: adminAuthorizeSvc, - notificationSvc: notificationSvc, + authSvc: authSvc, + adminKindBoxReqSvc: adminKindBoxReqSvc, + adminBenefactorAggSvc: adminBenefactorAggSvc, + adminAuthorizeSvc: adminAuthorizeSvc, + notificationSvc: notificationSvc, } } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 10cc69f3..83bb6e86 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -50,8 +50,8 @@ func New( Router: echo.New(), config: cfg, adminHandler: adminhandler.New(svc.AdminAuthSvc, svc.AdminSvc, svc.AdminAuthorizeSvc), - adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), - adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), + adminKindBoxReqHandler: adminkindboxreqhandler.New(svc.AdminAuthSvc, svc.AdminKindBoxReqSvc, svc.AdminBenefactorAggSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), + adminKindBoxHandler: adminKindBoxHandler.New(svc.AdminAuthSvc, svc.AdminKindBoxSvc, svc.AdminBenefactorAggSvc, svc.AdminAuthorizeSvc, svc.NotificationSvc), adminAgentHandler: adminagenthandler.New(svc.AdminAuthSvc, svc.AdminAgentSvc, svc.AdminAuthorizeSvc), adminBenefactorHandler: adminbenefactorhandler.New(svc.AdminAuthSvc, svc.AdminAuthorizeSvc, svc.AdminBenefactorSvc, svc.AdminAddressSvc, svc.AdminKindBoxSvc, svc.AdminKindBoxReqSvc), adminReferTimeHandler: adminrefertimehandler.New(svc.AdminAuthSvc, svc.AdminReferTimeSvc, svc.AdminAuthorizeSvc), diff --git a/param/admin/kind_box/get_all.go b/param/admin/kind_box/get_all.go index ae751161..0283db82 100644 --- a/param/admin/kind_box/get_all.go +++ b/param/admin/kind_box/get_all.go @@ -2,6 +2,7 @@ package adminkindboxparam import ( "git.gocasts.ir/ebhomengo/niki/param" + adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" ) type KindBoxGetAllRequest struct { @@ -11,8 +12,13 @@ type KindBoxGetAllRequest struct { Search param.SearchRequest } +type Info struct { + Benefactors []adminbenefactoreparam.Data `json:"benefactors"` +} + type KindBoxGetAllResponse struct { Data []Data `json:"data"` + Info Info `json:"info"` Pagination param.PaginationResponse `json:"pagination"` FieldErrors map[string]string `json:"field_errors,omitempty"` } diff --git a/param/admin/kind_box_req/get_all.go b/param/admin/kind_box_req/get_all.go index 55cc8e74..1ce722a3 100644 --- a/param/admin/kind_box_req/get_all.go +++ b/param/admin/kind_box_req/get_all.go @@ -2,6 +2,7 @@ package adminkindboxreqparam import ( "git.gocasts.ir/ebhomengo/niki/param" + adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" ) type KindBoxReqGetAllRequest struct { @@ -11,8 +12,13 @@ type KindBoxReqGetAllRequest struct { Search param.SearchRequest } +type Info struct { + Benefactors []adminbenefactoreparam.Data `json:"benefactors"` +} + type KindBoxReqGetAllResponse struct { Data []Data `json:"data"` + Info Info `json:"info"` Pagination param.PaginationResponse `json:"pagination"` FieldErrors map[string]string `json:"field_errors,omitempty"` } diff --git a/pkg/array_func/map.go b/pkg/array_func/map.go new file mode 100644 index 00000000..8816070d --- /dev/null +++ b/pkg/array_func/map.go @@ -0,0 +1,9 @@ +package arrayfunc + +func MapToSlice(mapList map[uint]bool) []any { + arrayList := make([]any, 0, len(mapList)) + for id := range mapList { + arrayList = append(arrayList, id) + } + return arrayList +} diff --git a/repository/mysql/benefactor/get_by_ids.go b/repository/mysql/benefactor/get_by_ids.go new file mode 100644 index 00000000..4b3f7658 --- /dev/null +++ b/repository/mysql/benefactor/get_by_ids.go @@ -0,0 +1,45 @@ +package mysqlbenefactor + +import ( + "context" + "fmt" + + "git.gocasts.ir/ebhomengo/niki/entity" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d *DB) GetBenefactorByIds(ctx context.Context, benefactorIDs []any) (map[uint]entity.Benefactor, error) { + const op = "mysqlbenefactor.GetBenefactorByIds" + + if len(benefactorIDs) <= 0 { + return nil, nil + } + query := `select * from benefactors where id in (%s)` + param := "?" + for i := 1; i < len(benefactorIDs); i++ { + param += ",?" + } + query = fmt.Sprintf(query, param) + rows, qErr := d.conn.Conn().QueryContext(ctx, query, benefactorIDs...) + if qErr != nil { + return nil, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + defer rows.Close() + + benefactors := make(map[uint]entity.Benefactor) + for rows.Next() { + benefactor, sErr := scanBenefactor(rows) + if sErr != nil { + return nil, richerror.New(op).WithErr(sErr). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + benefactors[benefactor.ID] = benefactor + } + if rErr := rows.Err(); rErr != nil { + return nil, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + return benefactors, nil +} diff --git a/repository/mysql/prepared_statement.go b/repository/mysql/prepared_statement.go index 80c907d3..8ec395ed 100644 --- a/repository/mysql/prepared_statement.go +++ b/repository/mysql/prepared_statement.go @@ -23,6 +23,7 @@ const ( StatementKeyAdminGetByPhoneNumber StatementKeyAdminAgentGetAll StatementKeyBenefactorGetByID + StatementKeyBenefactorGetByIDs StatementKeyBenefactorGetByPhoneNumber StatementKeyBenefactorCreate StatementKeyBenefactorGetAll diff --git a/service/admin/benefactor_aggregator/get.go b/service/admin/benefactor_aggregator/get.go new file mode 100644 index 00000000..1889c876 --- /dev/null +++ b/service/admin/benefactor_aggregator/get.go @@ -0,0 +1,33 @@ +package adminbenefactoraggregatorservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor" + response "git.gocasts.ir/ebhomengo/niki/pkg/response_builder" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) GetByIDs(ctx context.Context, benefactorIDs []any) ([]param.Data, error) { + const op = "adminbenefactoraggregatorservice.GetByIDs" + var data []param.Data + + benefactors, err := s.repo.GetBenefactorByIds(ctx, benefactorIDs) + if err != nil { + return nil, richerror.New(op).WithErr(err) + } + for _, benefactor := range benefactors { + data = append(data, param.Data{ + ID: benefactor.ID, + FirstName: benefactor.FirstName, + LastName: benefactor.LastName, + PhoneNumber: benefactor.PhoneNumber, + Description: benefactor.Description, + Email: benefactor.Email, + Gender: benefactor.Gender, + BirthDate: response.GetNullDate(benefactor.BirthDate), + Status: benefactor.Status, + }) + } + return data, nil +} diff --git a/service/admin/benefactor_aggregator/service.go b/service/admin/benefactor_aggregator/service.go new file mode 100644 index 00000000..da44564c --- /dev/null +++ b/service/admin/benefactor_aggregator/service.go @@ -0,0 +1,19 @@ +package adminbenefactoraggregatorservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type Repository interface { + GetBenefactorByIds(ctx context.Context, benefactorIDs []any) (map[uint]entity.Benefactor, error) +} + +type Service struct { + repo Repository +} + +func New(repo Repository) Service { + return Service{repo: repo} +} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index b12de392..58e3bae7 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -5,6 +5,7 @@ import ( "git.gocasts.ir/ebhomengo/niki/entity" params "git.gocasts.ir/ebhomengo/niki/param" + adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator" validator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" ) @@ -17,13 +18,15 @@ type Repository interface { } type Service struct { - repo Repository - vld validator.Validator + repo Repository + benefactorAggSvc adminbenefactoraggsvc.Service + vld validator.Validator } -func New(repo Repository, vld validator.Validator) Service { +func New(repo Repository, benefactorAggSvc adminbenefactoraggsvc.Service, vld validator.Validator) Service { return Service{ - repo: repo, - vld: vld, + repo: repo, + benefactorAggSvc: benefactorAggSvc, + vld: vld, } } diff --git a/service/service.go b/service/service.go index 6fb5ae7e..f21f9105 100644 --- a/service/service.go +++ b/service/service.go @@ -18,6 +18,7 @@ import ( adminagentservice "git.gocasts.ir/ebhomengo/niki/service/admin/agent" adminauthorizationservice "git.gocasts.ir/ebhomengo/niki/service/admin/authorization" adminbenefactorservice "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor" + adminbenefactoraggsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor_aggregator" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" adminrefertimeservice "git.gocasts.ir/ebhomengo/niki/service/admin/refer_time" @@ -62,6 +63,7 @@ type Service struct { NotificationSvc notification.Service BenefactorReferTimeSvc benefactorrefertimeservice.Service AdminReferTimeSvc adminrefertimeservice.Service + AdminBenefactorAggSvc adminbenefactoraggsvc.Service } func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscontract.SmsAdapter) *Service { @@ -76,19 +78,20 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont ) redisOtp := redisotp.New(rds) var ( - AdminAuthSvc = auth.New(cfg.AdminAuth) - AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo) - AdminReferTimeVld = adminrefertimevalidator.New() - AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo, AdminReferTimeVld) - AdminAddressSvc = adminaddressservice.New(addressRepo) - AdminBenefactorVld = adminbenefactorvalidator.New(benefactorRepo) - AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld) - AdminAgentSvc = adminagentservice.New(agentRepo) + AdminAuthSvc = auth.New(cfg.AdminAuth) + AdminAuthorizeSvc = adminauthorizationservice.New(adminRepo) + AdminReferTimeVld = adminrefertimevalidator.New() + AdminReferTimeSvc = adminrefertimeservice.New(referTimeRepo, AdminReferTimeVld) + AdminAddressSvc = adminaddressservice.New(addressRepo) + AdminBenefactorVld = adminbenefactorvalidator.New(benefactorRepo) + AdminBenefactorSvc = adminbenefactorservice.New(benefactorRepo, AdminAddressSvc, AdminBenefactorVld) + AdminAgentSvc = adminagentservice.New(agentRepo) + AdminBenefactorAggSvc = adminbenefactoraggsvc.New(benefactorRepo) AdminVld = adminvalidator.New(adminRepo) AdminSvc = adminservice.New(adminRepo, AdminAuthSvc, AdminVld) AdminKindBoxVld = adminkindboxvalidator.New(kindBoxRepo, AdminAgentSvc, AdminBenefactorSvc, AdminReferTimeSvc, AdminAddressSvc) - AdminKindBoxSvc = adminkindboxservice.New(kindBoxRepo, AdminKindBoxVld) + AdminKindBoxSvc = adminkindboxservice.New(kindBoxRepo, AdminBenefactorAggSvc, AdminKindBoxVld) AdminKindBoxReqVld = adminkindboxreqvalidator.New(kindBoxReqRepo, AdminSvc, AdminAgentSvc, AdminBenefactorSvc, AdminReferTimeSvc, AdminAddressSvc) AdminKindBoxReqSvc = adminkindboxreqservice.New(kindBoxReqRepo, AdminKindBoxReqVld) ) @@ -132,5 +135,6 @@ func New(cfg config.Config, db *mysql.DB, rds *redis.Adapter, smsAdapter smscont NotificationSvc: NotificationSvc, BenefactorReferTimeSvc: BenefactorReferTimeSvc, AdminReferTimeSvc: AdminReferTimeSvc, + AdminBenefactorAggSvc: AdminBenefactorAggSvc, } }