57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"high-loud-apps-labs/internal/domain"
|
|
)
|
|
|
|
type OutboxRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewOutboxRepo(pool *pgxpool.Pool) *OutboxRepo {
|
|
return &OutboxRepo{pool: pool}
|
|
}
|
|
|
|
func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.OutboxEntry) error {
|
|
if e.AggregateType == "" || e.AggregateID == "" || e.EventType == "" {
|
|
return &domain.ValidationError{
|
|
Field: "outbox",
|
|
Message: "aggregate_type, aggregate_id и event_type обязательны",
|
|
}
|
|
}
|
|
if len(e.Payload) == 0 {
|
|
return &domain.ValidationError{
|
|
Field: "outbox.payload",
|
|
Message: "не может быть пустым",
|
|
}
|
|
}
|
|
|
|
if e.ID == "" {
|
|
e.ID = uuid.NewString()
|
|
}
|
|
if e.CreatedAt.IsZero() {
|
|
e.CreatedAt = time.Now().UTC()
|
|
}
|
|
|
|
const q = `
|
|
INSERT INTO outbox
|
|
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at)
|
|
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8)
|
|
`
|
|
if err := tx.Exec(ctx, q,
|
|
e.ID, e.AggregateType, e.AggregateID, e.EventType,
|
|
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var _ domain.OutboxRepository = (*OutboxRepo)(nil)
|