-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdf-distance-extractor.go
74 lines (60 loc) · 1.81 KB
/
df-distance-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
package goautowp
import (
"context"
"github.com/autowp/goautowp/pictures"
"github.com/autowp/goautowp/query"
"github.com/autowp/goautowp/schema"
)
type DfDistanceExtractor struct {
picturesRepository *pictures.Repository
pictureExtractor *PictureExtractor
}
func NewDfDistanceExtractor(
picturesRepository *pictures.Repository, pictureExtractor *PictureExtractor,
) *DfDistanceExtractor {
return &DfDistanceExtractor{
picturesRepository: picturesRepository,
pictureExtractor: pictureExtractor,
}
}
func (s *DfDistanceExtractor) ExtractRows(
ctx context.Context, rows []*schema.DfDistanceRow, fields *DfDistanceFields, lang string, isModer bool, userID int64,
role string,
) ([]*DfDistance, error) {
result := make([]*DfDistance, 0, len(rows))
for _, row := range rows {
var dstPicture *Picture
dstPictureRequest := fields.GetDstPicture()
if row.DstPictureID != 0 && dstPictureRequest != nil {
dstPictureFields := dstPictureRequest.GetFields()
dstPictureOptions, err := convertPictureListOptions(dstPictureRequest.GetOptions())
if err != nil {
return nil, err
}
if dstPictureOptions == nil {
dstPictureOptions = &query.PictureListOptions{}
}
dstPictureOptions.ID = row.DstPictureID
picRow, err := s.picturesRepository.Picture(
ctx,
dstPictureOptions,
convertPictureFields(dstPictureFields),
convertPicturesOrder(dstPictureRequest.GetOrder()),
)
if err != nil {
return nil, err
}
dstPicture, err = s.pictureExtractor.Extract(ctx, picRow, dstPictureFields, lang, isModer, userID, role)
if err != nil {
return nil, err
}
}
result = append(result, &DfDistance{
SrcPictureId: row.SrcPictureID,
DstPictureId: row.DstPictureID,
Distance: int32(row.Distance), //nolint: gosec
DstPicture: dstPicture,
})
}
return result, nil
}