Browse Source

db.clickhouse

- retrieve product types
Alexey Kim 2 years ago
parent
commit
efce1dcac5
3 changed files with 39 additions and 5 deletions
  1. 34 4
      db/clickhouse.go
  2. 2 1
      db/database.go
  3. 3 0
      db/product.go

+ 34 - 4
db/clickhouse.go

@@ -378,13 +378,13 @@ func (db *clickhouse) ProductVariantOptions(ln model.LanguageCode, id string) ([
 	return options, nil
 }
 
-func (db *clickhouse) ProductTags() ([]model.ProductTag, error) {
+func (db *clickhouse) ProductTags() ([]*model.ProductTag, error) {
 	var (
-		tags []model.ProductTag
+		tags []*model.ProductTag
 		key  = productTagKey("list")
 		l    = ttlcache.LoaderFunc[string, any](
 			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
-				var o []model.ProductTag
+				var o []*model.ProductTag
 				rows, err := db.session.
 					Select(key.Selection()...).
 					From(key.Table()).
@@ -400,7 +400,7 @@ func (db *clickhouse) ProductTags() ([]model.ProductTag, error) {
 	)
 
 	if p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l)); p != nil {
-		for _, s := range p.Value().([]model.ProductTag) {
+		for _, s := range p.Value().([]*model.ProductTag) {
 			tags = append(tags, s)
 		}
 	}
@@ -408,6 +408,36 @@ func (db *clickhouse) ProductTags() ([]model.ProductTag, error) {
 	return tags, nil
 }
 
+func (db *clickhouse) ProductTypes() ([]*model.ProductType, error) {
+	var (
+		types []*model.ProductType
+		key   = productTypeKey("list")
+		l     = ttlcache.LoaderFunc[string, any](
+			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
+				var o []*model.ProductType
+				rows, err := db.session.
+					Select(key.Selection()...).
+					From(key.Table()).
+					OrderBy("count DESC").
+					Load(&o)
+				if rows < 1 || err != nil {
+					return nil
+				}
+
+				return ttl.Set(key.String(), o, key.TTL())
+			},
+		)
+	)
+
+	if p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l)); p != nil {
+		for _, s := range p.Value().([]*model.ProductType) {
+			types = append(types, s)
+		}
+	}
+
+	return types, nil
+}
+
 func (db *clickhouse) Ping() error {
 	return db.session.Ping()
 }

+ 2 - 1
db/database.go

@@ -21,7 +21,8 @@ type Database interface {
 	ProductOptions(ln model.LanguageCode, id string) ([]*generated.ProductOption, error)
 	ProductVariants(ctx *middleware.GShopifyContext, id string) ([]*generated.ProductVariant, error)
 	ProductVariantOptions(ln model.LanguageCode, id string) ([]*generated.SelectedOption, error)
-	ProductTags() ([]model.ProductTag, error)
+	ProductTags() ([]*model.ProductTag, error)
+	ProductTypes() ([]*model.ProductType, error)
 
 	Collection(ln model.LanguageCode, handle *string, id *string) (*generated.Collection, error)
 	Collections(ln model.LanguageCode) ([]*generated.Collection, error)

+ 3 - 0
db/product.go

@@ -97,4 +97,7 @@ var (
 	productTagKey = func(clause string) *cache.SqlKey {
 		return cache.NewSQLKey("product_tags", time.Minute, []string{"id, tag, products, count"}, clause)
 	}
+	productTypeKey = func(clause string) *cache.SqlKey {
+		return cache.NewSQLKey("product_types", time.Minute, []string{"id, type, products, count"}, clause)
+	}
 )