implemented get_path_by_id and get_all_paths
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
pkgs.gtk3
|
pkgs.gtk3
|
||||||
pkgs.webkitgtk_4_1
|
pkgs.webkitgtk_4_1
|
||||||
pkgs.zed-editor
|
pkgs.zed-editor
|
||||||
|
pkgs.openssl_3
|
||||||
# pkgs.webkitgtk # Linux Tauri dependency (Webview2 is the one used on Widnows)
|
# pkgs.webkitgtk # Linux Tauri dependency (Webview2 is the one used on Widnows)
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
use sqlx::{migrate::MigrateDatabase, sqlite::SqlitePoolOptions, Pool, Sqlite};
|
use sqlx::{migrate::MigrateDatabase, sqlite::SqlitePoolOptions, Pool, Sqlite};
|
||||||
use tauri::{App, Manager};
|
use tauri::{App, Manager};
|
||||||
|
|
||||||
|
mod repositories;
|
||||||
|
mod models;
|
||||||
|
|
||||||
// #[tauri::command]
|
// #[tauri::command]
|
||||||
// fn greet(name: &str) -> String {
|
// fn greet(name: &str) -> String {
|
||||||
// format!("Hello, {}! You've been greeted from Rust!", name)
|
// format!("Hello, {}! You've been greeted from Rust!", name)
|
||||||
@@ -69,7 +72,7 @@ pub fn run() {
|
|||||||
])
|
])
|
||||||
.setup(|app| {
|
.setup(|app| {
|
||||||
tauri::async_runtime::block_on(async move {
|
tauri::async_runtime::block_on(async move {
|
||||||
let db = setup_db(&app).await;
|
let db = setup_db(app).await;
|
||||||
|
|
||||||
app.manage(AppState { db });
|
app.manage(AppState { db });
|
||||||
});
|
});
|
||||||
|
|||||||
6
src-tauri/src/models/exercise.rs
Normal file
6
src-tauri/src/models/exercise.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub struct Exercise {
|
||||||
|
id: u16,
|
||||||
|
ex_type: String,
|
||||||
|
content: String,
|
||||||
|
node_id: u32
|
||||||
|
}
|
||||||
3
src-tauri/src/models/mod.rs
Normal file
3
src-tauri/src/models/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod path;
|
||||||
|
pub mod node;
|
||||||
|
pub mod exercise;
|
||||||
6
src-tauri/src/models/node.rs
Normal file
6
src-tauri/src/models/node.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub struct Node {
|
||||||
|
id: u32,
|
||||||
|
title: String,
|
||||||
|
description: String,
|
||||||
|
path_id: String
|
||||||
|
}
|
||||||
12
src-tauri/src/models/path.rs
Normal file
12
src-tauri/src/models/path.rs
Normal 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,
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
pub mod path_repository;
|
||||||
|
|||||||
@@ -1,11 +1,43 @@
|
|||||||
use sqlx::sqlite::{SqlitePool};
|
use sqlx::{pool, sqlite::SqlitePool, FromRow};
|
||||||
|
|
||||||
|
use crate::models::path::Path;
|
||||||
|
|
||||||
pub struct PathRepository<'a> {
|
pub struct PathRepository<'a> {
|
||||||
pub pool: &'a SqlitePool,
|
pub pool: &'a SqlitePool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PathRepository<'a> {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
0
src-tauri/src/services/mod.rs
Normal file
0
src-tauri/src/services/mod.rs
Normal file
Reference in New Issue
Block a user