diff --git a/src/db/mod.rs b/src/db/mod.rs index af5f83c..f857a18 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,14 +1,15 @@ -use std::env; - -use dotenv::dotenv; use tokio::sync::OnceCell; use tokio_postgres::{Client, Error, NoTls}; +use crate::configuration::get_configuration; + static CLIENT: OnceCell = OnceCell::const_new(); async fn init_client() -> Result { - dotenv().ok(); - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let database_url = get_configuration() + .expect("Failed to read configuration") + .database + .connection_string(); let (client, connection) = tokio_postgres::connect(&database_url, NoTls).await?; diff --git a/src/lib.rs b/src/lib.rs index 7cc4214..50458d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -195,17 +195,19 @@ impl Task { pub async fn list_tasks(date: Option) -> Result, Box> { let client = get_client().await?; + let date_params: NaiveDate; let (query, params): (&str, &[&(dyn ToSql + Sync)]) = match date { - Some(_d) => + Some(d) => { + date_params = d; ( "SELECT description, priority, difficulty, deadline FROM todo WHERE deadline = $1::date", - &[&date.unwrap()], - ), - None => - ( - "SELECT description, priority, difficulty, deadline FROM todo", - &[], + &[&date_params], ) + } + None => ( + "SELECT description, priority, difficulty, deadline FROM todo", + &[], + ), }; let rows = client .query(query, params)