Allow to delete mails
This commit is contained in:
parent
45f89df858
commit
d016f2e95e
2 changed files with 64 additions and 4 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -9,8 +9,8 @@ use axum::{
|
||||||
extract::{FromRef, FromRequestParts, Path, Query, State},
|
extract::{FromRef, FromRequestParts, Path, Query, State},
|
||||||
http::{request::Parts, StatusCode},
|
http::{request::Parts, StatusCode},
|
||||||
response::{Html, IntoResponse, Redirect},
|
response::{Html, IntoResponse, Redirect},
|
||||||
routing::get,
|
routing::{get, post},
|
||||||
Router,
|
Form, Router,
|
||||||
};
|
};
|
||||||
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
||||||
use base64::{engine::general_purpose, engine::Engine};
|
use base64::{engine::general_purpose, engine::Engine};
|
||||||
|
|
@ -503,6 +503,34 @@ async fn home(state: State<Arc<AppState>>, User(user): User) -> Result<Html<Stri
|
||||||
Ok(Html(TEMPLATES.render("home.html", &context)?))
|
Ok(Html(TEMPLATES.render("home.html", &context)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct MailDelete {
|
||||||
|
mail: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(state))]
|
||||||
|
async fn delete_mail(
|
||||||
|
state: State<Arc<AppState>>,
|
||||||
|
User(user): User,
|
||||||
|
Form(delete): Form<MailDelete>,
|
||||||
|
) -> Result<Redirect, Error> {
|
||||||
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> color_eyre::Result<()> {
|
async fn main() -> color_eyre::Result<()> {
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
@ -533,6 +561,7 @@ async fn main() -> color_eyre::Result<()> {
|
||||||
.route("/login", get(login))
|
.route("/login", get(login))
|
||||||
.route("/login/redirect/:id", get(redirected))
|
.route("/login/redirect/:id", get(redirected))
|
||||||
.route("/", get(home))
|
.route("/", get(home))
|
||||||
|
.route("/mail/delete", post(delete_mail))
|
||||||
.fallback(page_not_found)
|
.fallback(page_not_found)
|
||||||
.with_state(Arc::new(AppState {
|
.with_state(Arc::new(AppState {
|
||||||
db,
|
db,
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,39 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="title is-1">Mail management</h1>
|
<h1 class="title is-1">Mail management</h1>
|
||||||
<h2 class="title is-2">Mails</h2>
|
<h2 class="title is-2">Mails</h2>
|
||||||
<ul>
|
<ul class="list-group">
|
||||||
{% for mail in mails %}<li>{{ mail.mail }}</li>{% endfor %}
|
{% for mail in mails %}
|
||||||
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||||
|
{{ mail.mail }}
|
||||||
|
<button type="button"
|
||||||
|
class="btn btn-danger"
|
||||||
|
data-bs-toggle="modal"
|
||||||
|
data-bs-target="#mailDelete{{ loop.index }}">Delete</button>
|
||||||
|
<div class="modal fade"
|
||||||
|
tabindex="-1"
|
||||||
|
id="mailDelete{{ loop.index }}"
|
||||||
|
aria-labelledby="mailDeleteLabel{{ loop.index }}">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h1 class="modal-title fs-5" id="mailDeleteLabel{{ loop.index }}">Delete mail '{{ mail.mail }}'</h1>
|
||||||
|
<button type="button"
|
||||||
|
class="btn-close"
|
||||||
|
data-bs-dismiss="modal"
|
||||||
|
aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<form action="/mail/delete" method="post">
|
||||||
|
<input type="hidden" name="mail" value="{{ mail.mail }}" />
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
||||||
|
<button type="submit" class="btn btn-danger">Delete</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue