diff --git a/backend/internal/storage/postgres/message.go b/backend/internal/storage/postgres/message.go index 42567290..fdcabe63 100644 --- a/backend/internal/storage/postgres/message.go +++ b/backend/internal/storage/postgres/message.go @@ -240,4 +240,26 @@ func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID return nil } +func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID string, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)` + for i, id := range ids { + if _, err := tx.Exec(ctx, q, messageID, id, int16(i)); err != nil { + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "attachment", ID: id} + } + if uniqueViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "дубликаты запрещены"} + } + if checkViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "нарушено ограничение"} + } + return mapError(err) + } + } + return nil +} + var _ domain.MessageRepository = (*MessageRepo)(nil)