From ab35077afdd345afff8bc676babba67b431718cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Sat, 7 Nov 2026 06:46:21 +0300 Subject: [PATCH] feat(storage): lab5 implement outbox enqueue and pgx transaction adapter --- backend/internal/storage/postgres/outbox.go | 56 ++++++++++++++++++++ backend/internal/storage/postgres/storage.go | 1 + backend/internal/storage/postgres/tx.go | 35 ++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 backend/internal/storage/postgres/outbox.go create mode 100644 backend/internal/storage/postgres/tx.go diff --git a/backend/internal/storage/postgres/outbox.go b/backend/internal/storage/postgres/outbox.go new file mode 100644 index 0000000..7d666e0 --- /dev/null +++ b/backend/internal/storage/postgres/outbox.go @@ -0,0 +1,56 @@ +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) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go index 0712109..c3c5abb 100644 --- a/backend/internal/storage/postgres/storage.go +++ b/backend/internal/storage/postgres/storage.go @@ -17,3 +17,4 @@ func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } +func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} } diff --git a/backend/internal/storage/postgres/tx.go b/backend/internal/storage/postgres/tx.go new file mode 100644 index 0000000..4d8e771 --- /dev/null +++ b/backend/internal/storage/postgres/tx.go @@ -0,0 +1,35 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5" + + "high-loud-apps-labs/internal/domain" +) + +type pgxTx struct { + tx pgx.Tx +} + +func (t *pgxTx) Exec(ctx context.Context, sql string, args ...any) error { + _, err := t.tx.Exec(ctx, sql, args...) + return mapError(err) +} + +// WithTx выполняет fn в одной транзакции: +// - commit, если fn вернул nil; +// - rollback, если fn вернул ошибку. + +func (s *Storage) WithTx(ctx context.Context, fn func(tx domain.Tx) error) error { + tx, err := s.pool.Begin(ctx) + if err != nil { + return mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + if err := fn(&pgxTx{tx: tx}); err != nil { + return err + } + return mapError(tx.Commit(ctx)) +}