product_variant.go 3.1 KB

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