tmtd/src/database.rs
2022-04-16 23:54:13 +02:00

79 lines
2.5 KiB
Rust

/*
* tmtd - Suckless To Do list
* Copyright (C) 2022 C4TG1RL5
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions};
use sqlx::Executor;
use sqlx::{ConnectOptions, PgPool};
use tracing::info;
use tracing::log::LevelFilter;
use crate::task;
pub struct Database(PgPool);
impl Database {
pub async fn connect(conn_string: &str) -> anyhow::Result<Self> {
let mut connect_options: PgConnectOptions = conn_string.parse()?;
connect_options.log_statements(LevelFilter::Debug);
info!("Connecting to the database");
let pool = PgPoolOptions::new().connect_with(connect_options).await?;
let mut conn = pool.acquire().await?;
let pgver = conn
.server_version_num()
.map(|v| v.to_string());
info!(
"Database connected, PostgreSQL version '{}', migrating schema",
pgver.as_deref().unwrap_or("unknown")
);
conn.execute(include_str!("sql/schema.sql")).await?;
Ok(Self(pool))
}
pub fn pool(&self) -> PgPool {
self.0.clone()
}
pub async fn close(&self) {
self.0.close().await;
}
// Async might become a problem here
pub fn get_tasks(&self) -> Vec<task::Task> {
// TODO: actually get the issues from the db
vec![
task::Task {
title: "TODO".to_string(),
date: "TODO".to_string(), // Convert from unix timestamps to
// the actual date, also timezone info?
status: "TODO".to_string(),
assignee: "TODO".to_string(),
description: "TODO".to_string(),
id: 1,
}
]
}
pub async fn move_task(&self, task: task::MoveRequest) {
// TODO: move the task to the desired category
}
pub async fn create_task(&self, task: task::Create) {
// TODO: insert the task into the db
}
}