feat(postgres):lab4 add AttachmentRepo.ExistingKeys and interface assertion

This commit is contained in:
Голиков Maximillian Антон
2026-10-19 15:44:08 +03:00
parent 8fe9e0a962
commit d22d493b1a
@@ -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
}