Skip to content

Commit

Permalink
Refactor: Moving client code to lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
aryansri-19 committed Jul 4, 2024
1 parent ed0d7da commit ed1e736
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 45 deletions.
20 changes: 9 additions & 11 deletions src/bin/todo/create.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{Local, Weekday};
use clap::Parser;
use inquire::{DateSelect, Select};
use mindmap::db::get_client;
use mindmap::db::insert_todo;
use mindmap::{Difficulty, Priority, Task};

#[derive(Parser)]
Expand Down Expand Up @@ -29,14 +29,12 @@ pub async fn command(_args: &Args) {
.expect("An error occurred!"),
};

let client = get_client().await.expect("Failed to get client");
client
.execute(
"INSERT INTO todo (description, priority, difficulty, deadline) VALUES ($1, $2, $3, $4)",
&[&task.description, &task.priority, &task.difficulty, &task.deadline],
)
.await
.expect("Failed to insert task");

println!("Task \"{}\" created successfully!", task.description);
insert_todo(
task.description,
task.priority,
task.difficulty,
task.deadline,
)
.await
.expect("Failed to insert task.");
}
13 changes: 3 additions & 10 deletions src/bin/todo/delete.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use inquire::Select;
use mindmap::db::{get_all_tasks, get_client};
use mindmap::db::{delete_task, get_all_tasks};

#[derive(Parser)]
pub struct Args {}
Expand All @@ -16,14 +16,7 @@ pub async fn command(_args: &Args) {
.prompt()
.expect("An error occurred!");

let client = get_client().await.expect("Failed to fetch client");
client
.execute(
"DELETE FROM todo WHERE description = $1",
&[&task_description],
)
delete_task(task_description.to_string())
.await
.expect("Failed to delete task");

println!("Task \"{}\" deleted successfully!", task_description);
.expect("Failed to delete task.");
}
13 changes: 2 additions & 11 deletions src/bin/todo/list.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use chrono::Local;
use clap::Parser;
use mindmap::db::get_client;
use mindmap::db::list_tasks;

#[derive(Parser)]
pub struct Args {}

pub async fn command(_args: &Args) {
let client = get_client().await.expect("Failed to fetch client");
let today = Local::now().date_naive();
let rows = client
.query(
"SELECT description, priority, difficulty, deadline FROM todo WHERE deadline = $1::date",
&[&today],
)
.await
.expect("Failed to fetch tasks");
let rows = list_tasks().await.expect("Failed to list tasks.");

for row in rows {
let description: String = row.get(0);
Expand Down
21 changes: 10 additions & 11 deletions src/bin/todo/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{Local, Weekday};
use clap::Parser;
use mindmap::db::{get_all_tasks, get_client};
use mindmap::db::{get_all_tasks, update_task};
use mindmap::{Difficulty, Priority, Task};

#[derive(Parser)]
Expand Down Expand Up @@ -59,14 +59,13 @@ pub async fn command(_args: &Args) {
.expect("An error occurred!"),
};

let client = get_client().await.expect("Failed to fetch client");
client
.execute(
"UPDATE todo SET description = $1, priority = $2, difficulty = $3, deadline = $4 WHERE description = $5",
&[&new_task.description, &new_task.priority, &new_task.difficulty, &new_task.deadline, &task_description],
)
.await
.expect("Failed to update task");

println!("Task updated successfully!");
update_task(
new_task.description,
new_task.priority,
new_task.difficulty,
new_task.deadline,
task_description,
)
.await
.expect("Failed to update task.");
}
75 changes: 73 additions & 2 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::Task;
use crate::{Difficulty, Priority, Task};
use chrono::{Local, NaiveDate};
use dotenv::dotenv;
use std::env;
use tokio::sync::OnceCell;
use tokio_postgres::{Client, Error, NoTls};
use tokio_postgres::{Client, Error, NoTls, Row};

static CLIENT: OnceCell<Client> = OnceCell::const_new();

Expand Down Expand Up @@ -48,3 +49,73 @@ pub async fn get_all_tasks() -> Result<Vec<Task>, Error> {

Ok(tasks)
}

pub async fn insert_todo(
description: String,
priority: Option<Priority>,
difficulty: Option<Difficulty>,
deadline: Option<NaiveDate>,
) -> Result<(), Error> {
let client = get_client().await?;

client
.execute(
"INSERT INTO todo (description, priority, difficulty, deadline) VALUES ($1, $2, $3, $4)",
&[&description, &priority, &difficulty, &deadline],
)
.await
.expect("Failed to insert task");

println!("Task \"{}\" created successfully!", description);

Ok(())
}

pub async fn delete_task(description: String) -> Result<(), Error> {
let client = get_client().await?;

client
.execute("DELETE FROM todo WHERE description = $1", &[&description])
.await
.expect("Failed to delete task");

println!("Task \"{}\" deleted successfully!", description);

Ok(())
}

pub async fn list_tasks() -> Result<Vec<Row>, Error> {
let client = get_client().await?;

let today = Local::now().date_naive();
let rows = client
.query(
"SELECT description, priority, difficulty, deadline FROM todo WHERE deadline = $1::date",
&[&today],
)
.await
.expect("Failed to fetch tasks");

Ok(rows)
}

pub async fn update_task(
new_description: String,
new_priority: Option<Priority>,
new_difficulty: Option<Difficulty>,
new_deadline: Option<NaiveDate>,
old_description: String,
) -> Result<(), Error> {
let client = get_client().await?;
client
.execute(
"UPDATE todo SET description = $1, priority = $2, difficulty = $3, deadline = $4 WHERE description = $5",
&[&new_description, &new_priority, &new_difficulty, &new_deadline, &old_description],
)
.await
.expect("Failed to update task");

println!("Task updated successfully!");

Ok(())
}

0 comments on commit ed1e736

Please sign in to comment.