product.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "github.com/microcosm-cc/bluemonday"
  7. "gshopper.com/gshopify/products/graphql/generated"
  8. "time"
  9. )
  10. type Product struct {
  11. Id string `db:"id"`
  12. Title string `db:"title"`
  13. Description dbr.NullString `db:"description"`
  14. Handle dbr.NullString `db:"handle"`
  15. CreatedAt time.Time `db:"created_at"`
  16. UpdatedAt time.Time `db:"updated_at"`
  17. PublishedAt *time.Time `db:"published_at"`
  18. DeletedAt *time.Time `db:"deleted_at"`
  19. Type dbr.NullString `db:"type"`
  20. Scope string `db:"scope"`
  21. Status string `db:"status"`
  22. Tags []string `db:"tags"`
  23. TemplateSuffix dbr.NullString `db:"template_suffix"`
  24. Vendor dbr.NullString `db:"vendor"`
  25. Gift bool `db:"gift"`
  26. Collections []string `db:"collections"`
  27. Images []string `db:"images"`
  28. Options []struct {
  29. Field0 string
  30. Field1 int32
  31. } `db:"options"`
  32. InventoryManagement string `db:"inventory_management"`
  33. InventoryPolicy string `db:"inventory_policy"`
  34. RequiresShipping bool `db:"requires_shipping"`
  35. QuantityAvailable int `db:"available"`
  36. ForSale bool `db:"for_sale"`
  37. PriceMin float64 `db:"price_min"`
  38. PriceMax float64 `db:"price_max"`
  39. CompareAtPriceMin float64 `db:"compare_at_price_min"`
  40. CompareAtPriceMax float64 `db:"compare_at_price_max"`
  41. }
  42. func (p *Product) As() *generated.Product {
  43. description := bluemonday.StrictPolicy().Sanitize(p.Description.String)
  44. product := generated.Product{
  45. AvailableForSale: p.ForSale,
  46. PriceRange: &generated.ProductPriceRange{
  47. MaxVariantPrice: &generated.MoneyV2{
  48. Amount: scalar.NewDecimal(p.PriceMax),
  49. CurrencyCode: generated.CurrencyCodeUsd,
  50. },
  51. MinVariantPrice: &generated.MoneyV2{
  52. Amount: scalar.NewDecimal(p.PriceMin),
  53. CurrencyCode: generated.CurrencyCodeUsd,
  54. },
  55. },
  56. CompareAtPriceRange: &generated.ProductPriceRange{
  57. MaxVariantPrice: &generated.MoneyV2{
  58. Amount: scalar.NewDecimal(p.CompareAtPriceMax),
  59. CurrencyCode: generated.CurrencyCodeUsd,
  60. },
  61. MinVariantPrice: &generated.MoneyV2{
  62. Amount: scalar.NewDecimal(p.CompareAtPriceMin),
  63. CurrencyCode: generated.CurrencyCodeUsd,
  64. },
  65. },
  66. RequiresSellingPlan: false, // TODO:
  67. FeaturedImage: nil, // TODO:
  68. Images: nil, // TODO:
  69. Media: nil, // TODO:
  70. OnlineStoreURL: nil, // TODO:
  71. SellingPlanGroups: nil, // TODO:
  72. VariantBySelectedOptions: nil, // TODO:
  73. ID: model.NewId(model.GidProduct, p.Id),
  74. Handle: p.Handle.String,
  75. Title: p.Title,
  76. Vendor: p.Vendor.String,
  77. Description: description,
  78. DescriptionHTML: scalar.Html(p.Description.String),
  79. ProductType: p.Type.String,
  80. IsGiftCard: p.Gift,
  81. Tags: p.Tags,
  82. Seo: &generated.Seo{
  83. Description: &description,
  84. Title: &p.Title,
  85. },
  86. TotalInventory: &p.QuantityAvailable,
  87. CreatedAt: scalar.NewDateTimeFrom(p.CreatedAt),
  88. UpdatedAt: scalar.NewDateTimeFrom(p.UpdatedAt),
  89. }
  90. if p.PublishedAt != nil {
  91. product.PublishedAt = scalar.NewDateTimeFrom(*p.PublishedAt)
  92. }
  93. return &product
  94. }