From d22d493b1a3517bf03e4ca19fd02881192740671 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 15:44:08 +0300 Subject: [PATCH] feat(postgres):lab4 add AttachmentRepo.ExistingKeys and interface assertion --- .../internal/storage/postgres/attachments.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 1ddd928..57c2efd 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -6,6 +6,7 @@ import ( "github.com/jackc/pgx/v5/pgxpool" "high-loud-apps-labs/internal/domain" + "time" ) // pending -> ready (успешный upload) @@ -132,3 +133,44 @@ func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) e return &domain.ConflictError{Reason: "некорректное состояние вложения"} } } + +func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) { + if limit <= 0 { + return nil, nil + } + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at + FROM attachments + WHERE status = 'pending' AND created_at < $1 + ORDER BY created_at + LIMIT $2 + ` + rows, err := r.pool.Query(ctx, q, olderThan, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + out := make([]domain.Attachment, 0, limit) + for rows.Next() { + var a domain.Attachment + var status string + if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, + &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, + &status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil { + return nil, mapError(err) + } + a.Status = domain.AttachmentStatus(status) + out = append(out, a) + } + return out, mapError(rows.Err()) +} + +func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error { + const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'` + if _, err := r.pool.Exec(ctx, q, id); err != nil { + return mapError(err) + } + return nil +}