Files
flalingo/src-tauri/src/repositories/path_repository.rs
2025-10-28 20:34:10 +01:00

44 lines
1.3 KiB
Rust

use sqlx::{sqlite::SqlitePool, FromRow};
use crate::models::path::Path;
pub struct PathRepository<'a> {
pub pool: &'a SqlitePool,
}
impl<'a> PathRepository<'a> {
pub async fn get_path_by_id(&self, id: i32) -> Result<Path, String> {
let result = sqlx::query("SELECT * FROM path WHERE id = ?")
.bind(id)
.fetch_all(self.pool)
.await
.map_err(|e| format!("ERROR: Failed to query Path db: {} ", e))
.unwrap();
if result.len() > 1 {
return Err(format!("ERROR: Multiple paths for ID {} found", id));
} else if result.is_empty(){
return Err(format!("ERROR: No Path with ID {} found", id));
}
let path = match result.first() {
Some(p) => match Path::from_row(p) {
Ok(p) => {p},
Err(e) => {return Err(format!("ERROR: Could not parse Path: {}", e));},
},
None => return Err(format!("ERROR: No path for ID {} found", id)),
};
Ok(path)
}
pub async fn get_all_paths(&self) -> Result<Vec<Path>, String> {
let rows = sqlx::query_as::<_, Path>("SELECT * FROM paths")
.fetch_all(self.pool)
.await
.map_err(|e| e.to_string())?;
Ok(rows)
}
}