product.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package helper
  2. import (
  3. "fmt"
  4. "github.com/gshopify/service-wrapper/fun"
  5. "github.com/gshopify/service-wrapper/model"
  6. "gshopper.com/gshopify/products/graphql/generated"
  7. "sort"
  8. )
  9. func NewProductConnection(src []*generated.Product,
  10. after *string, before *string, first *int, last *int, reverse *bool) (*generated.ProductConnection, error) {
  11. var (
  12. connection = &generated.ProductConnection{}
  13. err error
  14. )
  15. if len(src) == 0 {
  16. return connection, nil
  17. }
  18. if reverse != nil && *reverse {
  19. src = fun.Reverse(src)
  20. }
  21. connection.Nodes = src[:]
  22. connection.Nodes = fun.First(connection.Nodes, first)
  23. connection.Nodes = fun.Last(connection.Nodes, last)
  24. connection.Nodes, err = fun.WithCursor(connection.Nodes, after, model.GidProduct, true)
  25. if err != nil {
  26. return nil, fmt.Errorf("illegal cursor: %s", *after)
  27. }
  28. connection.Nodes, err = fun.WithCursor(connection.Nodes, before, model.GidProduct, false)
  29. if err != nil {
  30. return nil, fmt.Errorf("illegal cursor: %s", *before)
  31. }
  32. connection.PageInfo = model.NewPageInfo(connection.Nodes, model.GidProduct, src[0], src[len(src)-1])
  33. // Edges
  34. for i := range connection.Nodes {
  35. e := &generated.ProductEdge{
  36. Node: connection.Nodes[i],
  37. }
  38. if c, err := model.NewSimpleCursor(e.Node.ID, model.GidProduct); err == nil {
  39. e.Cursor = *c.String()
  40. }
  41. connection.Edges = append(connection.Edges, e)
  42. }
  43. return connection, nil
  44. }
  45. // SortProducts TODO: ProductCollectionSortKeysBestSelling
  46. // SortProducts TODO: ProductCollectionSortKeysCollectionDefault
  47. // SortProducts TODO: ProductCollectionSortKeysManual
  48. // SortProducts TODO: ProductCollectionSortKeysRelevance
  49. func SortProducts(src []*generated.Product, sortKey *generated.ProductCollectionSortKeys) []*generated.Product {
  50. if sortKey == nil || !sortKey.IsValid() {
  51. return src[:]
  52. }
  53. sort.Slice(src[:], func(i, j int) bool {
  54. switch *sortKey {
  55. case generated.ProductCollectionSortKeysID:
  56. return src[i].ID < src[j].ID
  57. case generated.ProductCollectionSortKeysTitle:
  58. return src[i].Title < src[j].Title
  59. case generated.ProductCollectionSortKeysCreated:
  60. return src[i].CreatedAt < src[j].CreatedAt
  61. case generated.ProductCollectionSortKeysPrice:
  62. return src[i].PriceRange.MinVariantPrice.Amount < src[j].PriceRange.MinVariantPrice.Amount
  63. }
  64. return false
  65. })
  66. return src[:]
  67. }
  68. // FilterProducts TODO: VariantOptionFilter
  69. // FilterProducts TODO: ProductMetafield
  70. // FilterProducts TODO: VariantMetafield
  71. func FilterProducts(src []*generated.Product, filters []*generated.ProductFilter) []*generated.Product {
  72. if len(src) < 1 {
  73. return src
  74. }
  75. var f = func(src []*generated.Product, filter *generated.ProductFilter) []*generated.Product {
  76. if filter == nil || len(src) < 1 {
  77. return src
  78. }
  79. var products = make([]*generated.Product, 0)
  80. for i := range src {
  81. if filter.Available != nil && src[i].AvailableForSale != *filter.Available {
  82. continue
  83. }
  84. if filter.ProductType != nil && src[i].ProductType != *filter.ProductType {
  85. continue
  86. }
  87. if filter.ProductVendor != nil && src[i].Vendor != *filter.ProductVendor {
  88. continue
  89. }
  90. if filter.Price != nil {
  91. if filter.Price.Min != nil && src[i].PriceRange.MinVariantPrice.Amount.Float() < *filter.Price.Min {
  92. continue
  93. }
  94. if filter.Price.Max != nil && src[i].PriceRange.MaxVariantPrice.Amount.Float() > *filter.Price.Max {
  95. continue
  96. }
  97. }
  98. products = append(products, src[i])
  99. }
  100. return products
  101. }
  102. for _, filter := range filters {
  103. src = f(src, filter)
  104. }
  105. return src
  106. }