Skip to content

Commit

Permalink
Fix: Correct parameter in list_tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
aryansri-19 committed Jul 5, 2024
1 parent ef00a9d commit 57374cd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/bin/todo/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");

Expand Down
10 changes: 6 additions & 4 deletions src/bin/todo/list.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use chrono::Local;
use clap::Parser;
use mindmap::Task;

#[derive(Parser)]
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());
}
}
2 changes: 1 addition & 1 deletion src/bin/todo/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");

Expand Down
27 changes: 13 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -191,20 +191,19 @@ impl Task {
Ok(())
}

pub async fn list_tasks(current: bool) -> Result<Vec<Task>, Box<dyn Error>> {
pub async fn list_tasks(date: Option<NaiveDate>) -> Result<Vec<Task>, Box<dyn Error>> {
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)
Expand Down

0 comments on commit 57374cd

Please sign in to comment.