product.go 2.9 KB

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