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

@@ -20,6 +20,7 @@
pkgs.gtk3
pkgs.webkitgtk_4_1
pkgs.zed-editor
pkgs.openssl_3
# pkgs.webkitgtk # Linux Tauri dependency (Webview2 is the one used on Widnows)
];

View File

@@ -2,6 +2,9 @@
use sqlx::{migrate::MigrateDatabase, sqlite::SqlitePoolOptions, Pool, Sqlite};
use tauri::{App, Manager};
mod repositories;
mod models;
// #[tauri::command]
// fn greet(name: &str) -> String {
// format!("Hello, {}! You've been greeted from Rust!", name)
@@ -69,7 +72,7 @@ pub fn run() {
])
.setup(|app| {
tauri::async_runtime::block_on(async move {
let db = setup_db(&app).await;
let db = setup_db(app).await;
app.manage(AppState { db });
});

View File

@@ -0,0 +1,6 @@
pub struct Exercise {
id: u16,
ex_type: String,
content: String,
node_id: u32
}

View File

@@ -0,0 +1,3 @@
pub mod path;
pub mod node;
pub mod exercise;

View File

@@ -0,0 +1,6 @@
pub struct Node {
id: u32,
title: String,
description: String,
path_id: String
}

View File

@@ -0,0 +1,12 @@
#[derive(Debug, sqlx::FromRow)]
pub struct Path {
id: String,
title: String,
description: String,
}
pub struct PathVersions {
path_id: String,
version_number: String,
}

View File

@@ -0,0 +1 @@
pub mod path_repository;

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)
}
}

View File