From 57374cd6af851a5617f7b28c4f2eae611fbe6b02 Mon Sep 17 00:00:00 2001 From: aryansri-19 Date: Fri, 5 Jul 2024 16:00:35 +0530 Subject: [PATCH] Fix: Correct parameter in list_tasks --- src/bin/todo/delete.rs | 2 +- src/bin/todo/list.rs | 10 ++++++---- src/bin/todo/update.rs | 2 +- src/lib.rs | 27 +++++++++++++-------------- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/bin/todo/delete.rs b/src/bin/todo/delete.rs index b912c9c..1e1ddd4 100644 --- a/src/bin/todo/delete.rs +++ b/src/bin/todo/delete.rs @@ -6,7 +6,7 @@ use mindmap::Task; pub struct Args {} pub async fn command(_args: &Args) { - let tasks = Task::list_tasks(false) + let tasks = Task::list_tasks(None) .await .expect("Internal Server Error. Try Again!"); diff --git a/src/bin/todo/list.rs b/src/bin/todo/list.rs index 35eabb7..94c771b 100644 --- a/src/bin/todo/list.rs +++ b/src/bin/todo/list.rs @@ -1,3 +1,4 @@ +use chrono::Local; use clap::Parser; use mindmap::Task; @@ -5,14 +6,15 @@ use mindmap::Task; pub struct Args {} pub async fn command(_args: &Args) { - let rows = Task::list_tasks(true) + let today = Local::now().date_naive(); + let rows = Task::list_tasks(Some(today)) .await .expect("Failed to fetch today's tasks."); for row in rows { println!("------------------------\nTask: {}", row.description); - println!("Priority: {:?}", row.priority); - println!("Difficulty: {:?}", row.difficulty); - println!("Deadline: {:?}\n------------------------\n", row.deadline); + println!("Priority: {:?}", row.priority.unwrap()); + println!("Difficulty: {:?}", row.difficulty.unwrap()); + println!("Deadline: {:?}\n------------------------\n", row.deadline.unwrap()); } } diff --git a/src/bin/todo/update.rs b/src/bin/todo/update.rs index 415b786..3cc7932 100644 --- a/src/bin/todo/update.rs +++ b/src/bin/todo/update.rs @@ -7,7 +7,7 @@ use mindmap::{Difficulty, Priority}; pub struct Args {} pub async fn command(_args: &Args) { - let tasks = Task::list_tasks(false) + let tasks = Task::list_tasks(None) .await .expect("Internal Server Error. Try Again!"); diff --git a/src/lib.rs b/src/lib.rs index d4e1164..896b21b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use std::fmt; use std::str::FromStr; use bytes::BytesMut; -use chrono::{Local, NaiveDate}; +use chrono::NaiveDate; use clap::ValueEnum; use db::get_client; use tokio_postgres::types::{FromSql, IsNull, ToSql, Type}; @@ -191,20 +191,19 @@ impl Task { Ok(()) } - pub async fn list_tasks(current: bool) -> Result, Box> { + pub async fn list_tasks(date: Option) -> Result, Box> { let client = get_client().await?; - - let today = Local::now().date_naive(); - 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 (query, params): (&str, &[&(dyn ToSql + Sync)]) = match date { + Some(_d) => + ( + "SELECT description, priority, difficulty, deadline FROM todo WHERE deadline = $1::date", + &[&date.unwrap()], + ), + None => + ( + "SELECT description, priority, difficulty, deadline FROM todo", + &[], + ) }; let rows = client .query(query, params)