From d016f2e95ed3e7e4368076268c777f2053cca157 Mon Sep 17 00:00:00 2001 From: traxys Date: Mon, 28 Aug 2023 22:52:37 +0200 Subject: [PATCH] Allow to delete mails --- src/main.rs | 33 +++++++++++++++++++++++++++++++-- templates/home.html | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5300b3b..c99d97f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,8 @@ use axum::{ extract::{FromRef, FromRequestParts, Path, Query, State}, http::{request::Parts, StatusCode}, response::{Html, IntoResponse, Redirect}, - routing::get, - Router, + routing::{get, post}, + Form, Router, }; use axum_extra::extract::{cookie::Cookie, CookieJar}; use base64::{engine::general_purpose, engine::Engine}; @@ -503,6 +503,34 @@ async fn home(state: State>, User(user): User) -> Result>, + User(user): User, + Form(delete): Form, +) -> Result { + let rows_affected = sqlx::query!( + "DELETE FROM emails WHERE id = $1 AND mail = $2", + user, + delete.mail + ) + .execute(&state.db) + .await? + .rows_affected(); + + if rows_affected != 1 { + tracing::warn!("Invalid number of rows affected in delete: {rows_affected}"); + return Err(Error::InternalError); + } + + Ok(Redirect::to("/")) +} + #[tokio::main] async fn main() -> color_eyre::Result<()> { color_eyre::install()?; @@ -533,6 +561,7 @@ async fn main() -> color_eyre::Result<()> { .route("/login", get(login)) .route("/login/redirect/:id", get(redirected)) .route("/", get(home)) + .route("/mail/delete", post(delete_mail)) .fallback(page_not_found) .with_state(Arc::new(AppState { db, diff --git a/templates/home.html b/templates/home.html index bb9313b..6ae1723 100644 --- a/templates/home.html +++ b/templates/home.html @@ -9,8 +9,39 @@

Mail management

Mails

-
    - {% for mail in mails %}
  • {{ mail.mail }}
  • {% endfor %} +
      + {% for mail in mails %} +
    • + {{ mail.mail }} + + +
    • + {% endfor %}
{% endblock content %}