mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat(product): add GetProductBySKU method and corresponding server handler with validation
This commit is contained in:
parent
c41ca76a2a
commit
e2df9fd2e7
@ -1636,6 +1636,27 @@ func (r *ProductRepository) GetProduct(ctx context.Context, productID string) (*
|
|||||||
return product, skus[productID], nil
|
return product, skus[productID], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProductBySKU loads the SPU that owns skuID (product_items.id is the SKU PK).
|
||||||
|
func (r *ProductRepository) GetProductBySKU(ctx context.Context, skuID string) (*ProductRecord, []SKURecord, error) {
|
||||||
|
parsedSKU, err := parseID(skuID, "sku_id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
var productID int64
|
||||||
|
err = r.db.QueryRowContext(ctx, `
|
||||||
|
SELECT product_id
|
||||||
|
FROM product.product_items
|
||||||
|
WHERE id = $1 AND deleted_at IS NULL AND is_archived = FALSE
|
||||||
|
`, parsedSKU).Scan(&productID)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil, ErrSKUNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("lookup product by sku: %w", err)
|
||||||
|
}
|
||||||
|
return r.GetProduct(ctx, formatID(productID))
|
||||||
|
}
|
||||||
|
|
||||||
func (r *ProductRepository) listProductLeafCategoryIDs(ctx context.Context, productID string) ([]string, error) {
|
func (r *ProductRepository) listProductLeafCategoryIDs(ctx context.Context, productID string) ([]string, error) {
|
||||||
parsedProductID, err := parseID(productID, "product_id")
|
parsedProductID, err := parseID(productID, "product_id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
19
internal/server/get_product_by_sku_test.go
Normal file
19
internal/server/get_product_by_sku_test.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetProductBySkuRequiresID(t *testing.T) {
|
||||||
|
s := NewProductServer(nil, nil, nil, nil)
|
||||||
|
_, err := s.GetProductBySku(context.Background(), &productv1.GetProductBySkuRequest{})
|
||||||
|
if status.Code(err) != codes.InvalidArgument {
|
||||||
|
t.Fatalf("err = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -157,6 +157,39 @@ func (s *ProductServer) GetProduct(ctx context.Context, req *productv1.GetProduc
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProductBySku returns the SPU that owns the SKU.
|
||||||
|
func (s *ProductServer) GetProductBySku(ctx context.Context, req *productv1.GetProductBySkuRequest) (*productv1.GetProductResponse, error) {
|
||||||
|
if req == nil || req.GetSkuId() == "" {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, "sku_id is required")
|
||||||
|
}
|
||||||
|
if err := validatePositiveID(req.GetSkuId(), "sku_id"); err != nil {
|
||||||
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
dbCtx, dbSpan := s.observer.Tracer().Start(ctx, "product.load_by_sku", trace.WithAttributes(attribute.String("sku.id", req.GetSkuId())))
|
||||||
|
defer dbSpan.End()
|
||||||
|
|
||||||
|
product, skus, err := s.repo.GetProductBySKU(dbCtx, req.GetSkuId())
|
||||||
|
if errors.Is(err, repository.ErrSKUNotFound) {
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, "sku not found")
|
||||||
|
return nil, status.Error(codes.NotFound, "sku not found")
|
||||||
|
}
|
||||||
|
if errors.Is(err, repository.ErrProductNotFound) {
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, "product not found")
|
||||||
|
return nil, status.Error(codes.NotFound, "product not found")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
dbSpan.RecordError(err)
|
||||||
|
dbSpan.SetStatus(otelcodes.Error, err.Error())
|
||||||
|
s.observer.Logger(ctx).Error("get product by sku failed", zap.String("sku_id", req.GetSkuId()), zap.Error(err))
|
||||||
|
return nil, status.Errorf(codes.Internal, "get product by sku: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &productv1.GetProductResponse{
|
||||||
|
Product: toProtoProduct(product, skus),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// BatchGetCartItemProduct returns current SPU/SKU facts for cart rows in request order.
|
// BatchGetCartItemProduct returns current SPU/SKU facts for cart rows in request order.
|
||||||
// Missing and soft-deleted records are represented by their default invalid state rather
|
// Missing and soft-deleted records are represented by their default invalid state rather
|
||||||
// than failing the whole batch; malformed requests still return InvalidArgument.
|
// than failing the whole batch; malformed requests still return InvalidArgument.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user