Go workspace (go.work) with 5 microservices + shared library: - gateway (8080), auth-service (8081), purchaser-service (8082) - textile-service (8083), washing-service (8084) - shared: proto definitions, common packages Infrastructure: docker-compose for local dev, K8s manifests for K3s cluster deployment (mysql/redis/etcd + traefik ingress). Frontend (iloom-flatten) added as git submodule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
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
|
|
}
|