Skip to content

Commit

Permalink
Feat: Crud operations completed
Browse files Browse the repository at this point in the history
  • Loading branch information
aryansri-19 committed Jul 1, 2024
1 parent 6e0b318 commit 460381a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 17 deletions.
6 changes: 1 addition & 5 deletions src/bin/todo/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ pub struct Args {
description: String,
#[clap(long)]
priority: Priority,
#[clap(long, value_parser=parse_datetime)]
#[clap(long, value_parser=mindmap::parse_datetime)]
deadline: NaiveDateTime,
}

fn parse_datetime(s: &str) -> Result<NaiveDateTime, chrono::ParseError> {
NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
}

pub async fn command(args: &Args) {

let task = Task {
Expand Down
16 changes: 13 additions & 3 deletions src/bin/todo/delete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use clap::Parser;
use mindmap::db::get_client;

#[derive(Parser)]
pub struct Args {}
pub struct Args {
#[clap(long)]
description: String,
}

pub async fn command(args: &Args) {

let description = args.description.clone();

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

pub fn command(_args: &Args) {
println!("Deleted task successfully!")
println!("Task \"{}\" deleted successfully!", description);
}
20 changes: 18 additions & 2 deletions src/bin/todo/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use chrono::Local;
use clap::Parser;
use mindmap::db::get_client;

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

pub fn command(_args: &Args) {
println!("Tasks due today:");
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, deadline FROM todo WHERE DATE(deadline) = $1::date", &[&today]).await.expect("Failed to fetch tasks");

for row in rows {
let description: String = row.get(0);
let priority: mindmap::Priority = row.get(1);
let deadline: chrono::NaiveDateTime = row.get(2);

println!("Task: {}", description);
println!("Priority: {:?}", priority);
println!("Deadline: {}", deadline);
println!();
}
}
6 changes: 3 additions & 3 deletions src/bin/todo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ async fn main() {

match &cli.command {
Commands::Create(args) => create::command(args).await,
Commands::List(args) => list::command(args),
Commands::List(args) => list::command(args).await,
Commands::Show(args) => show::command(args),
Commands::Update(args) => update::command(args),
Commands::Delete(args) => delete::command(args),
Commands::Update(args) => update::command(args).await,
Commands::Delete(args) => delete::command(args).await,
}
}
37 changes: 34 additions & 3 deletions src/bin/todo/update.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
use chrono::NaiveDateTime;
use clap::Parser;
use mindmap::Priority;

#[derive(Parser)]
pub struct Args {}
pub struct Args {
#[clap(long)]
description: String,
#[clap(long)]
new_description: String,
#[clap(long)]
new_priority: Priority,
#[clap(long, value_parser=mindmap::parse_datetime)]
new_deadline: NaiveDateTime,
}

pub async fn command(_args: &Args) {

let client = mindmap::db::get_client().await.expect("Failed to fetch client");

let rows = client.query("SELECT description, priority, deadline FROM todo WHERE description = $1", &[&_args.description]).await.expect("Failed to fetch task");

if rows.is_empty() {
println!("Task not found!");
return;
}

let description = _args.description.clone();
let new_description = _args.new_description.clone();
let new_priority = _args.new_priority.clone();
let new_deadline = _args.new_deadline;

client.execute(
"UPDATE todo SET description = $1, priority = $2, deadline = $3 WHERE description = $4",
&[&new_description, &new_priority, &new_deadline, &description],
).await.expect("Failed to update task");

pub fn command(_args: &Args) {
println!("Updated task successfully!")
println!("Task updated successfully!");
}
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ pub mod db;

use chrono::NaiveDateTime;
use clap::ValueEnum;
use tokio_postgres::types::{IsNull, ToSql, Type};
use tokio_postgres::types::{IsNull, ToSql, FromSql, Type};
use std::error::Error as StdError;
use std::str::FromStr;
use bytes::BytesMut;

#[derive(Debug, Clone, ValueEnum)]
Expand Down Expand Up @@ -34,6 +35,36 @@ impl ToSql for Priority {
tokio_postgres::types::to_sql_checked!();
}

impl FromStr for Priority {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"low" => Ok(Priority::Low),
"neutral" => Ok(Priority::Neutral),
"unknown" => Ok(Priority::Unknown),
"high" => Ok(Priority::High),
"critical" => Ok(Priority::Critical),
_ => Err(format!("Invalid priority: {}", s)),
}
}
}

impl<'a> FromSql<'a> for Priority {
fn from_sql(_ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn StdError + Send + Sync>> {
let s = std::str::from_utf8(raw)?;
s.parse().map_err(|e| Box::new(std::io::Error::new(std::io::ErrorKind::InvalidData, e)) as Box<dyn StdError + Send + Sync>)
}

fn accepts(ty: &Type) -> bool {
ty.name() == "priority"
}
}

pub fn parse_datetime(s: &str) -> Result<NaiveDateTime, chrono::ParseError> {
NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
}

#[derive(Debug, Clone)]
pub struct Task {
pub description: String,
Expand Down

0 comments on commit 460381a

Please sign in to comment.