feat: added API gateway
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package grpc_client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pb "datarush/pkg/api/competition"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type CompetitionClient struct {
|
||||
client pb.CompetitionServiceClient
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewCompetitionClient(ctx context.Context, address string, factory *ClientFactory) (*CompetitionClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create competition client: %w", err)
|
||||
}
|
||||
|
||||
return &CompetitionClient{
|
||||
client: pb.NewCompetitionServiceClient(conn),
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) CreateCompetition(
|
||||
ctx context.Context,
|
||||
competition *pb.Competition,
|
||||
) (*pb.Competition, error) {
|
||||
resp, err := c.client.CreateCompetition(ctx, competition)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create competition failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) GetCompetition(ctx context.Context, competitionID string) (*pb.Competition, error) {
|
||||
req := &pb.GetCompetitionRequest{
|
||||
CompetitionId: competitionID,
|
||||
}
|
||||
|
||||
resp, err := c.client.GetCompetition(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get competition failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) EditCompetition(ctx context.Context, competition *pb.Competition) (*pb.Competition, error) {
|
||||
resp, err := c.client.EditCompetition(ctx, competition)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("edit competition failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) DeleteCompetition(ctx context.Context, competitionID string) error {
|
||||
req := &pb.DeleteCompetitionRequest{
|
||||
CompetitionId: competitionID,
|
||||
}
|
||||
|
||||
_, err := c.client.DeleteCompetition(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete competition failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) ListCompetitions(
|
||||
ctx context.Context,
|
||||
req *pb.ListCompetitionsRequest,
|
||||
) (*pb.ListCompetitionsResponse, error) {
|
||||
resp, err := c.client.ListCompetitions(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list competitions failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *CompetitionClient) ChangeCompetitionState(
|
||||
ctx context.Context,
|
||||
competitionID string,
|
||||
state pb.CompetitionState,
|
||||
) (*pb.Competition, error) {
|
||||
req := &pb.ChangeCompetitionStateRequest{
|
||||
CompetitionId: competitionID,
|
||||
State: state,
|
||||
}
|
||||
|
||||
resp, err := c.client.ChangeCompetitionState(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("change competition state failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user