feat(postgres):lab4 add validateAttachments helper

This commit is contained in:
Голиков Maximillian Антон
2026-10-19 18:23:00 +03:00
parent 54eca57152
commit 9becd7ddf0
@@ -37,6 +37,7 @@ func (r *MessageRepo) Create(ctx context.Context, m *domain.Message) error {
}
return nil
}
func (r *MessageRepo) CreateIdempotent(
ctx context.Context,
scope domain.IdempotencyScope,
@@ -200,4 +201,43 @@ func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int,
return page, next, nil
}
func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID string, ids []string) error {
if len(ids) == 0 {
return nil
}
const q = `SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])`
rows, err := tx.Query(ctx, q, ids)
if err != nil {
return mapError(err)
}
defer rows.Close()
type info struct {
chatID string
status domain.AttachmentStatus
}
found := make(map[string]info, len(ids))
for rows.Next() {
var id, chID, st string
if err := rows.Scan(&id, &chID, &st); err != nil {
return mapError(err)
}
found[id] = info{chatID: chID, status: domain.AttachmentStatus(st)}
}
if err := rows.Err(); err != nil {
return mapError(err)
}
for _, id := range ids {
a, ok := found[id]
if !ok || a.chatID != chatID {
return &domain.NotFoundError{Entity: "attachment", ID: id}
}
if a.status != domain.AttachmentStatusReady {
return &domain.ConflictError{Reason: "вложение " + id + " не готово"}
}
}
return nil
}
var _ domain.MessageRepository = (*MessageRepo)(nil)