From 814fb7d9ee2fd74f2c65585d8fe7783a0ed7f770 Mon Sep 17 00:00:00 2001 From: aryansri-19 Date: Fri, 5 Jul 2024 15:41:23 +0530 Subject: [PATCH] Refactor: Change semantics and remove redundant function --- src/bin/todo/create.rs | 10 +++++----- src/bin/todo/delete.rs | 6 +++--- src/bin/todo/list.rs | 2 +- src/bin/todo/update.rs | 36 +++++++++--------------------------- src/db/mod.rs | 26 -------------------------- src/lib.rs | 30 ++++++++++++++++++++++++------ 6 files changed, 42 insertions(+), 68 deletions(-) diff --git a/src/bin/todo/create.rs b/src/bin/todo/create.rs index 8df8dab..678feaa 100644 --- a/src/bin/todo/create.rs +++ b/src/bin/todo/create.rs @@ -8,25 +8,25 @@ pub struct Args {} pub async fn command(_args: &Args) { let task = Task { - description: inquire::prompt_text("Description").expect("An error occurred!"), + description: inquire::prompt_text("Description:").expect("An error occurred!"), difficulty: Select::new( - "Difficulty", + "Difficulty:", vec![Difficulty::Low, Difficulty::Medium, Difficulty::High], ) .prompt_skippable() .expect("An error occurred!"), priority: Select::new( - "Priority", + "Priority:", vec![Priority::Low, Priority::Medium, Priority::High], ) .prompt_skippable() .expect("An error occurred!"), - deadline: DateSelect::new("Deadline") + deadline: DateSelect::new("Deadline:") .with_min_date(Local::now().date_naive()) .with_week_start(Weekday::Mon) .prompt_skippable() .expect("An error occurred!"), }; - task.insert_todo().await.expect("Failed to insert task"); + task.save_to_db().await.expect("Failed to insert task"); } diff --git a/src/bin/todo/delete.rs b/src/bin/todo/delete.rs index e9b680d..b912c9c 100644 --- a/src/bin/todo/delete.rs +++ b/src/bin/todo/delete.rs @@ -1,12 +1,12 @@ use clap::Parser; use inquire::Select; -use mindmap::db::get_all_tasks; +use mindmap::Task; #[derive(Parser)] pub struct Args {} pub async fn command(_args: &Args) { - let tasks = get_all_tasks() + let tasks = Task::list_tasks(false) .await .expect("Internal Server Error. Try Again!"); @@ -22,5 +22,5 @@ pub async fn command(_args: &Args) { .find(|task| task.description == *task_description) .expect("Task not found!"); - task.delete_task().await.expect("Failed to delete task"); + task.delete_from_db().await.expect("Failed to delete task"); } diff --git a/src/bin/todo/list.rs b/src/bin/todo/list.rs index 75393aa..615d9f7 100644 --- a/src/bin/todo/list.rs +++ b/src/bin/todo/list.rs @@ -5,7 +5,7 @@ use mindmap::Task; pub struct Args {} pub async fn command(_args: &Args) { - let rows = Task::list_tasks() + let rows = Task::list_tasks(true) .await .expect("Failed to fetch all tasks."); diff --git a/src/bin/todo/update.rs b/src/bin/todo/update.rs index 05871db..415b786 100644 --- a/src/bin/todo/update.rs +++ b/src/bin/todo/update.rs @@ -1,13 +1,13 @@ use chrono::{Local, Weekday}; use clap::Parser; -use mindmap::db::get_all_tasks; -use mindmap::{Difficulty, Priority, Task}; +use mindmap::Task; +use mindmap::{Difficulty, Priority}; #[derive(Parser)] pub struct Args {} pub async fn command(_args: &Args) { - let tasks = get_all_tasks() + let tasks = Task::list_tasks(false) .await .expect("Internal Server Error. Try Again!"); @@ -16,43 +16,25 @@ pub async fn command(_args: &Args) { return; }; - let task_choices: Vec = tasks - .iter() - .map(|task| { - format!( - "\nDescription: {}\nDifficulty: {}\nPriority: {}\nDeadline: {}", - task.description, - task.difficulty - .as_ref() - .map_or("Not set".to_string(), |d| d.to_string()), - task.priority - .as_ref() - .map_or("Not set".to_string(), |p| p.to_string()), - task.deadline - .map_or("Not set".to_string(), |d| d.to_string()) - ) - }) - .collect(); - - let task_description = inquire::Select::new("Select the task to update:", task_choices) + let old_task = inquire::Select::new("Select the task to update:", tasks) .prompt() .expect("An error occurred!"); let new_task = Task { - description: inquire::prompt_text("New Description").expect("An error occurred!"), + description: inquire::prompt_text("New Description:").expect("An error occurred!"), difficulty: inquire::Select::new( - "New Difficulty", + "New Difficulty:", vec![Difficulty::Low, Difficulty::Medium, Difficulty::High], ) .prompt_skippable() .expect("An error occurred!"), priority: inquire::Select::new( - "New Priority", + "New Priority:", vec![Priority::Low, Priority::Medium, Priority::High], ) .prompt_skippable() .expect("An error occurred!"), - deadline: inquire::DateSelect::new("New Deadline") + deadline: inquire::DateSelect::new("New Deadline:") .with_min_date(Local::now().date_naive()) .with_week_start(Weekday::Mon) .prompt_skippable() @@ -60,7 +42,7 @@ pub async fn command(_args: &Args) { }; new_task - .update_task(task_description) + .update_task(old_task.description) .await .expect("Failed to update task"); } diff --git a/src/db/mod.rs b/src/db/mod.rs index c54186d..af5f83c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -4,8 +4,6 @@ use dotenv::dotenv; use tokio::sync::OnceCell; use tokio_postgres::{Client, Error, NoTls}; -use crate::Task; - static CLIENT: OnceCell = OnceCell::const_new(); async fn init_client() -> Result { @@ -26,27 +24,3 @@ async fn init_client() -> Result { pub async fn get_client() -> Result<&'static Client, Error> { CLIENT.get_or_try_init(init_client).await } - -pub async fn get_all_tasks() -> Result, Error> { - let client = get_client().await?; - - let rows = client - .query( - "SELECT description, priority, difficulty, deadline FROM todo", - &[], - ) - .await - .expect("Failed to fetch all tasks."); - - let tasks: Vec = rows - .iter() - .map(|row| Task { - description: row.get(0), - priority: row.get(1), - difficulty: row.get(2), - deadline: row.get(3), - }) - .collect(); - - Ok(tasks) -} diff --git a/src/lib.rs b/src/lib.rs index 11c816d..d4e1164 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,8 +148,18 @@ pub struct Task { pub deadline: Option, } +impl fmt::Display for Task { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "\nTask: {}\nPriority: {:?}\nDifficulty: {:?}\nDeadline: {:?}\n", + self.description, self.priority, self.difficulty, self.deadline + ) + } +} + impl Task { - pub async fn insert_todo(&self) -> Result<(), Box> { + pub async fn save_to_db(&self) -> Result<(), Box> { let client = get_client().await?; client @@ -165,7 +175,7 @@ impl Task { Ok(()) } - pub async fn delete_task(&self) -> Result<(), Box> { + pub async fn delete_from_db(&self) -> Result<(), Box> { let client = get_client().await?; client @@ -181,17 +191,25 @@ impl Task { Ok(()) } - pub async fn list_tasks() -> Result, Box> { + pub async fn list_tasks(current: bool) -> Result, Box> { let client = get_client().await?; let today = Local::now().date_naive(); - let rows = client - .query( + let (query, params): (&str, &[&(dyn ToSql + Sync)]) = if current { + ( "SELECT description, priority, difficulty, deadline FROM todo WHERE deadline = $1::date", &[&today], ) + } else { + ( + "SELECT description, priority, difficulty, deadline FROM todo", + &[], + ) + }; + let rows = client + .query(query, params) .await - .expect("Failed to fetch all tasks."); + .expect("Failed to fetch tasks."); let tasks: Vec = rows .iter()