feat(postgres):lab4 add AttachmentRepo.MarkReady and MarkFailed
This commit is contained in:
@@ -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: "некорректное состояние вложения"}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user