|
@@ -55,24 +55,44 @@ func NewProductConnection(src []*generated.Product,
|
|
return connection, nil
|
|
return connection, nil
|
|
}
|
|
}
|
|
|
|
|
|
-// SortProducts TODO: ProductCollectionSortKeysBestSelling
|
|
|
|
-// SortProducts TODO: ProductCollectionSortKeysCollectionDefault
|
|
|
|
-// SortProducts TODO: ProductCollectionSortKeysManual
|
|
|
|
-// SortProducts TODO: ProductCollectionSortKeysRelevance
|
|
|
|
-func SortProducts(src []*generated.Product, sortKey *generated.ProductCollectionSortKeys) []*generated.Product {
|
|
|
|
- if sortKey == nil || !sortKey.IsValid() {
|
|
|
|
|
|
+func SortProducts(src []*generated.Product, k fmt.Stringer) []*generated.Product {
|
|
|
|
+ if k == nil {
|
|
return src[:]
|
|
return src[:]
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ sortBy := k.String()
|
|
|
|
+ if !generated.ProductSortKeys(sortBy).IsValid() &&
|
|
|
|
+ !generated.ProductCollectionSortKeys(sortBy).IsValid() {
|
|
|
|
+ return src[:]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return internalSortProducts(src[:], sortBy)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// internalSortProducts TODO: generated.ProductCollectionSortKeysBestSelling
|
|
|
|
+// internalSortProducts TODO: generated.ProductCollectionSortKeysCollectionDefault
|
|
|
|
+// internalSortProducts TODO: generated.ProductCollectionSortKeysManual
|
|
|
|
+// internalSortProducts TODO: generated.ProductCollectionSortKeysRelevance
|
|
|
|
+// internalSortProducts TODO: generated.ProductSortKeysBestSelling
|
|
|
|
+// internalSortProducts TODO: generated.ProductSortKeysRelevance
|
|
|
|
+func internalSortProducts(src []*generated.Product, key string) []*generated.Product {
|
|
sort.Slice(src[:], func(i, j int) bool {
|
|
sort.Slice(src[:], func(i, j int) bool {
|
|
- switch *sortKey {
|
|
|
|
- case generated.ProductCollectionSortKeysID:
|
|
|
|
|
|
+ switch key {
|
|
|
|
+ case "ID":
|
|
return src[i].ID < src[j].ID
|
|
return src[i].ID < src[j].ID
|
|
- case generated.ProductCollectionSortKeysTitle:
|
|
|
|
|
|
+ case "TITLE":
|
|
return src[i].Title < src[j].Title
|
|
return src[i].Title < src[j].Title
|
|
- case generated.ProductCollectionSortKeysCreated:
|
|
|
|
|
|
+ case "PRODUCT_TYPE":
|
|
|
|
+ return src[i].ProductType < src[j].ProductType
|
|
|
|
+ case "VENDOR":
|
|
|
|
+ return src[i].Vendor < src[j].Vendor
|
|
|
|
+ case "CREATED":
|
|
|
|
+ return src[i].CreatedAt < src[j].CreatedAt
|
|
|
|
+ case "CREATED_AT":
|
|
return src[i].CreatedAt < src[j].CreatedAt
|
|
return src[i].CreatedAt < src[j].CreatedAt
|
|
- case generated.ProductCollectionSortKeysPrice:
|
|
|
|
|
|
+ case "UPDATED_AT":
|
|
|
|
+ return src[i].UpdatedAt < src[j].UpdatedAt
|
|
|
|
+ case "PRICE":
|
|
return src[i].PriceRange.MinVariantPrice.Amount < src[j].PriceRange.MinVariantPrice.Amount
|
|
return src[i].PriceRange.MinVariantPrice.Amount < src[j].PriceRange.MinVariantPrice.Amount
|
|
}
|
|
}
|
|
|
|
|