package grpc import ( "context" "fmt" "google.golang.org/grpc" pb "github.com/muyuqingfeng/iloom/shared/pkg/grpc/pb/inventory" ) // InventoryClient wraps the generated InventoryServiceClient with convenience methods. type InventoryClient struct { conn *grpc.ClientConn Raw pb.InventoryServiceClient } // NewInventoryClient creates an InventoryClient connected to the given address. func NewInventoryClient(addr string) (*InventoryClient, error) { conn, err := Dial(addr) if err != nil { return nil, err } return &InventoryClient{ conn: conn, Raw: pb.NewInventoryServiceClient(conn), }, nil } // Close closes the underlying gRPC connection. func (c *InventoryClient) Close() error { return c.conn.Close() } // CreateProduct creates a new product and returns the product ID. func (c *InventoryClient) CreateProduct(ctx context.Context, req *pb.CreateProductReq) (string, error) { resp, err := c.Raw.CreateProduct(ctx, req) if err != nil { return "", fmt.Errorf("inventory.CreateProduct: %w", err) } return resp.Id, nil } // GetProduct returns product info by product ID. func (c *InventoryClient) GetProduct(ctx context.Context, productID string) (*pb.ProductInfo, error) { resp, err := c.Raw.GetProduct(ctx, &pb.GetProductReq{ProductId: productID}) if err != nil { return nil, fmt.Errorf("inventory.GetProduct: %w", err) } return resp, nil } // ListProduct returns a paginated list of products with optional filters. func (c *InventoryClient) ListProduct(ctx context.Context, page, pageSize int64, productName string) ([]*pb.ProductInfo, int64, error) { resp, err := c.Raw.ListProduct(ctx, &pb.ListProductReq{ Page: page, PageSize: pageSize, ProductName: productName, }) if err != nil { return nil, 0, fmt.Errorf("inventory.ListProduct: %w", err) } return resp.List, resp.Total, nil }