Alexey Kim пре 2 година
родитељ
комит
c497dbf6e0
3 измењених фајлова са 35 додато и 40 уклоњено
  1. 20 0
      graphql/helper/money.go
  2. 4 21
      relation/product.go
  3. 11 19
      relation/product_variant.go

+ 20 - 0
graphql/helper/money.go

@@ -0,0 +1,20 @@
+package helper
+
+import (
+	"github.com/gshopify/service-wrapper/scalar"
+	"gshopper.com/gshopify/products/graphql/generated"
+)
+
+func NewPriceRange(min, max float64, currency generated.CurrencyCode) *generated.ProductPriceRange {
+	return &generated.ProductPriceRange{
+		MaxVariantPrice: NewMoney(max, currency),
+		MinVariantPrice: NewMoney(min, currency),
+	}
+}
+
+func NewMoney(f float64, currency generated.CurrencyCode) *generated.MoneyV2 {
+	return &generated.MoneyV2{
+		Amount:       scalar.NewDecimal(f),
+		CurrencyCode: currency,
+	}
+}

+ 4 - 21
relation/product.go

@@ -6,6 +6,7 @@ import (
 	"github.com/mailru/dbr"
 	"github.com/microcosm-cc/bluemonday"
 	"gshopper.com/gshopify/products/graphql/generated"
+	"gshopper.com/gshopify/products/graphql/helper"
 	"time"
 )
 
@@ -46,27 +47,9 @@ type Product struct {
 func (p *Product) As() *generated.Product {
 	description := bluemonday.StrictPolicy().Sanitize(p.Description.String)
 	product := generated.Product{
-		AvailableForSale: p.ForSale,
-		PriceRange: &generated.ProductPriceRange{
-			MaxVariantPrice: &generated.MoneyV2{
-				Amount:       scalar.NewDecimal(p.PriceMax),
-				CurrencyCode: generated.CurrencyCodeUsd,
-			},
-			MinVariantPrice: &generated.MoneyV2{
-				Amount:       scalar.NewDecimal(p.PriceMin),
-				CurrencyCode: generated.CurrencyCodeUsd,
-			},
-		},
-		CompareAtPriceRange: &generated.ProductPriceRange{
-			MaxVariantPrice: &generated.MoneyV2{
-				Amount:       scalar.NewDecimal(p.CompareAtPriceMax),
-				CurrencyCode: generated.CurrencyCodeUsd,
-			},
-			MinVariantPrice: &generated.MoneyV2{
-				Amount:       scalar.NewDecimal(p.CompareAtPriceMin),
-				CurrencyCode: generated.CurrencyCodeUsd,
-			},
-		},
+		AvailableForSale:    p.ForSale,
+		PriceRange:          helper.NewPriceRange(p.PriceMin, p.PriceMax, generated.CurrencyCodeUsd),
+		CompareAtPriceRange: helper.NewPriceRange(p.CompareAtPriceMin, p.CompareAtPriceMax, generated.CurrencyCodeUsd),
 
 		RequiresSellingPlan:      false, // TODO:
 		FeaturedImage:            nil,   // TODO:

+ 11 - 19
relation/product_variant.go

@@ -2,9 +2,9 @@ package relation
 
 import (
 	"github.com/gshopify/service-wrapper/model"
-	"github.com/gshopify/service-wrapper/scalar"
 	"github.com/mailru/dbr"
 	"gshopper.com/gshopify/products/graphql/generated"
+	"gshopper.com/gshopify/products/graphql/helper"
 	"time"
 )
 
@@ -35,28 +35,20 @@ type ProductVariant struct {
 
 func (v *ProductVariant) As() *generated.ProductVariant {
 	variant := generated.ProductVariant{
-		AvailableForSale:    v.ForSale,
-		CurrentlyNotInStock: v.QuantityAvailable <= 0,
-
 		Image:                nil, //TODO:
 		UnitPrice:            nil, //TODO:
 		UnitPriceMeasurement: nil, //TODO:
 
-		Price: &generated.MoneyV2{
-			Amount:       scalar.NewDecimal(v.Price),
-			CurrencyCode: generated.CurrencyCodeUsd,
-		},
-		CompareAtPrice: &generated.MoneyV2{
-			Amount:       scalar.NewDecimal(v.CompareAtPrice),
-			CurrencyCode: generated.CurrencyCodeUsd,
-		},
-
-		ID:                model.NewId(model.GidVariant, v.Id),
-		Product:           &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
-		QuantityAvailable: &v.QuantityAvailable,
-		RequiresShipping:  v.RequiresShipping,
-		Title:             v.Title,
-		WeightUnit:        generated.WeightUnit(v.WeightUnit),
+		ID:                  model.NewId(model.GidVariant, v.Id),
+		Product:             &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
+		Price:               helper.NewMoney(v.Price, generated.CurrencyCodeUsd),
+		CompareAtPrice:      helper.NewMoney(v.CompareAtPrice, generated.CurrencyCodeUsd),
+		AvailableForSale:    v.ForSale,
+		CurrentlyNotInStock: v.QuantityAvailable <= 0,
+		QuantityAvailable:   &v.QuantityAvailable,
+		RequiresShipping:    v.RequiresShipping,
+		Title:               v.Title,
+		WeightUnit:          generated.WeightUnit(v.WeightUnit),
 	}
 
 	variant.Metafields = append(variant.Metafields, &generated.Metafield{