feat: add favorites table, CRUD commands, and Pinia store

This commit is contained in:
Your Name
2026-02-18 02:02:57 +02:00
parent 68ce724980
commit 6892bf8b98
4 changed files with 132 additions and 0 deletions

View File

@@ -677,3 +677,61 @@ pub fn get_project_budget_status(state: State<AppState>, project_id: i64) -> Res
"percent_amount": project_row.1.map(|b| if b > 0.0 { (amount_used / b) * 100.0 } else { 0.0 })
}))
}
// Favorite structs and commands
#[derive(Debug, Serialize, Deserialize)]
pub struct Favorite {
pub id: Option<i64>,
pub project_id: i64,
pub task_id: Option<i64>,
pub description: Option<String>,
pub sort_order: i32,
}
#[tauri::command]
pub fn get_favorites(state: State<AppState>) -> Result<Vec<Favorite>, String> {
let conn = state.db.lock().map_err(|e| e.to_string())?;
let mut stmt = conn.prepare(
"SELECT id, project_id, task_id, description, sort_order FROM favorites ORDER BY sort_order"
).map_err(|e| e.to_string())?;
let favs = stmt.query_map([], |row| {
Ok(Favorite {
id: Some(row.get(0)?),
project_id: row.get(1)?,
task_id: row.get(2)?,
description: row.get(3)?,
sort_order: row.get(4)?,
})
}).map_err(|e| e.to_string())?;
favs.collect::<Result<Vec<_>, _>>().map_err(|e| e.to_string())
}
#[tauri::command]
pub fn create_favorite(state: State<AppState>, fav: Favorite) -> Result<i64, String> {
let conn = state.db.lock().map_err(|e| e.to_string())?;
conn.execute(
"INSERT INTO favorites (project_id, task_id, description, sort_order) VALUES (?1, ?2, ?3, ?4)",
params![fav.project_id, fav.task_id, fav.description, fav.sort_order],
).map_err(|e| e.to_string())?;
Ok(conn.last_insert_rowid())
}
#[tauri::command]
pub fn delete_favorite(state: State<AppState>, id: i64) -> Result<(), String> {
let conn = state.db.lock().map_err(|e| e.to_string())?;
conn.execute("DELETE FROM favorites WHERE id = ?1", params![id])
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub fn reorder_favorites(state: State<AppState>, ids: Vec<i64>) -> Result<(), String> {
let conn = state.db.lock().map_err(|e| e.to_string())?;
for (i, id) in ids.iter().enumerate() {
conn.execute(
"UPDATE favorites SET sort_order = ?1 WHERE id = ?2",
params![i as i32, id],
).map_err(|e| e.to_string())?;
}
Ok(())
}