2
0

product_variant.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package relation
  2. import (
  3. "github.com/gshopify/service-wrapper/model"
  4. "github.com/gshopify/service-wrapper/scalar"
  5. "github.com/mailru/dbr"
  6. "gshopper.com/gshopify/products/graphql/generated"
  7. "time"
  8. )
  9. type ProductVariant struct {
  10. Id string `db:"id"`
  11. ProductId string `db:"product_id"`
  12. InventoryItemId string `db:"inventory_item_id"`
  13. Price float64 `db:"price"`
  14. CompareAtPrice float64 `db:"compare_at_price"`
  15. Position int8 `db:"position"`
  16. Image dbr.NullString `db:"image"`
  17. InventoryManagement string `db:"inventory_management"`
  18. InventoryPolicy string `db:"inventory_policy"`
  19. Grams dbr.NullFloat64 `db:"grams"`
  20. Weight dbr.NullFloat64 `db:"weight"`
  21. WeightUnit string `db:"weight_unit"`
  22. CreatedAt time.Time `db:"created_at"`
  23. UpdatedAt time.Time `db:"updated_at"`
  24. PublishedAt *time.Time `db:"published_at"`
  25. DeletedAt *time.Time `db:"deleted_at"`
  26. Options []struct {
  27. Field0 string
  28. Field1 int32
  29. } `db:"options"`
  30. Title string `db:"title"`
  31. Sku dbr.NullString `db:"sku"`
  32. Barcode dbr.NullString `db:"barcode"`
  33. RequiresShipping bool `db:"requires_shipping"`
  34. QuantityAvailable int `db:"available"`
  35. ForSale bool `db:"for_sale"`
  36. }
  37. func (v *ProductVariant) As() *generated.ProductVariant {
  38. variant := generated.ProductVariant{
  39. AvailableForSale: v.ForSale,
  40. CurrentlyNotInStock: v.QuantityAvailable <= 0,
  41. Image: nil,
  42. SelectedOptions: nil,
  43. UnitPrice: nil,
  44. UnitPriceMeasurement: nil,
  45. CompareAtPrice: &generated.MoneyV2{
  46. Amount: scalar.NewDecimal(v.CompareAtPrice),
  47. CurrencyCode: generated.CurrencyCodeUsd,
  48. },
  49. ID: model.NewId(model.GidVariant, v.Id),
  50. Price: &generated.MoneyV2{
  51. Amount: scalar.NewDecimal(v.Price),
  52. CurrencyCode: generated.CurrencyCodeUsd,
  53. },
  54. Product: &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
  55. QuantityAvailable: &v.QuantityAvailable,
  56. RequiresShipping: v.RequiresShipping,
  57. Title: v.Title,
  58. WeightUnit: generated.WeightUnit(v.WeightUnit),
  59. }
  60. variant.Metafields = append(variant.Metafields, &generated.Metafield{
  61. Namespace: model.GidVariant.String(),
  62. Key: "position",
  63. Type: model.MetaFieldTypeNumberInteger.String(),
  64. Value: model.MetaFieldValue(model.MetaFieldTypeNumberInteger, v.Position),
  65. })
  66. if v.Weight.Valid {
  67. s := v.Weight.Float64
  68. variant.Weight = &s
  69. }
  70. if v.Sku.Valid {
  71. s := v.Sku.String
  72. variant.Sku = &s
  73. }
  74. if v.Barcode.Valid {
  75. s := v.Barcode.String
  76. variant.Barcode = &s
  77. }
  78. return &variant
  79. }