implemented get_path_by_id and get_all_paths

This commit is contained in:
2025-10-27 21:30:57 +01:00
parent 8c9b735d75
commit 2d6620faa6
9 changed files with 68 additions and 4 deletions

View File

@@ -1,11 +1,43 @@
use sqlx::sqlite::{SqlitePool};
use sqlx::{pool, sqlite::SqlitePool, FromRow};
use crate::models::path::Path;
pub struct PathRepository<'a> {
pub pool: &'a SqlitePool,
}
impl<'a> PathRepository<'a> {
pub fn get_path_by_id(&self, id: i32) -> Result<Path>{
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)
}
}