diff --git a/src/database.rs b/src/database.rs index b1ae66b..1f206e6 100644 --- a/src/database.rs +++ b/src/database.rs @@ -21,6 +21,7 @@ use sqlx::Executor; use sqlx::{ConnectOptions, PgPool}; use tracing::info; use tracing::log::LevelFilter; +use crate::task; pub struct Database(PgPool); @@ -51,5 +52,27 @@ impl Database { self.0.close().await; } + // Async might become a problem here + pub fn get_tasks(&self) -> Vec { + // 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 + } } diff --git a/src/task.rs b/src/task.rs index c11ae5d..fa573d6 100644 --- a/src/task.rs +++ b/src/task.rs @@ -20,7 +20,13 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize)] pub struct Request { - pub move_task: String, + pub request: String, +} + +#[derive(Debug, Deserialize)] +pub struct MoveRequest { + pub request: String, + pub task_id: u32, } #[derive(Debug, Deserialize)] @@ -38,5 +44,6 @@ pub struct Task { pub status: String, pub assignee: String, pub description: String, + pub id: u32, } diff --git a/src/web.rs b/src/web.rs index d041c38..1134ce4 100644 --- a/src/web.rs +++ b/src/web.rs @@ -115,6 +115,9 @@ impl App { } pub async fn run(self, mut cancel: broadcast::Receiver<()>) { + let cloned_db = self.db.clone(); // Borrow checker moment + let db = warp::any().map(move || cloned_db.clone()); + let route = warp_session!( warp::path::end(), self.session_store, @@ -128,19 +131,14 @@ impl App { ) .or(warp::path("task").map({ let tera = self.tera.clone(); + let db = self.db.clone(); move || { let mut ctx = Context::new(); - let mut tasks = Vec::new(); - for _ in 0..10 { - let task_ctx = task::Task { - date: "TODO".to_string(), - assignee: "TODO".to_string(), - description: "TODO".to_string(), - status: "TODO".to_string(), - title: "TODO".to_string(), - }; - tasks.push(task_ctx); - } + // 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(); ctx.insert("tasks", &tasks); warp::reply::html(tera.render("task/task_page.html", &ctx).unwrap()) } @@ -158,15 +156,25 @@ impl App { .and(warp::path("api")) .and(warp::path("task")) .and(warp::path("move")) + .and(warp::body::form::()) + .and(db.clone()) + .then(post_task_move) + ) + .or(warp::post() + .and(warp::path("api")) + .and(warp::path("task")) + .and(warp::path("sort")) .and(warp::body::form::()) - .then(handle_post) + .and(db.clone()) + .then(post_task_sort) ) .or(warp::post() .and(warp::path("api")) .and(warp::path("task")) .and(warp::path("create")) .and(warp::body::form::()) - .then(handle_post_create) + .and(db.clone()) + .then(post_task_create) ); let (_, server) = @@ -178,16 +186,23 @@ impl App { } } -pub async fn handle_post(form: task::Request) -> impl warp::Reply { - // TODO: intelligent SQL code here (or maybe in another function idk, it's up to the reader to - // decide whats more intelligent) - tracing::debug!("Got POST on /api/task/move: {}", form.move_task); - warp::redirect(warp::http::Uri::from_static("/")) +pub async fn post_task_move(form: task::MoveRequest, db: Arc) -> impl warp::Reply { + tracing::debug!("Got POST on /api/task/move: {:?}", form); + db.move_task(form).await; + warp::redirect(warp::http::Uri::from_static("/task")) } -pub async fn handle_post_create(form: task::Create)-> impl warp::Reply { +pub async fn post_task_sort(form: task::Request, db: Arc) -> 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 + warp::redirect(warp::http::Uri::from_static("/task")) +} + +pub async fn post_task_create(form: task::Create, db: Arc)-> impl warp::Reply { let date = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); tracing::debug!("Got POST on /api/task/create: {:?}, date: {:?}", form, date); - warp::redirect(warp::http::Uri::from_static("/")) + db.create_task(form); + warp::redirect(warp::http::Uri::from_static("/task")) } diff --git a/templates/task/task.html b/templates/task/task.html index e1ae8f4..a41775f 100644 --- a/templates/task/task.html +++ b/templates/task/task.html @@ -11,13 +11,14 @@
- - - + +
diff --git a/templates/task/task_page.html b/templates/task/task_page.html index cb115e7..d1ae6b9 100644 --- a/templates/task/task_page.html +++ b/templates/task/task_page.html @@ -6,13 +6,17 @@ Create issue

- - +
+ + + +
+