diff --git a/src/bin/todo/create.rs b/src/bin/todo/create.rs index ef85c04..4d8cc25 100644 --- a/src/bin/todo/create.rs +++ b/src/bin/todo/create.rs @@ -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)] @@ -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."); } diff --git a/src/bin/todo/delete.rs b/src/bin/todo/delete.rs index b8fac1c..f3c986c 100644 --- a/src/bin/todo/delete.rs +++ b/src/bin/todo/delete.rs @@ -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 {} @@ -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."); } diff --git a/src/bin/todo/list.rs b/src/bin/todo/list.rs index 4502300..3c6a319 100644 --- a/src/bin/todo/list.rs +++ b/src/bin/todo/list.rs @@ -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); diff --git a/src/bin/todo/update.rs b/src/bin/todo/update.rs index ac9c9ed..20e9a6b 100644 --- a/src/bin/todo/update.rs +++ b/src/bin/todo/update.rs @@ -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)] @@ -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."); } diff --git a/src/db/mod.rs b/src/db/mod.rs index 24d92ff..2af0eee 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -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 = OnceCell::const_new(); @@ -48,3 +49,73 @@ pub async fn get_all_tasks() -> Result, Error> { Ok(tasks) } + +pub async fn insert_todo( + description: String, + priority: Option, + difficulty: Option, + deadline: Option, +) -> 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, 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, + new_difficulty: Option, + new_deadline: Option, + 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(()) +}