mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"go.uber.org/zap"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
|
|
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
|
)
|
|
|
|
// ProductServer implements productv1.ProductServiceServer.
|
|
type ProductServer struct {
|
|
productv1.UnimplementedProductServiceServer
|
|
|
|
repo *repository.ProductRepository
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// NewProductServer constructs the gRPC product service handler.
|
|
func NewProductServer(repo *repository.ProductRepository, logger *zap.Logger) *ProductServer {
|
|
if logger == nil {
|
|
logger = zap.NewNop()
|
|
}
|
|
return &ProductServer{
|
|
repo: repo,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// GetProduct returns a single SPU and its SKU list.
|
|
func (s *ProductServer) GetProduct(ctx context.Context, req *productv1.GetProductRequest) (*productv1.GetProductResponse, error) {
|
|
if req == nil || req.GetProductId() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "product_id is required")
|
|
}
|
|
|
|
product, skus, err := s.repo.GetProduct(ctx, req.GetProductId())
|
|
if errors.Is(err, repository.ErrProductNotFound) {
|
|
return nil, status.Error(codes.NotFound, "product not found")
|
|
}
|
|
if err != nil {
|
|
s.logger.Error("get product failed", zap.String("product_id", req.GetProductId()), zap.Error(err))
|
|
return nil, status.Errorf(codes.Internal, "get product: %v", err)
|
|
}
|
|
|
|
return &productv1.GetProductResponse{
|
|
Product: toProtoProduct(product, skus),
|
|
}, nil
|
|
}
|
|
|
|
// BatchGetProducts returns existing products for the requested IDs; missing IDs are omitted.
|
|
func (s *ProductServer) BatchGetProducts(ctx context.Context, req *productv1.BatchGetProductsRequest) (*productv1.BatchGetProductsResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "request is required")
|
|
}
|
|
|
|
products, skuMap, err := s.repo.BatchGetProducts(ctx, req.GetProductIds())
|
|
if err != nil {
|
|
s.logger.Error("batch get products failed", zap.Strings("product_ids", req.GetProductIds()), zap.Error(err))
|
|
return nil, status.Errorf(codes.Internal, "batch get products: %v", err)
|
|
}
|
|
|
|
protoProducts := make([]*productv1.Product, 0, len(products))
|
|
for _, product := range products {
|
|
protoProducts = append(protoProducts, toProtoProduct(product, skuMap[product.ID]))
|
|
}
|
|
|
|
return &productv1.BatchGetProductsResponse{
|
|
Products: protoProducts,
|
|
}, nil
|
|
}
|
|
|
|
func toProtoProduct(product *repository.ProductRecord, skus []repository.SKURecord) *productv1.Product {
|
|
if product == nil {
|
|
return nil
|
|
}
|
|
|
|
protoSKUs := make([]*productv1.SKU, 0, len(skus))
|
|
for _, sku := range skus {
|
|
protoSKUs = append(protoSKUs, &productv1.SKU{
|
|
SkuId: sku.ID,
|
|
ProductId: sku.ProductID,
|
|
Name: sku.Name,
|
|
Price: sku.Price,
|
|
Stock: sku.Stock,
|
|
AttributesJsonb: sku.AttributesJSONB,
|
|
})
|
|
}
|
|
|
|
return &productv1.Product{
|
|
ProductId: product.ID,
|
|
Name: product.Name,
|
|
Description: product.Description,
|
|
Status: product.Status,
|
|
Skus: protoSKUs,
|
|
CreatedAt: timestamppb.New(product.CreatedAt),
|
|
UpdatedAt: timestamppb.New(product.UpdatedAt),
|
|
}
|
|
}
|