From 2652f262713c441658bb8d312a04f4e20c4f99e0 Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Thu, 12 Dec 2024 17:10:18 +1000 Subject: [PATCH] fix: same item inserted multiple times --- src-tauri/src/db/history.rs | 81 +++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/src-tauri/src/db/history.rs b/src-tauri/src/db/history.rs index 1ac3c70..6c2a572 100644 --- a/src-tauri/src/db/history.rs +++ b/src-tauri/src/db/history.rs @@ -1,9 +1,9 @@ -use sqlx::{Row, SqlitePool}; +use crate::utils::types::{ContentType, HistoryItem}; +use base64::{engine::general_purpose::STANDARD, Engine}; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; -use crate::utils::types::{HistoryItem, ContentType}; +use sqlx::{Row, SqlitePool}; use std::fs; -use base64::{Engine, engine::general_purpose::STANDARD}; pub async fn initialize_history(pool: &SqlitePool) -> Result<(), Box> { let id: String = thread_rng() @@ -27,19 +27,22 @@ pub async fn initialize_history(pool: &SqlitePool) -> Result<(), Box) -> Result, String> { let rows = sqlx::query( - "SELECT id, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC" + "SELECT id, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC", ) .fetch_all(&*pool) .await .map_err(|e| e.to_string())?; - let items = rows.iter().map(|row| HistoryItem { - id: row.get("id"), - content_type: ContentType::from(row.get::("content_type")), - content: row.get("content"), - favicon: row.get("favicon"), - timestamp: row.get("timestamp"), - }).collect(); + let items = rows + .iter() + .map(|row| HistoryItem { + id: row.get("id"), + content_type: ContentType::from(row.get::("content_type")), + content: row.get("content"), + favicon: row.get("favicon"), + timestamp: row.get("timestamp"), + }) + .collect(); Ok(items) } @@ -50,7 +53,19 @@ pub async fn add_history_item( item: HistoryItem, ) -> Result<(), String> { let (id, content_type, content, favicon, timestamp) = 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); + + if last_content.as_deref() == Some(&content) { + return Ok(()); + } + sqlx::query( "INSERT INTO history (id, content_type, content, favicon, timestamp) VALUES (?, ?, ?, ?, ?)" ) @@ -69,7 +84,7 @@ pub async fn add_history_item( #[tauri::command] pub async fn search_history( pool: tauri::State<'_, SqlitePool>, - query: String + query: String, ) -> Result, String> { let query = format!("%{}%", query); let rows = sqlx::query( @@ -80,13 +95,16 @@ pub async fn search_history( .await .map_err(|e| e.to_string())?; - let items = rows.iter().map(|row| HistoryItem { - id: row.get("id"), - content_type: ContentType::from(row.get::("content_type")), - content: row.get("content"), - favicon: row.get("favicon"), - timestamp: row.get("timestamp"), - }).collect(); + let items = rows + .iter() + .map(|row| HistoryItem { + id: row.get("id"), + content_type: ContentType::from(row.get::("content_type")), + content: row.get("content"), + favicon: row.get("favicon"), + timestamp: row.get("timestamp"), + }) + .collect(); Ok(items) } @@ -95,7 +113,7 @@ pub async fn search_history( pub async fn load_history_chunk( pool: tauri::State<'_, SqlitePool>, offset: i64, - limit: i64 + limit: i64, ) -> Result, String> { let rows = sqlx::query( "SELECT id, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC LIMIT ? OFFSET ?" @@ -106,13 +124,16 @@ pub async fn load_history_chunk( .await .map_err(|e| e.to_string())?; - let items = rows.iter().map(|row| HistoryItem { - id: row.get("id"), - content_type: ContentType::from(row.get::("content_type")), - content: row.get("content"), - favicon: row.get("favicon"), - timestamp: row.get("timestamp"), - }).collect(); + let items = rows + .iter() + .map(|row| HistoryItem { + id: row.get("id"), + content_type: ContentType::from(row.get::("content_type")), + content: row.get("content"), + favicon: row.get("favicon"), + timestamp: row.get("timestamp"), + }) + .collect(); Ok(items) } @@ -120,7 +141,7 @@ pub async fn load_history_chunk( #[tauri::command] pub async fn delete_history_item( pool: tauri::State<'_, SqlitePool>, - id: String + id: String, ) -> Result<(), String> { sqlx::query("DELETE FROM history WHERE id = ?") .bind(id) @@ -145,4 +166,4 @@ pub async fn clear_history(pool: tauri::State<'_, SqlitePool>) -> Result<(), Str pub async fn read_image(filename: String) -> Result { let bytes = fs::read(filename).map_err(|e| e.to_string())?; Ok(STANDARD.encode(bytes)) -} \ No newline at end of file +}