|
@@ -5,10 +5,11 @@ import (
|
|
|
"github.com/gshopify/service-wrapper/fun"
|
|
|
"github.com/gshopify/service-wrapper/model"
|
|
|
"gshopper.com/gshopify/products/graphql/generated"
|
|
|
+ "sort"
|
|
|
)
|
|
|
|
|
|
func NewProductConnection(src []*generated.Product,
|
|
|
- after *string, before *string, first *int, last *int, reverse *bool) (*generated.ProductConnection, error) {
|
|
|
+ after *string, before *string, first *int, last *int, reverse *bool, sortKey *generated.ProductCollectionSortKeys) (*generated.ProductConnection, error) {
|
|
|
var (
|
|
|
connection = &generated.ProductConnection{}
|
|
|
err error
|
|
@@ -22,6 +23,27 @@ func NewProductConnection(src []*generated.Product,
|
|
|
src = fun.Reverse(src)
|
|
|
}
|
|
|
|
|
|
+ //TODO: ProductCollectionSortKeysBestSelling
|
|
|
+ //TODO: ProductCollectionSortKeysCollectionDefault
|
|
|
+ //TODO: ProductCollectionSortKeysManual
|
|
|
+ //TODO: ProductCollectionSortKeysRelevance
|
|
|
+ if sortKey != nil && sortKey.IsValid() {
|
|
|
+ sort.Slice(src[:], func(i, j int) bool {
|
|
|
+ switch *sortKey {
|
|
|
+ case generated.ProductCollectionSortKeysID:
|
|
|
+ return src[i].ID < src[j].ID
|
|
|
+ case generated.ProductCollectionSortKeysTitle:
|
|
|
+ return src[i].Title < src[j].Title
|
|
|
+ case generated.ProductCollectionSortKeysCreated:
|
|
|
+ return src[i].CreatedAt < src[j].CreatedAt
|
|
|
+ case generated.ProductCollectionSortKeysPrice:
|
|
|
+ return src[i].PriceRange.MinVariantPrice.Amount < src[j].PriceRange.MinVariantPrice.Amount
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
connection.Nodes = src[:]
|
|
|
connection.Nodes = fun.First(connection.Nodes, first)
|
|
|
connection.Nodes = fun.Last(connection.Nodes, last)
|