forked from mtusi2026/mtusi2026lab4
refactor: isolate attachment lifecycle
This commit is contained in:
@@ -97,11 +97,20 @@ func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, atta
|
||||
return true, nil
|
||||
}
|
||||
const query = `
|
||||
SELECT count(*)
|
||||
SELECT id
|
||||
FROM attachments
|
||||
WHERE chat_id = $1 AND id::text = ANY($2)`
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, query, chatID, attachmentIDs).Scan(&count); err != nil {
|
||||
WHERE chat_id = $1 AND id::text = ANY($2) AND status = 'ready'
|
||||
FOR UPDATE`
|
||||
rows, err := tx.Query(ctx, query, chatID, attachmentIDs)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("validate message attachments: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
count := 0
|
||||
for rows.Next() {
|
||||
count++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return false, fmt.Errorf("validate message attachments: %w", err)
|
||||
}
|
||||
return count == len(attachmentIDs), nil
|
||||
|
||||
Reference in New Issue
Block a user