Read tasks from db on startup
This commit is contained in:
parent
535f332a81
commit
8393ed610f
25
migrations/tables.sql
Normal file
25
migrations/tables.sql
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
PRAGMA foreign_keys = ON;
|
||||||
|
|
||||||
|
create table if not exists task
|
||||||
|
(
|
||||||
|
id integer primary key autoincrement,
|
||||||
|
title text not null,
|
||||||
|
description text not null,
|
||||||
|
column_id integer,
|
||||||
|
foreign key (column_id) references kb_column(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
create table if not exists kb_column
|
||||||
|
(
|
||||||
|
id integer primary key autoincrement,
|
||||||
|
name text not null,
|
||||||
|
selected_task integer not null default 0
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
create table if not exists setting
|
||||||
|
(
|
||||||
|
id integer primary key autoincrement,
|
||||||
|
name text not null,
|
||||||
|
value text not null
|
||||||
|
);
|
22
src/app.rs
22
src/app.rs
@ -1,5 +1,6 @@
|
|||||||
// use indexmap::IndexMap;
|
// use indexmap::IndexMap;
|
||||||
// use int_enum::IntEnum;
|
// use int_enum::IntEnum;
|
||||||
|
use crate::get_all_tasks;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
@ -17,7 +18,7 @@ pub struct Column {
|
|||||||
pub tasks: Vec<Task>,
|
pub tasks: Vec<Task>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Deserialize, Serialize, Debug, sqlx::FromRow)]
|
#[derive(Clone, Default, Deserialize, Serialize, Debug, sqlx::FromRow)]
|
||||||
pub struct Task {
|
pub struct Task {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
@ -179,6 +180,25 @@ impl Project {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn load2(pool: &SqlitePool) -> Result<Self, KanbanError> {
|
||||||
|
let todos = get_all_tasks(&pool).await.unwrap();
|
||||||
|
|
||||||
|
Ok(Project {
|
||||||
|
name: String::from("Kanban Board"),
|
||||||
|
filepath: String::from("path"),
|
||||||
|
columns: todos
|
||||||
|
.iter()
|
||||||
|
.map(|(cname, tasks)| Column {
|
||||||
|
name: cname.clone(),
|
||||||
|
// TODO: Figure out how to avoid cloning here
|
||||||
|
tasks: tasks.to_vec(),
|
||||||
|
selected_task_idx: 0,
|
||||||
|
})
|
||||||
|
.collect::<Vec<Column>>(),
|
||||||
|
selected_column_idx: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Will panic if there's an error serializing the Json or there's an issue
|
/// Will panic if there's an error serializing the Json or there's an issue
|
||||||
|
27
src/db.rs
27
src/db.rs
@ -1,13 +1,30 @@
|
|||||||
use sqlx::SqlitePool;
|
|
||||||
use crate::app::Task;
|
use crate::app::Task;
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
pub async fn get_all_tasks(pool: SqlitePool) -> Option<Vec<Task>> {
|
pub async fn get_tasks_by_column(pool: &SqlitePool, column_name: &String) -> Option<Vec<Task>> {
|
||||||
sqlx::query_as!(Task,
|
sqlx::query_as!(
|
||||||
|
Task,
|
||||||
r#"
|
r#"
|
||||||
select title, description from task
|
select title, description from task
|
||||||
"#
|
join kb_column on column_id = kb_column.id
|
||||||
|
where kb_column.name = ?1
|
||||||
|
"#,
|
||||||
|
column_name
|
||||||
)
|
)
|
||||||
.fetch_all(&pool)
|
.fetch_all(pool)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_all_tasks(pool: &SqlitePool) -> Option<Vec<(String, Vec<Task>)>> {
|
||||||
|
let columns = sqlx::query!("select name from kb_column")
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let mut tasks_by_column: Vec<(String, Vec<Task>)> = Vec::with_capacity(columns.len());
|
||||||
|
for col in columns {
|
||||||
|
let tasks = get_tasks_by_column(pool, &col.name).await.unwrap();
|
||||||
|
tasks_by_column.push((col.name, tasks));
|
||||||
|
}
|
||||||
|
Some(tasks_by_column)
|
||||||
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#![deny(rust_2018_idioms)]
|
#![deny(rust_2018_idioms)]
|
||||||
mod app;
|
mod app;
|
||||||
|
mod db;
|
||||||
mod input;
|
mod input;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod db;
|
|
||||||
|
|
||||||
pub use app::*;
|
pub use app::*;
|
||||||
pub use db::*;
|
pub use db::*;
|
||||||
|
@ -59,7 +59,7 @@ fn prompt_project_init(default_name: &str) -> (String, io::Result<File>) {
|
|||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
async fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
||||||
let (filepath, file) = match CliArgs::parse() {
|
let (_filepath, _file) = match CliArgs::parse() {
|
||||||
CliArgs {
|
CliArgs {
|
||||||
filepath: Some(filepath),
|
filepath: Some(filepath),
|
||||||
} => {
|
} => {
|
||||||
@ -89,7 +89,8 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
let db_pool = SqlitePool::connect("sqlite:db.sqlite").await?;
|
let db_pool = SqlitePool::connect("sqlite:db.sqlite").await?;
|
||||||
|
|
||||||
let mut state = State::new(db_pool, Project::load(filepath, &file)?);
|
let project = Project::load2(&db_pool).await?;
|
||||||
|
let mut state = State::new(db_pool, project);
|
||||||
|
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user