forked from ebhomengo/niki
Merge pull request 'feat(admin): append refer time key on get all kindbox' (#210) from stage/hamed/kind_box_aggreation into develop
Reviewed-on: ebhomengo/niki#210
This commit is contained in:
commit
4e2baf17c9
|
|
@ -77,10 +77,26 @@ func (h Handler) GetAll(c echo.Context) error {
|
||||||
return echo.NewHTTPError(code, msg)
|
return echo.NewHTTPError(code, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deliverReferTime, dRErr := h.referTimeAggSvc.GetReferTimeByIDs(c.Request().Context(), getDeliverReferTimeIDs(kindboxes.Data))
|
||||||
|
if dRErr != nil {
|
||||||
|
msg, code := httpmsg.Error(dRErr)
|
||||||
|
|
||||||
|
return echo.NewHTTPError(code, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
returnReferTime, rRErr := h.referTimeAggSvc.GetReferTimeByIDs(c.Request().Context(), getReturnReferTimeIDs(kindboxes.Data))
|
||||||
|
if rRErr != nil {
|
||||||
|
msg, code := httpmsg.Error(rRErr)
|
||||||
|
|
||||||
|
return echo.NewHTTPError(code, msg)
|
||||||
|
}
|
||||||
|
|
||||||
resp := param.KindBoxGetAllResponse{
|
resp := param.KindBoxGetAllResponse{
|
||||||
Data: kindboxes.Data,
|
Data: kindboxes.Data,
|
||||||
Info: param.Info{
|
Info: param.Info{
|
||||||
Benefactors: benefactors,
|
Benefactors: benefactors,
|
||||||
|
DeliverReferTimes: deliverReferTime,
|
||||||
|
ReturnReferTimes: returnReferTime,
|
||||||
},
|
},
|
||||||
Pagination: kindboxes.Pagination,
|
Pagination: kindboxes.Pagination,
|
||||||
}
|
}
|
||||||
|
|
@ -94,3 +110,19 @@ func getBenefactorIDs(kindBoxes []param.Data) []any {
|
||||||
}
|
}
|
||||||
return arrayfunc.MapToSlice(benefactorsMap)
|
return arrayfunc.MapToSlice(benefactorsMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDeliverReferTimeIDs(kindBoxes []param.Data) []any {
|
||||||
|
deliverReferTimesMap := make(map[uint]bool)
|
||||||
|
for _, kindBox := range kindBoxes {
|
||||||
|
deliverReferTimesMap[kindBox.DeliverReferTimeID] = true
|
||||||
|
}
|
||||||
|
return arrayfunc.MapToSlice(deliverReferTimesMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getReturnReferTimeIDs(kindBoxes []param.Data) []any {
|
||||||
|
returnReferTimesMap := make(map[uint]bool)
|
||||||
|
for _, kindBox := range kindBoxes {
|
||||||
|
returnReferTimesMap[kindBox.ReturnReferTimeID] = true
|
||||||
|
}
|
||||||
|
return arrayfunc.MapToSlice(returnReferTimesMap)
|
||||||
|
}
|
||||||
|
|
|
||||||
12
docs/docs.go
12
docs/docs.go
|
|
@ -3545,6 +3545,18 @@ const docTemplate = `{
|
||||||
"items": {
|
"items": {
|
||||||
"$ref": "#/definitions/adminbenefactoreparam.Data"
|
"$ref": "#/definitions/adminbenefactoreparam.Data"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"deliver_refer_times": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/adminrefertimeparam.Data"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"return-refer-times": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/adminrefertimeparam.Data"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -3534,6 +3534,18 @@
|
||||||
"items": {
|
"items": {
|
||||||
"$ref": "#/definitions/adminbenefactoreparam.Data"
|
"$ref": "#/definitions/adminbenefactoreparam.Data"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"deliver_refer_times": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/adminrefertimeparam.Data"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"return-refer-times": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/adminrefertimeparam.Data"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -305,6 +305,14 @@ definitions:
|
||||||
items:
|
items:
|
||||||
$ref: '#/definitions/adminbenefactoreparam.Data'
|
$ref: '#/definitions/adminbenefactoreparam.Data'
|
||||||
type: array
|
type: array
|
||||||
|
deliver_refer_times:
|
||||||
|
items:
|
||||||
|
$ref: '#/definitions/adminrefertimeparam.Data'
|
||||||
|
type: array
|
||||||
|
return-refer-times:
|
||||||
|
items:
|
||||||
|
$ref: '#/definitions/adminrefertimeparam.Data'
|
||||||
|
type: array
|
||||||
type: object
|
type: object
|
||||||
adminkindboxparam.KindBoxGetAllResponse:
|
adminkindboxparam.KindBoxGetAllResponse:
|
||||||
properties:
|
properties:
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package adminkindboxparam
|
||||||
import (
|
import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/param"
|
"git.gocasts.ir/ebhomengo/niki/param"
|
||||||
adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
adminbenefactoreparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
||||||
|
adminrefertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KindBoxGetAllRequest struct {
|
type KindBoxGetAllRequest struct {
|
||||||
|
|
@ -13,7 +14,9 @@ type KindBoxGetAllRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
Benefactors []adminbenefactoreparam.Data `json:"benefactors"`
|
Benefactors []adminbenefactoreparam.Data `json:"benefactors"`
|
||||||
|
DeliverReferTimes []adminrefertimeparam.Data `json:"deliver_refer_times"`
|
||||||
|
ReturnReferTimes []adminrefertimeparam.Data `json:"return-refer-times"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindBoxGetAllResponse struct {
|
type KindBoxGetAllResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package mysqlrefertime
|
||||||
|
|
||||||
|
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) GetByIDs(ctx context.Context, referTimeIDs []any) (map[uint]entity.ReferTime, error) {
|
||||||
|
const op = "mysqlrefertime.GetByIDs"
|
||||||
|
|
||||||
|
if len(referTimeIDs) <= 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
query := `select * from refer_times where id in (%s)`
|
||||||
|
param := "?"
|
||||||
|
for i := 1; i < len(referTimeIDs); i++ {
|
||||||
|
param += ",?"
|
||||||
|
}
|
||||||
|
query = fmt.Sprintf(query, param)
|
||||||
|
rows, qErr := d.conn.Conn().QueryContext(ctx, query, referTimeIDs...)
|
||||||
|
if qErr != nil {
|
||||||
|
return nil, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
referTimes := make(map[uint]entity.ReferTime)
|
||||||
|
for rows.Next() {
|
||||||
|
referTime, sErr := scanReferTime(rows)
|
||||||
|
if sErr != nil {
|
||||||
|
return nil, richerror.New(op).WithErr(sErr).
|
||||||
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
referTimes[referTime.ID] = referTime
|
||||||
|
}
|
||||||
|
if rErr := rows.Err(); rErr != nil {
|
||||||
|
return nil, richerror.New(op).WithErr(rErr).
|
||||||
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
|
}
|
||||||
|
return referTimes, nil
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,24 @@ import (
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s Service) GetReferTimeByIDs(ctx context.Context, ids []any) ([]param.Data, error) {
|
||||||
|
const op = "adminrefertimeaggregatorservice.GetReferTimeByIDs"
|
||||||
|
var data []param.Data
|
||||||
|
|
||||||
|
referTimes, err := s.repo.GetByIDs(ctx, ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, richerror.New(op).WithErr(err)
|
||||||
|
}
|
||||||
|
for _, referTime := range referTimes {
|
||||||
|
data = append(data, param.Data{
|
||||||
|
ID: referTime.ID,
|
||||||
|
Status: referTime.Status,
|
||||||
|
Duration: referTime.Duration,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s Service) GetReferTimeByID(ctx context.Context, id uint) (param.Data, error) {
|
func (s Service) GetReferTimeByID(ctx context.Context, id uint) (param.Data, error) {
|
||||||
const op = "adminrefertimeaggregatorservice.GetReferTimeByID"
|
const op = "adminrefertimeaggregatorservice.GetReferTimeByID"
|
||||||
referTime, gErr := s.repo.Get(ctx, id)
|
referTime, gErr := s.repo.Get(ctx, id)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ type Service struct {
|
||||||
}
|
}
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error)
|
Get(ctx context.Context, referTimeID uint) (entity.ReferTime, error)
|
||||||
|
GetByIDs(ctx context.Context, id []any) (map[uint]entity.ReferTime, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(repo Repository) Service {
|
func New(repo Repository) Service {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue