product.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. Collections []string `db:"collections"`
  26. Images []string `db:"images"`
  27. Options []string `db:"options"`
  28. }
  29. func (p *Product) As() *generated.Product {
  30. description := bluemonday.StrictPolicy().Sanitize(p.Description.String)
  31. product := generated.Product{
  32. AvailableForSale: false,
  33. CompareAtPriceRange: nil,
  34. FeaturedImage: nil,
  35. Images: nil,
  36. IsGiftCard: false,
  37. Media: nil,
  38. OnlineStoreURL: nil,
  39. PriceRange: nil,
  40. RequiresSellingPlan: false,
  41. SellingPlanGroups: nil,
  42. TotalInventory: nil,
  43. VariantBySelectedOptions: nil,
  44. ID: model.NewId(model.GidProduct, p.Id),
  45. Handle: p.Handle.String,
  46. Title: p.Title,
  47. Vendor: p.Vendor.String,
  48. Description: description,
  49. DescriptionHTML: scalar.Html(p.Description.String),
  50. ProductType: p.Type.String,
  51. Tags: p.Tags,
  52. Seo: &generated.Seo{
  53. Description: &description,
  54. Title: &p.Title,
  55. },
  56. CreatedAt: scalar.NewDateTimeFrom(p.CreatedAt),
  57. UpdatedAt: scalar.NewDateTimeFrom(p.UpdatedAt),
  58. }
  59. if p.PublishedAt != nil {
  60. product.PublishedAt = scalar.NewDateTimeFrom(*p.PublishedAt)
  61. }
  62. return &product
  63. }