83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package grpc_client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
pb "datarush/pkg/api/task"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type TaskClient struct {
|
|
client pb.TaskServiceClient
|
|
conn *grpc.ClientConn
|
|
}
|
|
|
|
func NewTaskClient(ctx context.Context, address string, factory *ClientFactory) (*TaskClient, error) {
|
|
conn, err := factory.GetConnection(ctx, address)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create task client: %w", err)
|
|
}
|
|
return &TaskClient{client: pb.NewTaskServiceClient(conn), conn: conn}, nil
|
|
}
|
|
|
|
func (c *TaskClient) CreateTask(ctx context.Context, task *pb.Task) (*pb.Task, error) {
|
|
resp, err := c.client.CreateTask(ctx, task)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create task failed: %w", err)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (c *TaskClient) GetTask(ctx context.Context, taskID string) (*pb.Task, error) {
|
|
req := &pb.GetTaskRequest{TaskId: taskID}
|
|
resp, err := c.client.GetTask(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get task failed: %w", err)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (c *TaskClient) EditTask(ctx context.Context, task *pb.Task) (*pb.Task, error) {
|
|
resp, err := c.client.EditTask(ctx, task)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("edit task failed: %w", err)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (c *TaskClient) DeleteTask(ctx context.Context, taskID string) error {
|
|
req := &pb.DeleteTaskRequest{TaskId: taskID}
|
|
_, err := c.client.DeleteTask(ctx, req)
|
|
if err != nil {
|
|
return fmt.Errorf("delete task failed: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *TaskClient) ListCompetitionTasks(ctx context.Context, competitionID string) ([]*pb.Task, error) {
|
|
req := &pb.ListCompetitionTasksRequest{CompetitionId: competitionID}
|
|
resp, err := c.client.ListCompetitionTasks(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list tasks failed: %w", err)
|
|
}
|
|
return resp.Tasks, nil
|
|
}
|
|
|
|
func (c *TaskClient) GetTaskAttachments(
|
|
ctx context.Context,
|
|
taskID string,
|
|
showPrivate bool,
|
|
) ([]*pb.TaskAttachment, error) {
|
|
req := &pb.GetTaskAttachmentsRequest{
|
|
TaskId: taskID,
|
|
ShowPrivate: &showPrivate,
|
|
}
|
|
resp, err := c.client.GetTaskAttachments(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get task attachments failed: %w", err)
|
|
}
|
|
return resp.Attachments, nil
|
|
}
|