WIP: Fetch all tasks

This commit is contained in:
Joseph Ferano 2023-06-10 13:48:20 +07:00
parent a8e3a44816
commit 535f332a81
4 changed files with 22 additions and 17 deletions

View File

@ -1,6 +1,7 @@
// use indexmap::IndexMap; // use indexmap::IndexMap;
// use int_enum::IntEnum; // use int_enum::IntEnum;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use std::cmp::min; use std::cmp::min;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -16,8 +17,7 @@ pub struct Column {
pub tasks: Vec<Task>, pub tasks: Vec<Task>,
} }
// #[derive(Deserialize, Serialize, Debug, Clone, Copy)] #[derive(Default, Deserialize, Serialize, Debug, sqlx::FromRow)]
#[derive(Default, Deserialize, Serialize, Debug)]
pub struct Task { pub struct Task {
pub title: String, pub title: String,
pub description: String, pub description: String,
@ -68,6 +68,7 @@ impl Default for TaskState<'_> {
pub struct State<'a> { pub struct State<'a> {
pub project: Project, pub project: Project,
pub db_pool: SqlitePool,
pub quit: bool, pub quit: bool,
pub columns: Vec<Column>, pub columns: Vec<Column>,
pub task_edit_state: Option<TaskState<'a>>, pub task_edit_state: Option<TaskState<'a>>,
@ -75,10 +76,11 @@ pub struct State<'a> {
impl State<'_> { impl State<'_> {
#[must_use] #[must_use]
pub fn new(project: Project) -> Self { pub fn new(db_pool: SqlitePool, project: Project) -> Self {
State { State {
quit: false, quit: false,
task_edit_state: None, task_edit_state: None,
db_pool,
project, project,
columns: vec![], columns: vec![],
} }

13
src/db.rs Normal file
View File

@ -0,0 +1,13 @@
use sqlx::SqlitePool;
use crate::app::Task;
pub async fn get_all_tasks(pool: SqlitePool) -> Option<Vec<Task>> {
sqlx::query_as!(Task,
r#"
select title, description from task
"#
)
.fetch_all(&pool)
.await
.ok()
}

View File

@ -2,7 +2,9 @@
mod app; mod app;
mod input; mod input;
mod ui; mod ui;
mod db;
pub use app::*; pub use app::*;
pub use db::*;
pub use input::handle; pub use input::handle;
pub use ui::draw; pub use ui::draw;

View File

@ -87,21 +87,9 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
} }
}; };
let pool = SqlitePool::connect("sqlite:db.sqlite").await?; let db_pool = SqlitePool::connect("sqlite:db.sqlite").await?;
let stuff = sqlx::query!( let mut state = State::new(db_pool, Project::load(filepath, &file)?);
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)?);
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();