-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem-parent-cache-extractor.go
105 lines (82 loc) · 2.13 KB
/
item-parent-cache-extractor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package goautowp
import (
"context"
"github.com/autowp/goautowp/items"
"github.com/autowp/goautowp/query"
"github.com/autowp/goautowp/schema"
)
type ItemParentCacheExtractor struct {
repository *items.Repository
itemExtractor *ItemExtractor
}
func NewItemParentCacheExtractor(repository *items.Repository, itemExtractor *ItemExtractor) *ItemParentCacheExtractor {
return &ItemParentCacheExtractor{
repository: repository,
itemExtractor: itemExtractor,
}
}
func (s *ItemParentCacheExtractor) preloadItems( //nolint: dupl
ctx context.Context, request *ItemsRequest, ids []int64, lang string,
) (map[int64]*APIItem, error) {
if request == nil {
return nil, nil //nolint: nilnil
}
result := make(map[int64]*APIItem, len(ids))
if len(ids) == 0 {
return result, nil
}
itemFields := request.GetFields()
options, err := convertItemListOptions(request.GetOptions())
if err != nil {
return nil, err
}
if options == nil {
options = &query.ItemListOptions{}
}
options.ItemIDs = ids
options.Language = lang
rows, _, err := s.repository.List(
ctx,
options,
convertItemFields(itemFields),
items.OrderByNone,
false,
)
if err != nil {
return nil, err
}
for _, row := range rows {
result[row.ID], err = s.itemExtractor.Extract(ctx, row, itemFields, lang)
if err != nil {
return nil, err
}
}
return result, nil
}
func (s *ItemParentCacheExtractor) ExtractRows(
ctx context.Context, rows []*schema.ItemParentCacheRow, fields *ItemParentCacheFields, lang string,
) ([]*ItemParentCache, error) {
parentIDs := make([]int64, 0, len(rows))
for _, row := range rows {
if row.ParentID != 0 {
parentIDs = append(parentIDs, row.ParentID)
}
}
itemRequest := fields.GetParentItem()
parentItemRows, err := s.preloadItems(ctx, itemRequest, parentIDs, lang)
if err != nil {
return nil, err
}
result := make([]*ItemParentCache, 0, len(rows))
for _, row := range rows {
resultRow := &ItemParentCache{
ItemId: row.ItemID,
ParentId: row.ParentID,
}
if itemRequest != nil {
resultRow.ParentItem = parentItemRows[row.ParentID]
}
result = append(result, resultRow)
}
return result, nil
}