From 460888a1e72cd8373905e8e3ce98bd87e1b37147 Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Wed, 18 Dec 2024 14:15:34 +1000 Subject: [PATCH] feat: move item to the top if copied again --- src-tauri/src/db/history.rs | 57 +++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/db/history.rs b/src-tauri/src/db/history.rs index 67895e5..fc24bcb 100644 --- a/src-tauri/src/db/history.rs +++ b/src-tauri/src/db/history.rs @@ -59,33 +59,42 @@ pub async fn add_history_item( let (id, source, source_icon, content_type, content, favicon, timestamp, language) = item.to_row(); - let last_content: Option = sqlx::query_scalar( - "SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1", - ) - .bind(content_type.clone()) - .fetch_one(&*pool) - .await - .unwrap_or(None); + let existing = sqlx::query("SELECT id FROM history WHERE content = ? AND content_type = ?") + .bind(&content) + .bind(&content_type) + .fetch_optional(&*pool) + .await + .map_err(|e| e.to_string())?; - if last_content.as_deref() == Some(&content) { - return Ok(()); + match existing { + Some(_) => { + sqlx::query( + "UPDATE history SET timestamp = strftime('%Y-%m-%dT%H:%M:%f+00:00', 'now') WHERE content = ? AND content_type = ?" + ) + .bind(&content) + .bind(&content_type) + .execute(&*pool) + .await + .map_err(|e| e.to_string())?; + } + None => { + sqlx::query( + "INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp, language) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + ) + .bind(id) + .bind(source) + .bind(source_icon) + .bind(content_type) + .bind(content) + .bind(favicon) + .bind(timestamp) + .bind(language) + .execute(&*pool) + .await + .map_err(|e| e.to_string())?; + } } - sqlx::query( - "INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp, language) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" - ) - .bind(id) - .bind(source) - .bind(source_icon) - .bind(content_type) - .bind(content) - .bind(favicon) - .bind(timestamp) - .bind(language) - .execute(&*pool) - .await - .map_err(|e| e.to_string())?; - Ok(()) }