55 lines
959 B
Go
55 lines
959 B
Go
package queue
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"gotemplate/internal/document"
|
|
"gotemplate/internal/queue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DocumentController struct {
|
|
document document.Service
|
|
}
|
|
|
|
func NewDocumentController(svc document.Service) *DocumentController {
|
|
return &DocumentController{
|
|
document: svc,
|
|
}
|
|
}
|
|
|
|
type DocumentQueryEvent struct {
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (s *DocumentController) Sync(ctx context.Context, config *queue.QueueConfig, msg *types.Message) error {
|
|
var body document.Document
|
|
err := json.Unmarshal([]byte(*msg.Body), &body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.document.Sync(ctx, body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
queryEvent := DocumentQueryEvent{
|
|
ID: body.ID,
|
|
}
|
|
|
|
err = queue.Send(ctx, config, "DOCQUERY", queryEvent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = queue.Delete(ctx, config, msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|