feat: update ListCategoryProducts to support target node type for category and group

This commit is contained in:
BXY 2026-09-23 14:18:26 +08:00
parent 0b848372f3
commit 4f8a99010a
2 changed files with 15 additions and 5 deletions

View File

@ -2540,8 +2540,8 @@ WHERE store_id = $1
return &counts, nil return &counts, nil
} }
// ListCategoryProducts loads paginated product summaries for a category ID. // 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, scope StoreScope) ([]*CategoryProductRecord, int64, error) { func (r *ProductRepository) ListCategoryProducts(ctx context.Context, categoryID string, page, pageSize int32, onlyVisible bool, targetNodeType int32, scope StoreScope) ([]*CategoryProductRecord, int64, error) {
if err := requireWriteScope(scope); err != nil { if err := requireWriteScope(scope); err != nil {
return nil, 0, err return nil, 0, err
} }
@ -2567,8 +2567,9 @@ WHERE b.category_id = $1
AND p.deleted_at IS NULL AND p.deleted_at IS NULL
AND p.store_id = $5 AND p.store_id = $5
AND c.store_id = $5 AND c.store_id = $5
AND c.node_type = $6
AND ($2 = FALSE OR p.status IN ($3, $4)) 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).Scan(&total); err != nil { `, 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, 0, fmt.Errorf("count category products: %w", err)
} }
if total == 0 { if total == 0 {
@ -2606,10 +2607,11 @@ WHERE b.category_id = $1
AND p.deleted_at IS NULL AND p.deleted_at IS NULL
AND p.store_id = $7 AND p.store_id = $7
AND c.store_id = $7 AND c.store_id = $7
AND c.node_type = $8
AND ($2 = FALSE OR p.status IN ($5, $6)) AND ($2 = FALSE OR p.status IN ($5, $6))
ORDER BY p.created_at DESC, p.id DESC ORDER BY p.created_at DESC, p.id DESC
LIMIT $3 OFFSET $4 LIMIT $3 OFFSET $4
`, parsedCategoryID, onlyVisible, pageSize, offset, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE), scope.StoreID) `, parsedCategoryID, onlyVisible, pageSize, offset, statusToDB(productv1.ProductStatus_PRODUCT_STATUS_ON_SALE), statusToDB(productv1.ProductStatus_PRODUCT_STATUS_OFF_SALE), scope.StoreID, targetNodeType)
if err != nil { if err != nil {
return nil, 0, fmt.Errorf("query category products: %w", err) return nil, 0, fmt.Errorf("query category products: %w", err)
} }

View File

@ -554,17 +554,25 @@ func (s *ProductServer) ListCategoryProducts(ctx context.Context, req *productv1
if err != nil || categoryID <= 0 { if err != nil || categoryID <= 0 {
return nil, status.Error(codes.InvalidArgument, "category_id must be a positive snowflake id") return nil, status.Error(codes.InvalidArgument, "category_id must be a positive snowflake id")
} }
targetNodeType := req.GetTargetNodeType()
if targetNodeType == 0 {
targetNodeType = 1
}
if targetNodeType != 1 && targetNodeType != 2 {
return nil, status.Error(codes.InvalidArgument, "target_node_type must be category (1) or group (2)")
}
page, pageSize := normalizedPage(req.GetPage(), req.GetPageSize()) page, pageSize := normalizedPage(req.GetPage(), req.GetPageSize())
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.list_by_category", trace.WithAttributes( dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.list_by_category", trace.WithAttributes(
attribute.String("category.id", req.GetCategoryId()), attribute.String("category.id", req.GetCategoryId()),
attribute.Int("category.node_type", int(targetNodeType)),
attribute.Int("page", int(page)), attribute.Int("page", int(page)),
attribute.Int("page_size", int(pageSize)), attribute.Int("page_size", int(pageSize)),
)) ))
defer dbSpan.End() defer dbSpan.End()
products, total, err := s.repo.ListCategoryProducts(dbCtx, req.GetCategoryId(), page, pageSize, req.GetOnlyVisible(), scope) products, total, err := s.repo.ListCategoryProducts(dbCtx, req.GetCategoryId(), page, pageSize, req.GetOnlyVisible(), targetNodeType, scope)
if err != nil { if err != nil {
dbSpan.RecordError(err) dbSpan.RecordError(err)
dbSpan.SetStatus(otelcodes.Error, err.Error()) dbSpan.SetStatus(otelcodes.Error, err.Error())