소스 검색

Collections

- basic implementation
- sort implementation
Alexey Kim 2 년 전
부모
커밋
3f178f039c
4개의 변경된 파일74개의 추가작업 그리고 4개의 파일을 삭제
  1. 30 1
      db/clickhouse.go
  2. 3 1
      db/database.go
  3. 23 0
      graphql/helper/collection.go
  4. 18 2
      graphql/query_collections.go

+ 30 - 1
db/clickhouse.go

@@ -75,6 +75,35 @@ func New(ctx context.Context, forceDebug bool) (Database, error) {
 	return r, nil
 }
 
+func (db *clickhouse) Collections(ln model.LanguageCode) ([]*generated.Collection, error) {
+	var (
+		collections []*generated.Collection
+		key         = productCollectionKey("list")
+		l           = ttlcache.LoaderFunc[string, any](
+			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
+				var o []relation.ProductCollection
+				rows, err := db.session.
+					Select(productCollectionSelection(ln)...).
+					From(key.Table()).
+					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 _, row := range p.Value().([]relation.ProductCollection) {
+			collections = append(collections, row.As())
+		}
+	}
+
+	return collections, nil
+}
+
 func (db *clickhouse) ProductCollections(ln model.LanguageCode, id string) ([]*generated.Collection, error) {
 	var (
 		collections []*generated.Collection
@@ -225,7 +254,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(t.collections, ?)", id)
+		key      = productKey("has(t.collections, ?) AND t.status = ?", id, model.ProductStatusActive)
 		l        = ttlcache.LoaderFunc[string, any](
 			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
 				var o []relation.Product

+ 3 - 1
db/database.go

@@ -17,9 +17,11 @@ type Database interface {
 	Close() error
 
 	Product(ln model.LanguageCode, handle *string, id *string) (*generated.Product, error)
-	ProductCollections(ln model.LanguageCode, id string) ([]*generated.Collection, error)
 	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)
+
+	Collections(ln model.LanguageCode) ([]*generated.Collection, error)
+	ProductCollections(ln model.LanguageCode, id string) ([]*generated.Collection, error)
 	CollectionProducts(ln model.LanguageCode, id string) ([]*generated.Product, error)
 }

+ 23 - 0
graphql/helper/collection.go

@@ -5,6 +5,7 @@ import (
 	"github.com/gshopify/service-wrapper/fun"
 	"github.com/gshopify/service-wrapper/model"
 	"gshopper.com/gshopify/products/graphql/generated"
+	"sort"
 )
 
 func NewCollectionConnection(src []*generated.Collection,
@@ -51,3 +52,25 @@ func NewCollectionConnection(src []*generated.Collection,
 
 	return connection, nil
 }
+
+// SortCollections TODO: CollectionSortKeysRelevance
+func SortCollections(src []*generated.Collection, sortKey *generated.CollectionSortKeys) []*generated.Collection {
+	if sortKey == nil || !sortKey.IsValid() {
+		return src[:]
+	}
+
+	sort.Slice(src[:], func(i, j int) bool {
+		switch *sortKey {
+		case generated.CollectionSortKeysID:
+			return src[i].ID < src[j].ID
+		case generated.CollectionSortKeysTitle:
+			return src[i].Title < src[j].Title
+		case generated.CollectionSortKeysUpdatedAt:
+			return src[i].UpdatedAt < src[j].UpdatedAt
+		}
+
+		return false
+	})
+
+	return src[:]
+}

+ 18 - 2
graphql/query_collections.go

@@ -10,8 +10,24 @@ import (
 	"gshopper.com/gshopify/products/graphql/helper"
 )
 
-func (r *queryResolver) Collections(ctx context.Context, after *string, before *string, first *int, last *int, query *string, reverse *bool, sort *generated.CollectionSortKeys) (*generated.CollectionConnection, error) {
-	panic("not implemented")
+func (r *queryResolver) Collections(ctx context.Context,
+	after *string, before *string, first *int, last *int, query *string, reverse *bool, sort *generated.CollectionSortKeys) (*generated.CollectionConnection, error) {
+	var (
+		inContext   *middleware.GShopifyContext
+		collections []*generated.Collection
+		err         error
+	)
+
+	if inContext, err = middleware.InContext(ctx); err != nil {
+		return nil, err
+	}
+
+	if collections, err = r.db.Collections(inContext.Language); err != nil {
+		return nil, err
+	}
+
+	collections = helper.SortCollections(collections, sort)
+	return helper.NewCollectionConnection(collections, after, before, first, last, reverse)
 }
 
 func (r *productResolver) Collections(ctx context.Context, product *generated.Product,