Add ability to delete tasks

This commit is contained in:
Yash Karandikar 2022-08-27 12:19:38 -05:00
parent f70e411fca
commit ed15d48309
3 changed files with 50 additions and 1 deletions

View file

@ -151,7 +151,9 @@ async fn main() -> Result<(), Error> {
.route("/update/:id", post(tasks::update_backend))
.route("/create", get(tasks::create_form))
.route("/create", post(tasks::create_backend))
.route("/:id", get(tasks::task_detail));
.route("/:id", get(tasks::task_detail))
.route("/delete/:id", get(tasks::delete_form))
.route("/delete/:id", post(tasks::delete_backend));
let serve_dir = get_service(ServeDir::new(
config.static_dir.unwrap_or_else(|| "static".into()),

View file

@ -245,3 +245,42 @@ pub async fn task_detail(
Err(StatusCode::NOT_FOUND.into())
}
pub async fn delete_form(
Extension(tera): Extension<Arc<Tera>>,
OriginalUri(uri): OriginalUri,
session: ReadableSession,
) -> Result<Html<String>, Error> {
let _username = login_or_redirect!(session, "/login");
let c = ctx! {
"uri" => uri.to_string()
};
let rendered = tera.render("tasks/delete.html", &c)?;
Ok(Html(rendered))
}
pub async fn delete_backend(
Path(id): Path<i32>,
Extension(pool): Extension<Arc<Pool<Postgres>>>,
session: ReadableSession,
) -> Result<Redirect, Error> {
let username = login_or_redirect!(session, "/login");
info!("Getting user id");
let (user_id,): (i32,) = sqlx::query_as("select id from users where username=$1")
.bind(&username)
.fetch_one(&*pool)
.await?;
info!("deleting task");
sqlx::query("delete from tasks where owner=$1 and id=$2")
.bind(user_id)
.bind(id)
.execute(&*pool)
.await?;
info!("task deleted, redirecting");
Ok(Redirect::to("/"))
}

View file

@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block content %}
<form action="{{uri}}" method="POST">
Are you sure you want to delete this task?
<br>
<a href="/"><button>Cancel</button></a><input style="background-color: #bf616a" type="submit" value="Delete">
</form>
{% endblock %}