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 { 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, String> { let rows = sqlx::query_as::<_, Path>("SELECT * FROM paths") .fetch_all(self.pool) .await .map_err(|e| e.to_string())?; Ok(rows) } }