Basic unsubscribe endpoint
This commit is contained in:
36
src/routes/unsubscribe.rs
Normal file
36
src/routes/unsubscribe.rs
Normal 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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user