64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/geo-platform/tenant-api/internal/shared/messaging/rabbitmq"
|
||
|
|
)
|
||
|
|
|
||
|
|
type articleGenerationTaskEnvelope struct {
|
||
|
|
TaskID int64 `json:"task_id"`
|
||
|
|
TaskType string `json:"task_type"`
|
||
|
|
TenantID int64 `json:"tenant_id"`
|
||
|
|
ArticleID int64 `json:"article_id"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type ArticleGenerationTaskEnvelope struct {
|
||
|
|
TaskID int64 `json:"task_id"`
|
||
|
|
TaskType string `json:"task_type"`
|
||
|
|
TenantID int64 `json:"tenant_id"`
|
||
|
|
ArticleID int64 `json:"article_id"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func publishArticleGenerationTask(ctx context.Context, client *rabbitmq.Client, envelope articleGenerationTaskEnvelope) error {
|
||
|
|
if client == nil {
|
||
|
|
return fmt.Errorf("rabbitmq client is not configured")
|
||
|
|
}
|
||
|
|
|
||
|
|
payload, err := json.Marshal(envelope)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("marshal generation task envelope: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := client.PublishGenerationTask(ctx, payload); err != nil {
|
||
|
|
return fmt.Errorf("publish generation task: %w", err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func decodeArticleGenerationTaskEnvelope(payload []byte) (*articleGenerationTaskEnvelope, error) {
|
||
|
|
var envelope articleGenerationTaskEnvelope
|
||
|
|
if err := json.Unmarshal(payload, &envelope); err != nil {
|
||
|
|
return nil, fmt.Errorf("decode generation task envelope: %w", err)
|
||
|
|
}
|
||
|
|
if envelope.TaskID <= 0 {
|
||
|
|
return nil, fmt.Errorf("generation task envelope task_id is required")
|
||
|
|
}
|
||
|
|
return &envelope, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func DecodeArticleGenerationTaskEnvelope(payload []byte) (*ArticleGenerationTaskEnvelope, error) {
|
||
|
|
envelope, err := decodeArticleGenerationTaskEnvelope(payload)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &ArticleGenerationTaskEnvelope{
|
||
|
|
TaskID: envelope.TaskID,
|
||
|
|
TaskType: envelope.TaskType,
|
||
|
|
TenantID: envelope.TenantID,
|
||
|
|
ArticleID: envelope.ArticleID,
|
||
|
|
}, nil
|
||
|
|
}
|