Files
query-orchestration/api/queryRunner/queryrunner.go
T

65 lines
1.2 KiB
Go
Raw Normal View History

2025-01-10 11:12:03 +00:00
package controllers
2024-12-18 18:54:48 +00:00
import (
"context"
"encoding/json"
"queryorchestration/internal/query/document"
"queryorchestration/internal/server/queue"
2024-12-18 18:54:48 +00:00
2024-12-23 16:49:53 +00:00
"github.com/go-playground/validator/v10"
2025-01-10 11:12:03 +00:00
"github.com/aws/aws-sdk-go-v2/aws"
2024-12-18 18:54:48 +00:00
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/google/uuid"
2024-12-18 18:54:48 +00:00
)
2025-01-10 11:12:03 +00:00
type QueryRunner struct {
2024-12-23 16:49:53 +00:00
validator *validator.Validate
2025-01-10 11:12:03 +00:00
document *document.Service
2024-12-18 18:54:48 +00:00
}
2025-01-10 11:12:03 +00:00
func NewQueryRunner(svc *document.Service, validator *validator.Validate) QueryRunner {
return QueryRunner{
2024-12-23 16:49:53 +00:00
validator: validator,
document: svc,
2024-12-18 18:54:48 +00:00
}
}
type DocumentQueryEvent struct {
ID uuid.UUID `json:"id"`
2024-12-18 18:54:48 +00:00
}
2025-01-10 11:12:03 +00:00
func (s QueryRunner) Process(ctx context.Context, config *queue.Config, msg *types.Message) error {
2024-12-18 18:54:48 +00:00
var body document.Document
err := json.Unmarshal([]byte(*msg.Body), &body)
if err != nil {
return err
}
2024-12-23 16:49:53 +00:00
err = s.validator.Struct(body)
if err != nil {
return err
}
2024-12-20 17:35:33 +00:00
err = s.document.Sync(ctx, &body)
2024-12-18 18:54:48 +00:00
if err != nil {
return err
}
queryEvent := DocumentQueryEvent{
ID: body.ID,
}
2025-01-10 11:12:03 +00:00
err = queue.Send(ctx, config, queryEvent, map[string]types.MessageAttributeValue{
"type": {
DataType: aws.String("String"),
StringValue: aws.String("DOCQUERY"),
},
})
2024-12-18 18:54:48 +00:00
if err != nil {
return err
}
return nil
}