mirror of
https://github.com/ByteAtATime/raycast-linux.git
synced 2025-08-31 11:17:27 +00:00
feat(snippets): implement backend crud operations
This commit creates the basic CRUD functions (`create`, `list`, `update`, `delete`) as Tauri commands.
This commit is contained in:
parent
1a2fa36a79
commit
d72b4dd450
3 changed files with 131 additions and 3 deletions
|
@ -230,7 +230,11 @@ pub fn run() {
|
|||
system::show_in_finder,
|
||||
system::trash,
|
||||
record_usage,
|
||||
get_frecency_data
|
||||
get_frecency_data,
|
||||
snippets::create_snippet,
|
||||
snippets::list_snippets,
|
||||
snippets::update_snippet,
|
||||
snippets::delete_snippet
|
||||
])
|
||||
.setup(|app| {
|
||||
let app_handle = app.handle().clone();
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use crate::error::AppError;
|
||||
use rusqlite::{Connection, Result as RusqliteResult};
|
||||
use crate::snippets::types::Snippet;
|
||||
use chrono::{DateTime, Utc};
|
||||
use rusqlite::{params, Connection, Result as RusqliteResult};
|
||||
use std::sync::Mutex;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
|
@ -33,4 +35,84 @@ impl SnippetManager {
|
|||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn create_snippet(
|
||||
&self,
|
||||
name: String,
|
||||
keyword: String,
|
||||
content: String,
|
||||
) -> Result<i64, AppError> {
|
||||
let db = self.db.lock().unwrap();
|
||||
let now = Utc::now().timestamp();
|
||||
db.execute(
|
||||
"INSERT INTO snippets (name, keyword, content, created_at, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?4)",
|
||||
params![name, keyword, content, now],
|
||||
)?;
|
||||
Ok(db.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn list_snippets(&self) -> Result<Vec<Snippet>, AppError> {
|
||||
let db = self.db.lock().unwrap();
|
||||
let mut stmt = db.prepare("SELECT id, name, keyword, content, created_at, updated_at FROM snippets ORDER BY name ASC")?;
|
||||
let snippets_iter = stmt.query_map([], |row| {
|
||||
let created_at_ts: i64 = row.get(4)?;
|
||||
let updated_at_ts: i64 = row.get(5)?;
|
||||
Ok(Snippet {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
keyword: row.get(2)?,
|
||||
content: row.get(3)?,
|
||||
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
|
||||
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
|
||||
})
|
||||
})?;
|
||||
|
||||
snippets_iter.collect::<Result<Vec<_>, _>>().map_err(|e| e.into())
|
||||
}
|
||||
|
||||
pub fn update_snippet(
|
||||
&self,
|
||||
id: i64,
|
||||
name: String,
|
||||
keyword: String,
|
||||
content: String,
|
||||
) -> Result<(), AppError> {
|
||||
let db = self.db.lock().unwrap();
|
||||
let now = Utc::now().timestamp();
|
||||
db.execute(
|
||||
"UPDATE snippets SET name = ?1, keyword = ?2, content = ?3, updated_at = ?4 WHERE id = ?5",
|
||||
params![name, keyword, content, now, id],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_snippet(&self, id: i64) -> Result<(), AppError> {
|
||||
let db = self.db.lock().unwrap();
|
||||
db.execute("DELETE FROM snippets WHERE id = ?1", params![id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn find_snippet_by_keyword(&self, keyword: &str) -> Result<Option<Snippet>, AppError> {
|
||||
let db = self.db.lock().unwrap();
|
||||
let mut stmt = db.prepare("SELECT id, name, keyword, content, created_at, updated_at FROM snippets WHERE keyword = ?1")?;
|
||||
let mut rows = stmt.query_map(params![keyword], |row| {
|
||||
let created_at_ts: i64 = row.get(4)?;
|
||||
let updated_at_ts: i64 = row.get(5)?;
|
||||
Ok(Snippet {
|
||||
id: row.get(0)?,
|
||||
name: row.get(1)?,
|
||||
keyword: row.get(2)?,
|
||||
content: row.get(3)?,
|
||||
created_at: DateTime::from_timestamp(created_at_ts, 0).unwrap_or_default(),
|
||||
updated_at: DateTime::from_timestamp(updated_at_ts, 0).unwrap_or_default(),
|
||||
})
|
||||
})?;
|
||||
|
||||
if let Some(row) = rows.next() {
|
||||
Ok(Some(row?))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,45 @@
|
|||
pub mod input_manager;
|
||||
pub mod manager;
|
||||
pub mod types;
|
||||
pub mod types;
|
||||
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use types::Snippet;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn create_snippet(
|
||||
app: AppHandle,
|
||||
name: String,
|
||||
keyword: String,
|
||||
content: String,
|
||||
) -> Result<i64, String> {
|
||||
app.state::<manager::SnippetManager>()
|
||||
.create_snippet(name, keyword, content)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn list_snippets(app: AppHandle) -> Result<Vec<Snippet>, String> {
|
||||
app.state::<manager::SnippetManager>()
|
||||
.list_snippets()
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn update_snippet(
|
||||
app: AppHandle,
|
||||
id: i64,
|
||||
name: String,
|
||||
keyword: String,
|
||||
content: String,
|
||||
) -> Result<(), String> {
|
||||
app.state::<manager::SnippetManager>()
|
||||
.update_snippet(id, name, keyword, content)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn delete_snippet(app: AppHandle, id: i64) -> Result<(), String> {
|
||||
app.state::<manager::SnippetManager>()
|
||||
.delete_snippet(id)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue