feat(postgres):lab4 add loadAttachmentIDs helper
This commit is contained in:
@@ -108,6 +108,9 @@ func (r *MessageRepo) CreateIdempotent(
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4) Слот наш — валидируем вложения, вставляем сообщение, линкуем
|
||||||
|
// вложения. Всё в одной транзакции: при ошибке на любом шаге
|
||||||
|
// откатывается и idempotency-ключ, и сообщение, и связи.
|
||||||
if err := r.validateAttachments(ctx, tx, fresh.ChatID, fresh.AttachmentIDs); err != nil {
|
if err := r.validateAttachments(ctx, tx, fresh.ChatID, fresh.AttachmentIDs); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -269,4 +272,31 @@ func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *MessageRepo) loadAttachmentIDs(ctx context.Context, messageIDs []string) (map[string][]string, error) {
|
||||||
|
if len(messageIDs) == 0 {
|
||||||
|
return map[string][]string{}, nil
|
||||||
|
}
|
||||||
|
const q = `
|
||||||
|
SELECT message_id, attachment_id
|
||||||
|
FROM message_attachments
|
||||||
|
WHERE message_id = ANY($1::uuid[])
|
||||||
|
ORDER BY message_id, position
|
||||||
|
`
|
||||||
|
rows, err := r.pool.Query(ctx, q, messageIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, mapError(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
result := make(map[string][]string, len(messageIDs))
|
||||||
|
for rows.Next() {
|
||||||
|
var msgID, attID string
|
||||||
|
if err := rows.Scan(&msgID, &attID); err != nil {
|
||||||
|
return nil, mapError(err)
|
||||||
|
}
|
||||||
|
result[msgID] = append(result[msgID], attID)
|
||||||
|
}
|
||||||
|
return result, mapError(rows.Err())
|
||||||
|
}
|
||||||
|
|
||||||
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user