/
search_product.go
42 lines (35 loc) 路 1.36 KB
/
search_product.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
package queries
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/romaxa83/mst-app/gateway/config"
"github.com/romaxa83/mst-app/gateway/internal/dto"
"github.com/romaxa83/mst-app/pkg/logger"
"github.com/romaxa83/mst-app/pkg/tracing"
readerService "github.com/romaxa83/mst-app/reader_service/proto/product_reader"
)
type SearchProductHandler interface {
Handle(ctx context.Context, query *SearchProductQuery) (*dto.ProductsListResponse, error)
}
type searchProductHandler struct {
log logger.Logger
cfg *config.Config
rsClient readerService.ReaderServiceClient
}
func NewSearchProductHandler(log logger.Logger, cfg *config.Config, rsClient readerService.ReaderServiceClient) *searchProductHandler {
return &searchProductHandler{log: log, cfg: cfg, rsClient: rsClient}
}
func (s *searchProductHandler) Handle(ctx context.Context, query *SearchProductQuery) (*dto.ProductsListResponse, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "searchProductHandler.Handle")
defer span.Finish()
ctx = tracing.InjectTextMapCarrierToGrpcMetaData(ctx, span.Context())
res, err := s.rsClient.SearchProduct(ctx, &readerService.SearchReq{
Search: query.Text,
Page: int64(query.Pagination.GetPage()),
Size: int64(query.Pagination.GetSize()),
})
if err != nil {
return nil, err
}
return dto.ProductsListResponseFromGrpc(res), nil
}