feat(storage): lab5 implement outbox enqueue and pgx transaction adapter
This commit is contained in:
@@ -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)
|
||||||
@@ -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) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} }
|
||||||
func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{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) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} }
|
||||||
|
func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} }
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user