Added SQL functions

This commit is contained in:
famfo 2022-04-16 23:54:13 +02:00
parent 27d25a1818
commit 08a1803219
5 changed files with 81 additions and 31 deletions

View file

@ -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<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
}
}

View file

@ -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,
}

View file

@ -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::<task::MoveRequest>())
.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::<task::Request>())
.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::<task::Create>())
.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<Database>) -> 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<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
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();
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"))
}

View file

@ -11,13 +11,14 @@
<hr>
<form action="/api/task/move" method="post">
<label for="move_task">Move to:</label>
<select name="move_task" id="move_task" style="width:auto">
<label for="request">Move to:</label>
<select name="request" id="request" style="width:auto">
<option value="Ideas">ideas</option>
<option value="Assigned">assigned</option>
<option value="InProgress">in progreess</option>
<option value="Done">done</option>
<input type="submit" value="Move">
</select>
<input type="hidden" id="task_id" name="task_id" value="{{task.id}}">
<input type="submit" value="Move">
</form>

View file

@ -6,13 +6,17 @@
<a href="api/task/create">Create issue</a><br><br>
<label for="group">Task group</label>
<select name="group" id="group" style="width:auto">
<option value="ideas">ideas</option>
<option value="assigned">assigned</option>
<option value="in-progress">in progreess</option>
<option value="done">done</option>
</select>
<form action="/api/task/sort" method="post">
<label for="request">Task group</label>
<select name="request" id="request" style="width:auto">
<option value="Ideas">ideas</option>
<option value="Assigned">assigned</option>
<option value="InProgress">in progreess</option>
<option value="Done">done</option>
<input type="submit" value="Select">
</select>
</form>
<hr>
<!-- Some only selecting specific task code here, for smarter people then me -->