Save new sort order when moving tasks to different columns
This commit is contained in:
parent
403572723c
commit
d7eb3fb47a
36
src/app.rs
36
src/app.rs
@ -3,8 +3,6 @@
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cmp::min;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use tui_textarea::TextArea;
|
||||
|
||||
use crate::db;
|
||||
@ -184,39 +182,7 @@ impl<'a> Column {
|
||||
}
|
||||
|
||||
impl Project {
|
||||
#[must_use]
|
||||
pub fn new(name: &str, filepath: String) -> Self {
|
||||
Project {
|
||||
name: name.to_owned(),
|
||||
filepath,
|
||||
columns: vec![
|
||||
Column::new("Todo"),
|
||||
Column::new("InProgress"),
|
||||
Column::new("Done"),
|
||||
Column::new("Ideas"),
|
||||
],
|
||||
selected_column_idx: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn load_from_json(json: &str) -> Result<Self, KanbanError> {
|
||||
serde_json::from_str(json).map_err(|_| KanbanError::BadJson)
|
||||
}
|
||||
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return `Err` if `file` contains json that doesn't match State schema
|
||||
pub fn load(path: String, mut file: &File) -> Result<Self, KanbanError> {
|
||||
let mut json = String::new();
|
||||
file.read_to_string(&mut json)?;
|
||||
if json.trim().is_empty() {
|
||||
Ok(Project::new("", path))
|
||||
} else {
|
||||
Self::load_from_json(&json)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn load2(pool: &Connection) -> Result<Self, KanbanError> {
|
||||
pub async fn load(pool: &Connection) -> Result<Self, KanbanError> {
|
||||
let columns = db::get_all_columns(&pool).unwrap();
|
||||
|
||||
Ok(Project {
|
||||
|
15
src/db.rs
15
src/db.rs
@ -79,10 +79,17 @@ pub fn update_task_text(conn: &Connection, task: &Task) {
|
||||
|
||||
pub fn move_task_to_column(conn: &Connection, task: &Task, target_column: &Column) {
|
||||
let mut stmt = conn
|
||||
.prepare("update task set column_id = ?2, sort_order = ?3 where task.id = ?1")
|
||||
.unwrap();
|
||||
stmt.execute((&task.id, &target_column.id, &target_column.tasks.len()))
|
||||
.prepare(
|
||||
"update task
|
||||
set
|
||||
column_id = ?2,
|
||||
sort_order = 1 +
|
||||
(select sort_order from task
|
||||
where column_id = ?2 order by sort_order desc limit 1)
|
||||
where task.id = ?1",
|
||||
)
|
||||
.unwrap();
|
||||
stmt.execute((&task.id, &target_column.id)).unwrap();
|
||||
}
|
||||
|
||||
pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) {
|
||||
@ -93,12 +100,14 @@ pub fn swap_task_order(conn: &mut Connection, task1: &Task, task2: &Task) {
|
||||
&[&task1.id],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
tx.execute(
|
||||
"update task set sort_order = (select sort_order from task where id = ?2)
|
||||
where id = ?1",
|
||||
(task1.id, task2.id),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
tx.execute(
|
||||
"update task set sort_order = (select sort_order from temp_order) where id = ?1",
|
||||
&[&task2.id],
|
||||
|
@ -90,7 +90,7 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
||||
let db_pool = Connection::open("db.sqlite")?;
|
||||
|
||||
// let project = Project::load(_filepath, &_file)?;
|
||||
let project = Project::load2(&db_pool).await?;
|
||||
let project = Project::load(&db_pool).await?;
|
||||
let mut state = State::new(db_pool, project);
|
||||
|
||||
enable_raw_mode()?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user