This commit is contained in:
Yash Karandikar 2022-04-16 20:23:42 -05:00
parent f660f63f23
commit 07d19eeed5
3 changed files with 25 additions and 33 deletions

View file

@ -16,12 +16,12 @@
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::task;
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);
@ -32,9 +32,7 @@ impl Database {
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());
let pgver = conn.server_version_num().map(|v| v.to_string());
info!(
"Database connected, PostgreSQL version '{}', migrating schema",
pgver.as_deref().unwrap_or("unknown")
@ -54,18 +52,16 @@ impl Database {
// 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,
}
]
// 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) {

View file

@ -25,7 +25,7 @@ pub struct Request {
#[derive(Debug, Deserialize)]
pub struct MoveRequest {
pub request: String,
pub request: String,
pub task_id: u32,
}
@ -46,4 +46,3 @@ pub struct Task {
pub description: String,
pub id: u32,
}

View file

@ -21,12 +21,12 @@ use std::sync::Arc;
use async_sqlx_session::PostgresSessionStore;
use chrono::Duration;
use sqlx::types::chrono::Utc;
use std::time::SystemTime;
use tera::{Context, Tera};
use tokio::sync::broadcast;
use tracing::info;
use warp::{Filter, Reply};
use warp_sessions::{CookieOptions, SameSiteCookieOption, SessionWithStore};
use std::time::SystemTime;
use crate::{task, Config, Database};
@ -134,8 +134,8 @@ impl App {
let db = self.db.clone();
move || {
let mut ctx = Context::new();
// TODO: Replace the for loop with an SQL request to select only the tasks you can
// see after you logged in and the selected category which is stored in a cookie
// TODO: Replace the for loop with an SQL request to select only the tasks you can
// see after you logged in and the selected category which is stored in a cookie
// previously (see `post_task_sort` for more info)
// TODO: figure out what we need to pass to the get_issues function
let tasks = db.get_tasks();
@ -143,8 +143,7 @@ impl App {
warp::reply::html(tera.render("task/task_page.html", &ctx).unwrap())
}
}))
.or(warp::path("create")
.map({
.or(warp::path("create").map({
let tera = self.tera.clone();
move || {
let ctx = Context::new();
@ -157,24 +156,21 @@ impl App {
.and(warp::path("move"))
.and(warp::body::form::<task::MoveRequest>())
.and(db.clone())
.then(post_task_move)
)
.then(post_task_move))
.or(warp::post()
.and(warp::path("api"))
.and(warp::path("task"))
.and(warp::path("sort"))
.and(warp::body::form::<task::Request>())
.and(db.clone())
.then(post_task_sort)
)
.then(post_task_sort))
.or(warp::post()
.and(warp::path("api"))
.and(warp::path("task"))
.and(warp::path("create"))
.and(warp::body::form::<task::Create>())
.and(db.clone())
.then(post_task_create)
);
.then(post_task_create));
let (_, server) =
warp::serve(route).bind_with_graceful_shutdown(self.config.listen_addr, async move {
@ -194,14 +190,15 @@ pub async fn post_task_move(form: task::MoveRequest, db: Arc<Database>) -> impl
pub async fn post_task_sort(form: task::Request, db: Arc<Database>) -> impl warp::Reply {
tracing::debug!("Got POST on /api/task/move: {}", form.request);
// TODO: Store the information of the form in a cookie for the selection of the tasks in the
// website building
// website building
warp::redirect(warp::http::Uri::from_static("/task"))
}
pub async fn post_task_create(form: task::Create, db: Arc<Database>)-> impl warp::Reply {
let date = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
pub async fn post_task_create(form: task::Create, db: Arc<Database>) -> impl warp::Reply {
let date = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
tracing::debug!("Got POST on /api/task/create: {:?}, date: {:?}", form, date);
db.create_task(form).await;
warp::redirect(warp::http::Uri::from_static("/task"))
}