From 8fe9e0a962286c6b9edf21d054947bd029d168a6 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 13:53:08 +0300 Subject: [PATCH] feat(postgres):lab4 add AttachmentRepo.MarkReady and MarkFailed --- .../internal/storage/postgres/attachments.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go index 9ea3180..1ddd928 100644 --- a/backend/internal/storage/postgres/attachments.go +++ b/backend/internal/storage/postgres/attachments.go @@ -69,3 +69,66 @@ func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attach a.Status = domain.AttachmentStatus(status) return a, nil } + +// MarkReady переводит pending -> ready. Идемпотентна. +func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { + const q = ` + UPDATE attachments + SET status = 'ready', updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusReady: + return nil // idempotent no-op + case domain.AttachmentStatusFailed: + return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"} + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +} + +// MarkFailed переводит pending -> failed. Идемпотентна. +func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error { + if errorCode == "" { + return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"} + } + const q = ` + UPDATE attachments + SET status = 'failed', error_code = $2, updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id, errorCode) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusFailed, domain.AttachmentStatusReady: + return nil + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +}