From 936747fe7618bbdd6387b3ca1104e61d6aef7309 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: Mon, 19 Oct 2026 19:04:11 +0300 Subject: [PATCH] feat(postgres):lab4 add linkAttachments helper --- backend/internal/storage/postgres/message.go | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) 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)