2
0
Quellcode durchsuchen

refactoring: Product related models

Alexey Kim vor 2 Jahren
Ursprung
Commit
d367f7ac6d
6 geänderte Dateien mit 92 neuen und 106 gelöschten Zeilen
  1. 20 34
      db/clickhouse.go
  2. 13 8
      db/product.go
  3. 37 35
      relation/product.go
  4. 14 12
      relation/product_collection.go
  5. 8 7
      relation/product_option.go
  6. 0 10
      relation/product_status.go

+ 20 - 34
db/clickhouse.go

@@ -81,18 +81,12 @@ func (db *clickhouse) ProductCollections(ln model.LanguageCode, id string) ([]*g
 		key         = productCollectionKey("product.id=?", id)
 		l           = ttlcache.LoaderFunc[string, any](
 			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
-				var o []relation.Collection
-
+				var o []relation.ProductCollection
 				rows, err := db.session.SelectBySql("SELECT "+
-					ln.SqlFieldSelection("title", "")+
-					", "+
-					ln.SqlFieldSelection("description", "")+
-					", `id`, `handle`, `thumbnail`, "+
-					"`created_at`, `updated_at`, `deleted_at` "+
-					"FROM `product_collection` "+
-					"ARRAY JOIN (SELECT `collection_ids` FROM `product` WHERE `id` = ?) AS cid "+
-					"WHERE `id` = cid "+
-					"ORDER BY `created_at` ASC;", key.Args()...).
+					strings.Join(productCollectionSelection(ln), ", ")+
+					" FROM `"+key.Table()+"`"+
+					" ARRAY JOIN (SELECT `collections` FROM `product` WHERE `id` = ?) AS cid"+
+					" WHERE `id` = cid", key.Args()...).
 					Load(&o)
 				if rows < 1 || err != nil {
 					return nil
@@ -102,13 +96,10 @@ func (db *clickhouse) ProductCollections(ln model.LanguageCode, id string) ([]*g
 			})
 	)
 
-	p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l))
-	if p == nil {
-		return nil, fmt.Errorf("not found")
-	}
-
-	for _, row := range p.Value().([]relation.Collection) {
-		collections = append(collections, row.As())
+	if p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l)); p != nil {
+		for _, row := range p.Value().([]relation.ProductCollection) {
+			collections = append(collections, row.As())
+		}
 	}
 
 	return collections, nil
