product.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. TotalInventory: func() *int {
  67. i := p.QuantityAvailable
  68. return &i
  69. }(),
  70. RequiresSellingPlan: false, // TODO:
  71. FeaturedImage: nil,
  72. Images: nil,
  73. Media: nil,
  74. OnlineStoreURL: nil,
  75. SellingPlanGroups: nil,
  76. VariantBySelectedOptions: nil,
  77. ID: model.NewId(model.GidProduct, p.Id),
  78. Handle: p.Handle.String,
  79. Title: p.Title,
  80. Vendor: p.Vendor.String,
  81. Description: description,
  82. DescriptionHTML: scalar.Html(p.Description.String),
  83. ProductType: p.Type.String,
  84. IsGiftCard: p.Gift,
  85. Tags: p.Tags,
  86. Seo: &generated.Seo{
  87. Description: &description,
  88. Title: &p.Title,
  89. },
  90. CreatedAt: scalar.NewDateTimeFrom(p.CreatedAt),
  91. UpdatedAt: scalar.NewDateTimeFrom(p.UpdatedAt),
  92. }
  93. if p.PublishedAt != nil {
  94. product.PublishedAt = scalar.NewDateTimeFrom(*p.PublishedAt)
  95. }
  96. return &product
  97. }