product_collection.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ProductCollection struct {
  11. Id string `db:"id"`
  12. Title string `db:"title"`
  13. Description string `db:"description"`
  14. Handle dbr.NullString `db:"handle"`
  15. Scope string `db:"scope"`
  16. SortOrder string `db:"sort_order"`
  17. Image dbr.NullString `db:"image"`
  18. TemplateSuffix dbr.NullString `db:"template_suffix"`
  19. CreatedAt time.Time `db:"created_at"`
  20. UpdatedAt time.Time `db:"updated_at"`
  21. PublishedAt *time.Time `db:"published_at"`
  22. DeletedAt *time.Time `db:"deleted_at"`
  23. }
  24. func (c *ProductCollection) As() *generated.Collection {
  25. var (
  26. description = bluemonday.StrictPolicy().Sanitize(c.Description)
  27. collection = &generated.Collection{
  28. Description: description,
  29. DescriptionHTML: scalar.Html(c.Description),
  30. Handle: c.Handle.String,
  31. ID: model.NewId(model.GidCollection, c.Id),
  32. Image: nil, //TODO:
  33. OnlineStoreURL: nil, //TODO:
  34. Seo: &generated.Seo{
  35. Description: &description,
  36. Title: &c.Title,
  37. },
  38. Title: c.Title,
  39. UpdatedAt: scalar.NewDateTimeFrom(c.UpdatedAt),
  40. }
  41. )
  42. if order := generated.CollectionSortOrder(c.SortOrder); order.IsValid() {
  43. collection.Metafields = append(collection.Metafields, &generated.Metafield{
  44. Namespace: model.GidCollection.String(),
  45. Key: "sort_order",
  46. Type: model.MetaFieldTypeSingleLineTextField.String(),
  47. Value: order.String(),
  48. })
  49. }
  50. if scope := model.PublishScope(c.Scope); scope.IsValid() {
  51. collection.Metafields = append(collection.Metafields, &generated.Metafield{
  52. Namespace: model.GidCollection.String(),
  53. Key: "scope",
  54. Type: model.MetaFieldTypeSingleLineTextField.String(),
  55. Value: scope.String(),
  56. })
  57. }
  58. return collection
  59. }