Basic unsubscribe endpoint

This commit is contained in:
Alphonse Paix
2025-09-21 17:49:31 +02:00
parent 0725b87bf2
commit 829f3e4e4f
17 changed files with 292 additions and 43 deletions

36
src/routes/unsubscribe.rs Normal file
View File

@@ -0,0 +1,36 @@
use crate::{routes::AppError, startup::AppState, templates::UnsubscribeTemplate};
use anyhow::Context;
use askama::Template;
use axum::{
extract::{Query, State},
response::{Html, IntoResponse, Response},
};
use reqwest::StatusCode;
use sqlx::Executor;
#[derive(serde::Deserialize)]
pub struct UnsubQueryParams {
token: String,
}
pub async fn unsubscribe(
Query(UnsubQueryParams { token }): Query<UnsubQueryParams>,
State(AppState {
connection_pool, ..
}): State<AppState>,
) -> Result<Response, AppError> {
let query = sqlx::query!(
"DELETE FROM subscriptions WHERE unsubscribe_token = $1",
token
);
let result = connection_pool
.execute(query)
.await
.context("Could not update subscriptions table.")?;
if result.rows_affected() == 0 {
Ok(StatusCode::NOT_FOUND.into_response())
} else {
Ok(Html(UnsubscribeTemplate.render().unwrap()).into_response())
}
}