diff --git a/src/main.rs b/src/main.rs index a1d6495..cc6be29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,7 +128,9 @@ async fn main() -> Result<(), Error> { let task_routes = Router::new() .route("/update/:id", get(tasks::update_form)) - .route("/update/:id", post(tasks::update_backend)); + .route("/update/:id", post(tasks::update_backend)) + .route("/create", get(tasks::create_form)) + .route("/create", post(tasks::create_backend)); let app = Router::new() .route("/", get(homepage)) @@ -207,5 +209,5 @@ async fn homepage( }, )?; - return Ok(Html(rendered)); + Ok(Html(rendered)) } diff --git a/src/tasks.rs b/src/tasks.rs index a451a75..0858267 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -38,11 +38,10 @@ pub async fn update_form( if let Some(task) = task { let c = ctx! { "task" => task, - "update" => true, "url" => uri.to_string() }; - let rendered = tera.render("tasks/create.html", &c)?; + let rendered = tera.render("tasks/update.html", &c)?; #[allow(clippy::needless_return)] // it's a compile error, clippy return Ok(Html(rendered)); @@ -51,6 +50,22 @@ pub async fn update_form( Err(StatusCode::NOT_FOUND.into()) } +pub async fn create_form( + Extension(tera): Extension>, + OriginalUri(uri): OriginalUri, + session: ReadableSession, +) -> Result, Error> { + let _username = login_or_redirect!(session, "/login"); + + let c = ctx! { + "url" => uri.to_string() + }; + + let rendered = tera.render("tasks/create.html", &c)?; + + Ok(Html(rendered)) +} + pub async fn update_backend( Path(id): Path, Extension(pool): Extension>>, @@ -59,10 +74,33 @@ pub async fn update_backend( sqlx::query("update tasks set title=$1,description=$2,status=$3 where id=$4") .bind(&data.title) .bind(&data.description) - .bind(&data.status) + .bind(data.status) .bind(id) .execute(&*pool) .await?; Ok(Redirect::to("/")) } + +pub async fn create_backend( + Extension(pool): Extension>>, + Form(data): Form, + session: ReadableSession, +) -> Result { + let username = login_or_redirect!(session, "/login"); + + let (user_id,): (i32,) = sqlx::query_as("select id from users where username=$1") + .bind(&username) + .fetch_one(&*pool) + .await?; + + sqlx::query("insert into tasks (owner,title, description, status) values ($1, $2, $3, $4)") + .bind(user_id) + .bind(&data.title) + .bind(&data.description) + .bind(data.status) + .execute(&*pool) + .await?; + + Ok(Redirect::to("/")) +} diff --git a/templates/tasks/create.html b/templates/tasks/create.html index 52cb09c..7de5935 100644 --- a/templates/tasks/create.html +++ b/templates/tasks/create.html @@ -2,17 +2,18 @@ {% block content %}
- +
- +
- +
+
{% endblock %} diff --git a/templates/tasks/update.html b/templates/tasks/update.html new file mode 100644 index 0000000..5b071d6 --- /dev/null +++ b/templates/tasks/update.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% block content %} +
+ + +
+ + +
+ +
+ +
+
+{% endblock %}