@@ -117,7 +108,7 @@ func (db *clickhouse) ProductCollections(ln model.LanguageCode, id string) ([]*g
 func (db *clickhouse) Product(ln model.LanguageCode, handle *string, id *string) (*generated.Product, error) {
 	var (
 		clause = strings.Builder{}
-		vars   = []any{relation.ProductStatusPublished}
+		vars   = []any{model.ProductStatusActive}
 	)
 
 	clause.WriteString("status=?")
@@ -141,7 +132,6 @@ func (db *clickhouse) Product(ln model.LanguageCode, handle *string, id *string)
 					Select(productSelection(ln)...).
 					From(key.Table()).
 					Where(key.Clause(), key.Args()...).
-					OrderBy("created_at").
 					Limit(1).
 					Load(&o)
 				if rows < 1 || err != nil {
@@ -168,11 +158,11 @@ func (db *clickhouse) ProductOptions(ln model.LanguageCode, id string) ([]*gener
 		l       = ttlcache.LoaderFunc[string, any](
 			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
 				var o []relation.ProductOption
-				rows, err := db.session.
-					Select(productOptionSelection(ln)...).
-					From(key.Table()).
-					Where(key.Clause(), key.Args()...).
-					OrderBy("created_at").
+				rows, err := db.session.SelectBySql("SELECT "+
+					strings.Join(productOptionSelection(ln), ", ")+
+					" FROM `"+key.Table()+"`"+
+					" ARRAY JOIN (SELECT `options` FROM `product` WHERE `id` = ?) AS oid"+
+					" WHERE `id` = oid", key.Args()).
 					Load(&o)
 				if rows < 1 || err != nil {
 					return nil
@@ -183,13 +173,10 @@ func (db *clickhouse) ProductOptions(ln model.LanguageCode, id string) ([]*gener
 		)
 	)
 
-	p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l))
-	if p == nil {
-		return nil, fmt.Errorf("not found")
-	}
-
-	for _, v := range p.Value().([]relation.ProductOption) {
-		options = append(options, v.As())
+	if p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l)); p != nil {
+		for _, v := range p.Value().([]relation.ProductOption) {
+			options = append(options, v.As())
+		}
 	}
 
 	return options, nil
@@ -243,7 +230,7 @@ func (db *clickhouse) ProductVariants(ctx *middleware.GShopifyContext, id string
 func (db *clickhouse) CollectionProducts(ln model.LanguageCode, id string) ([]*generated.Product, error) {
 	var (
 		products []*generated.Product
-		key      = productKey("has(collection_ids, ?)", id)
+		key      = productKey("has(collections, ?)", id)
 		l        = ttlcache.LoaderFunc[string, any](
 			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
 				var o []relation.Product
@@ -251,7 +238,6 @@ func (db *clickhouse) CollectionProducts(ln model.LanguageCode, id string) ([]*g
 					Select(productSelection(ln)...).
 					From(key.Table()).
 					Where(key.Clause(), key.Args()...).
-					OrderBy("created_at").
 					Load(&o)
 				if rows < 1 || err != nil {
 					return nil

+ 13 - 8
db/product.go

@@ -17,12 +17,20 @@ var (
 	}
 	productSelection = func(ln model.LanguageCode) []string {
 		return append([]string{
-			"id", "profile_id", "collection_ids", "type_id", "handle", "thumbnail", "is_giftcard", "discountable",
-			"hs_code", "mid_code", "weight", "length", "height", "width", "origin_country", "vendor", "material",
-			"created_at", "updated_at", "deleted_at", "tags", "status",
+			"id", "handle", "type", "scope", "tags", "vendor",
+			"collections", "images", "options", "variants",
+			"created_at", "updated_at", "published_at", "deleted_at",
+		},
+			ln.SqlFieldSelection("title", ""),
+			ln.SqlFieldSelection("description", ""),
+		)
+	}
+	productCollectionSelection = func(ln model.LanguageCode) []string {
+		return append([]string{
+			"id", "handle", "scope", "sort_order", "image", "template_suffix",
+			"created_at", "updated_at", "published_at", "deleted_at",
 		},
 			ln.SqlFieldSelection("title", ""),
-			ln.SqlFieldSelection("subtitle", ""),
 			ln.SqlFieldSelection("description", ""),
 		)
 	}
@@ -35,10 +43,7 @@ var (
 		)
 	}
 	productOptionSelection = func(ln model.LanguageCode) []string {
-		return append([]string{
-			"id", "product_id",
-			"created_at", "updated_at", "deleted_at",
-		},
+		return append([]string{"id", "position", "created_at", "updated_at", "published_at", "deleted_at"},
 			ln.SqlFieldSelection("name", ""),
 			ln.SqlArraySelection("values"),
 		)

+ 37 - 35
relation/product.go

@@ -10,38 +10,31 @@ import (
 )
 
 type Product struct {
-	Id            string         `db:"id"`
-	Title         string         `db:"title"`
-	Subtitle      dbr.NullString `db:"subtitle"`
-	Description   dbr.NullString `db:"description"`
-	Handle        dbr.NullString `db:"handle"`
-	IsGiftcard    bool           `db:"is_giftcard"`
-	Thumbnail     dbr.NullString `db:"thumbnail"`
-	ProfileId     string         `db:"profile_id"`
-	Weight        dbr.NullInt64  `db:"weight"`
-	Length        dbr.NullInt64  `db:"length"`
-	Height        dbr.NullInt64  `db:"height"`
-	Width         dbr.NullInt64  `db:"width"`
-	HsCode        dbr.NullString `db:"hs_code"`
-	OriginCountry dbr.NullString `db:"origin_country"`
-	Vendor        dbr.NullString `db:"vendor"`
-	MidCode       dbr.NullString `db:"mid_code"`
-	Material      dbr.NullString `db:"material"`
-	CreatedAt     time.Time      `db:"created_at"`
-	UpdatedAt     time.Time      `db:"updated_at"`
-	DeletedAt     *time.Time     `db:"deleted_at"`
-	CollectionIds []string       `db:"collection_ids"`
-	TypeId        dbr.NullString `db:"type_id"`
-	Discountable  bool           `db:"discountable"`
-	Status        string         `db:"status"`
-	Tags          []string       `db:"tags"`
-	ExternalId    dbr.NullString `db:"external_id"`
+	Id             string         `db:"id"`
+	Title          string         `db:"title"`
+	Description    dbr.NullString `db:"description"`
+	Handle         dbr.NullString `db:"handle"`
+	CreatedAt      time.Time      `db:"created_at"`
+	UpdatedAt      time.Time      `db:"updated_at"`
+	PublishedAt    *time.Time     `db:"published_at"`
+	DeletedAt      *time.Time     `db:"deleted_at"`
+	Type           dbr.NullString `db:"type"`
+	Scope          string         `db:"scope"`
+	Status         string         `db:"status"`
+	Tags           []string       `db:"tags"`
+	TemplateSuffix dbr.NullString `db:"template_suffix"`
+	Vendor         dbr.NullString `db:"vendor"`
+	Collections    []string       `db:"collections"`
+	Images         []string       `db:"images"`
+	Options        []string       `db:"options"`
+	Variants       []string       `db:"variants"`
 }
 
 func (p *Product) As() *generated.Product {
 	description := bluemonday.StrictPolicy().Sanitize(p.Description.String)
-	return &generated.Product{
+	product := generated.Product{
 		AvailableForSale:    false,
+		Collections:         nil,
 		CompareAtPriceRange: nil,
 		CreatedAt:           scalar.NewDateTimeFrom(p.CreatedAt),
 		Description:         description,
@@ -50,24 +43,33 @@ func (p *Product) As() *generated.Product {
 		Handle:              p.Handle.String,
 		ID:                  model.NewId(model.GidProduct, p.Id),
 		Images:              nil,
-		IsGiftCard:          p.IsGiftcard,
+		IsGiftCard:          false,
 		Media:               nil,
 		Metafield:           nil,
 		Metafields:          nil,
 		OnlineStoreURL:      nil,
+		Options:             nil,
 		PriceRange:          nil,
-		ProductType:         p.TypeId.String,
-		PublishedAt:         scalar.NewDateTimeFrom(p.CreatedAt),
+		ProductType:         p.Type.String,
+		PublishedAt:         0,
 		RequiresSellingPlan: false,
 		SellingPlanGroups:   nil,
 		Seo: &generated.Seo{
 			Description: &description,
 			Title:       &p.Title,
 		},
-		Tags:           p.Tags,
-		Title:          p.Title,
-		TotalInventory: nil,
-		UpdatedAt:      scalar.NewDateTimeFrom(p.UpdatedAt),
-		Vendor:         p.Vendor.String,
+		Tags:                     p.Tags,
+		Title:                    p.Title,
+		TotalInventory:           nil,
+		UpdatedAt:                scalar.NewDateTimeFrom(p.UpdatedAt),
+		VariantBySelectedOptions: nil,
+		Variants:                 nil,
+		Vendor:                   p.Vendor.String,
 	}
+
+	if p.PublishedAt != nil {
+		product.PublishedAt = scalar.NewDateTimeFrom(*p.PublishedAt)
+	}
+
+	return &product
 }

+ 14 - 12
relation/collection.go → relation/product_collection.go

@@ -9,18 +9,22 @@ import (
 	"time"
 )
 
-type Collection struct {
-	Id          string         `db:"id"`
-	Title       string         `db:"title"`
-	Description string         `db:"description"`
-	Handle      dbr.NullString `db:"handle"`
-	Thumbnail   dbr.NullString `db:"thumbnail"`
-	CreatedAt   time.Time      `db:"created_at"`
-	UpdatedAt   time.Time      `db:"updated_at"`
-	DeletedAt   *time.Time     `db:"deleted_at"`
+type ProductCollection struct {
+	Id             string         `db:"id"`
+	Title          string         `db:"title"`
+	Description    string         `db:"description"`
+	Handle         dbr.NullString `db:"handle"`
+	Scope          string         `db:"scope"`
+	SortOrder      string         `db:"sort_order"`
+	Image          dbr.NullString `db:"image"`
+	TemplateSuffix dbr.NullString `db:"template_suffix"`
+	CreatedAt      time.Time      `db:"created_at"`
+	UpdatedAt      time.Time      `db:"updated_at"`
+	PublishedAt    *time.Time     `db:"published_at"`
+	DeletedAt      *time.Time     `db:"deleted_at"`
 }
 
-func (c *Collection) As() *generated.Collection {
+func (c *ProductCollection) As() *generated.Collection {
 	description := bluemonday.StrictPolicy().Sanitize(c.Description)
 	return &generated.Collection{
 		Description:     description,
@@ -28,8 +32,6 @@ func (c *Collection) As() *generated.Collection {
 		Handle:          c.Handle.String,
 		ID:              model.NewId(model.GidCollection, c.Id),
 		Image:           nil, //TODO:
-		Metafield:       nil,
-		Metafields:      nil,
 		OnlineStoreURL:  nil, //TODO:
 		Seo: &generated.Seo{
 			Description: &description,

+ 8 - 7
relation/product_option.go

@@ -7,13 +7,14 @@ import (
 )
 
 type ProductOption struct {
-	Id        string     `db:"id"`
-	Name      string     `db:"name"`
-	Values    []string   `db:"values"`
-	ProductId string     `db:"product_id"`
-	CreatedAt time.Time  `db:"created_at"`
-	UpdatedAt time.Time  `db:"updated_at"`
-	DeletedAt *time.Time `db:"deleted_at"`
+	Id          string     `db:"id"`
+	Name        string     `db:"name"`
+	Values      []string   `db:"values"`
+	Position    int8       `db:"position"`
+	CreatedAt   time.Time  `db:"created_at"`
+	UpdatedAt   time.Time  `db:"updated_at"`
+	PublishedAt *time.Time `db:"published_at"`
+	DeletedAt   *time.Time `db:"deleted_at"`
 }
 
 func (opt *ProductOption) As() *generated.ProductOption {

+ 0 - 10
relation/product_status.go

@@ -1,10 +0,0 @@
-package relation
-
-type ProductStatus string
-
-const (
-	ProductStatusDraft     ProductStatus = "draft"
-	ProductStatusProposed  ProductStatus = "proposed"
-	ProductStatusPublished ProductStatus = "published"
-	ProductStatusRejected  ProductStatus = "rejected"
-)