From 17f8a65e87d8e1269cec58467ef3356e4e51a5a4 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 11:48:30 +0300 Subject: [PATCH] feat(postgres):lab4 add AttachmentRepo scaffold and Create --- .../internal/storage/postgres/attachments.go | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/internal/storage/postgres/attachments.go diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go new file mode 100644 index 0000000..b3098e2 --- /dev/null +++ b/backend/internal/storage/postgres/attachments.go @@ -0,0 +1,51 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +// pending -> ready (успешный upload) +// pending -> failed (отказ upload или reconciler) +type AttachmentRepo struct { + pool *pgxpool.Pool +} + +func NewAttachmentRepo(pool *pgxpool.Pool) *AttachmentRepo { + return &AttachmentRepo{pool: pool} +} + +func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error { + const q = ` + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, $10, $10)` + _, err := r.pool.Exec(ctx, q, + a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, + a.SHA256, a.ObjectKey, string(a.Status), a.CreatedAt, + ) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "attachment", + Field: "object_key", + Value: a.ObjectKey, + } + } + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "chat", ID: a.ChatID} + } + if checkViolation(err) { + return &domain.ValidationError{ + Field: "attachment", + Message: "нарушено ограничение целостности", + } + } + return mapError(err) + } + return nil +}