155 lines
4.8 KiB
Rust
155 lines
4.8 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 crate::task;
|
|
use blake2::{Blake2b512, Digest};
|
|
use sqlx::postgres::{PgConnectOptions, PgConnectionInfo, PgPoolOptions};
|
|
use sqlx::Executor;
|
|
use sqlx::{ConnectOptions, PgPool};
|
|
use tracing::info;
|
|
use tracing::log::LevelFilter;
|
|
|
|
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;
|
|
}
|
|
|
|
pub async fn create_user(&self, register: task::Register) -> anyhow::Result<i32> {
|
|
if register.username.len() < 17 {
|
|
let mut hasher = Blake2b512::new();
|
|
hasher.update(register.password);
|
|
let hash = format!("{:x}", hasher.finalize());
|
|
tracing::debug!("Password hash for user {}: {}", register.username, hash);
|
|
let user = sqlx::query!(
|
|
"INSERT INTO users (username, hash)
|
|
VALUES ($1::Varchar, $2::Varchar)
|
|
RETURNING id",
|
|
register.username,
|
|
hash
|
|
)
|
|
.fetch_one(&self.0)
|
|
.await?;
|
|
return Ok(user.id);
|
|
}
|
|
Err(anyhow::anyhow!("Username is longer then 16 characters"))
|
|
}
|
|
|
|
pub async fn login(&self, login: task::Register) -> anyhow::Result<i32> {
|
|
let mut hasher = Blake2b512::new();
|
|
hasher.update(login.password);
|
|
let hash = format!("{:x}", hasher.finalize());
|
|
let user = sqlx::query!(
|
|
"SELECT * FROM users
|
|
WHERE username = $1::Varchar",
|
|
login.username
|
|
)
|
|
.fetch_one(&self.0)
|
|
.await;
|
|
if let Ok(user) = user {
|
|
if user.hash == hash {
|
|
return Ok(user.id);
|
|
} else {
|
|
return Err(anyhow::anyhow!("Passwords do not match."));
|
|
}
|
|
}
|
|
Err(anyhow::anyhow!("Could not find user {}.", login.username))
|
|
}
|
|
|
|
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,
|
|
author_id: i32,
|
|
category_id: i32,
|
|
org_id: i32,
|
|
status_id: i32,
|
|
created: sqlx::types::chrono::NaiveDateTime,
|
|
) {
|
|
let tasks = sqlx::query!(
|
|
"INSERT INTO tasks (
|
|
title,
|
|
description,
|
|
author,
|
|
category,
|
|
org,
|
|
status,
|
|
created
|
|
)
|
|
VALUES (
|
|
$1::Varchar,
|
|
$2::Varchar,
|
|
$3::Integer,
|
|
$4::Integer,
|
|
$5::Integer,
|
|
$6::Integer,
|
|
$7::Timestamp
|
|
)",
|
|
task.title,
|
|
task.description,
|
|
author_id,
|
|
category_id,
|
|
org_id,
|
|
status_id,
|
|
created,
|
|
)
|
|
.execute(&self.0)
|
|
.await;
|
|
if let Err(tasks) = tasks {
|
|
tracing::error!("{}", tasks);
|
|
}
|
|
}
|
|
}
|