feat: update ListCategoryProducts to return product IDs and adjust related methods

This commit is contained in:
BXY 2026-09-23 17:41:39 +08:00
parent 7ab1dbb85c
commit 6e7449646c
2 changed files with 40 additions and 10 deletions

View File

@ -2604,13 +2604,13 @@ WHERE store_id = $1
}
// ListCategoryProducts loads paginated product summaries for a category or group node.
func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32, onlyVisible bool, targetNodeType int32, scope StoreScope) ([]*CategoryProductRecord, int64, error) {
func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32, onlyVisible bool, targetNodeType int32, scope StoreScope) ([]*CategoryProductRecord, []string, int64, error) {
if err := requireWriteScope(scope); err != nil {
return nil, 0, err
return nil, nil, 0, err
}
parsedCategoryID, err := parseID(categoryID, "category id")
if err != nil {
return nil, 0, err
return nil, nil, 0, err
}
if page < 1 {
@ -2633,10 +2633,39 @@ WHERE b.category_id = $1
AND c.node_type = $6
AND ($2 = FALSE OR p.status IN ($3, $4))
`, parsedCategoryID, onlyVisible, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE), scope.StoreID, targetNodeType).Scan(&total); err != nil {
return nil, 0, fmt.Errorf("count category products: %w", err)
return nil, nil, 0, fmt.Errorf("count category products: %w", err)
}
if total == 0 {
return []*CategoryProductRecord{}, 0, nil
return []*CategoryProductRecord{}, []string{}, 0, nil
}
idRows, err := r.db.QueryContext(ctx, `
SELECT p.id
FROM product.products p
JOIN product.product_category_bindings b ON b.product_id = p.id
JOIN product.product_category c ON c.id = b.category_id
WHERE b.category_id = $1
AND p.deleted_at IS NULL
AND p.store_id = $5
AND c.store_id = $5
AND c.node_type = $6
AND ($2 = FALSE OR p.status IN ($3, $4))
ORDER BY p.created_at DESC, p.id DESC
`, parsedCategoryID, onlyVisible, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE), scope.StoreID, targetNodeType)
if err != nil {
return nil, nil, 0, fmt.Errorf("query category product ids: %w", err)
}
defer idRows.Close()
productIDs := make([]string, 0)
for idRows.Next() {
var productID int64
if err := idRows.Scan(&productID); err != nil {
return nil, nil, 0, fmt.Errorf("scan category product id: %w", err)
}
productIDs = append(productIDs, formatID(productID))
}
if err := idRows.Err(); err != nil {
return nil, nil, 0, fmt.Errorf("iterate category product ids: %w", err)
}
offset := int64(page-1) * int64(pageSize)
@ -2676,7 +2705,7 @@ ORDER BY p.created_at DESC, p.id DESC
LIMIT $3 OFFSET $4
`, parsedCategoryID, onlyVisible, pageSize, offset, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE), scope.StoreID, targetNodeType)
if err != nil {
return nil, 0, fmt.Errorf("query category products: %w", err)
return nil, nil, 0, fmt.Errorf("query category products: %w", err)
}
defer rows.Close()
@ -2684,14 +2713,14 @@ LIMIT $3 OFFSET $4
for rows.Next() {
product, err := scanCategoryProduct(rows)
if err != nil {
return nil, 0, err
return nil, nil, 0, err
}
products = append(products, product)
}
if err := rows.Err(); err != nil {
return nil, 0, fmt.Errorf("iterate category products: %w", err)
return nil, nil, 0, fmt.Errorf("iterate category products: %w", err)
}
return products, total, nil
return products, productIDs, total, nil
}
// ListCategoryTreeNodes loads product category nodes for storefront category trees.

View File

@ -578,7 +578,7 @@ func (s *ProductServer) ListCategoryProducts(ctx context.Context, req *productv1
))
defer dbSpan.End()
products, total, err := s.repo.ListCategoryProducts(dbCtx, req.GetCategoryId(), page, pageSize, req.GetOnlyVisible(), targetNodeType, scope)
products, productIDs, total, err := s.repo.ListCategoryProducts(dbCtx, req.GetCategoryId(), page, pageSize, req.GetOnlyVisible(), targetNodeType, scope)
if err != nil {
dbSpan.RecordError(err)
dbSpan.SetStatus(otelcodes.Error, err.Error())
@ -599,6 +599,7 @@ func (s *ProductServer) ListCategoryProducts(ctx context.Context, req *productv1
PageSize: pageSize,
Total: total,
TotalPages: totalPages(total, pageSize),
ProductIds: productIDs,
}, nil
}