diff --git a/src/app.rs b/src/app.rs index 3848b6d..86ef52b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ // use indexmap::IndexMap; // use int_enum::IntEnum; use serde::{Deserialize, Serialize}; +use sqlx::SqlitePool; use std::cmp::min; use std::fs::File; use std::io::Read; @@ -16,8 +17,7 @@ pub struct Column { pub tasks: Vec, } -// #[derive(Deserialize, Serialize, Debug, Clone, Copy)] -#[derive(Default, Deserialize, Serialize, Debug)] +#[derive(Default, Deserialize, Serialize, Debug, sqlx::FromRow)] pub struct Task { pub title: String, pub description: String, @@ -68,6 +68,7 @@ impl Default for TaskState<'_> { pub struct State<'a> { pub project: Project, + pub db_pool: SqlitePool, pub quit: bool, pub columns: Vec, pub task_edit_state: Option>, @@ -75,10 +76,11 @@ pub struct State<'a> { impl State<'_> { #[must_use] - pub fn new(project: Project) -> Self { + pub fn new(db_pool: SqlitePool, project: Project) -> Self { State { quit: false, task_edit_state: None, + db_pool, project, columns: vec![], } diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..e397b94 --- /dev/null +++ b/src/db.rs @@ -0,0 +1,13 @@ +use sqlx::SqlitePool; +use crate::app::Task; + +pub async fn get_all_tasks(pool: SqlitePool) -> Option> { + sqlx::query_as!(Task, + r#" + select title, description from task + "# + ) + .fetch_all(&pool) + .await + .ok() +} diff --git a/src/lib.rs b/src/lib.rs index 21507c2..a3d2ded 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,9 @@ mod app; mod input; mod ui; +mod db; pub use app::*; +pub use db::*; pub use input::handle; pub use ui::draw; diff --git a/src/main.rs b/src/main.rs index 16293c6..519ac3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,21 +87,9 @@ async fn main() -> anyhow::Result<(), Box> { } }; - let pool = SqlitePool::connect("sqlite:db.sqlite").await?; + let db_pool = SqlitePool::connect("sqlite:db.sqlite").await?; - let stuff = sqlx::query!( - r#" - select * from kanban - "# - ) - .fetch_all(&pool) - .await?; - - for item in stuff { - println!("{} - {} - {}", item.id, item.name, item.description); - } - - let mut state = State::new(Project::load(filepath, &file)?); + let mut state = State::new(db_pool, Project::load(filepath, &file)?); enable_raw_mode()?; let mut stdout = io::stdout();