From b15fdf5921309914fef60ff767bb40e88d679e3d Mon Sep 17 00:00:00 2001 From: Piotr Orzechowski Date: Fri, 12 Apr 2024 13:47:56 +0200 Subject: [PATCH] Redirect to /admin/newsletters after sending newsletter --- src/routes/admin/newsletters/post.rs | 10 +++++++--- tests/api/admin_newsletters.rs | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/routes/admin/newsletters/post.rs b/src/routes/admin/newsletters/post.rs index eeda95a..1d78b94 100644 --- a/src/routes/admin/newsletters/post.rs +++ b/src/routes/admin/newsletters/post.rs @@ -4,15 +4,17 @@ use crate::{ utils::{e500, HttpError}, }; use anyhow::Context; -use axum::{extract::State, Form}; +use axum::{extract::State, response::Redirect, Form}; +use axum_messages::Messages; use serde::Deserialize; use sqlx::PgPool; #[tracing::instrument(skip(app_state, form))] pub(in crate::routes::admin) async fn publish_newsletter( State(app_state): State, + messages: Messages, Form(form): Form, -) -> Result<(), HttpError> { +) -> Result> { for subscriber in get_confirmed_subscribers(&app_state.db_pool) .await .map_err(e500)? @@ -38,7 +40,9 @@ pub(in crate::routes::admin) async fn publish_newsletter( } } - Ok(()) + messages.info("Newsletter sent!"); + + Ok(Redirect::to("/admin/newsletters")) } #[tracing::instrument(skip(db_pool))] diff --git a/tests/api/admin_newsletters.rs b/tests/api/admin_newsletters.rs index e04f13c..4f68bfa 100644 --- a/tests/api/admin_newsletters.rs +++ b/tests/api/admin_newsletters.rs @@ -36,7 +36,7 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() { let response = app.post_newsletters(newsletter_request_body).await; // then - assert_eq!(response.status(), 200); + assert_redirect_to(&response, "/admin/newsletters"); } #[tokio::test] @@ -69,7 +69,7 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() { let response = app.post_newsletters(newsletter_request_body).await; // then - assert_eq!(response.status(), 200); + assert_redirect_to(&response, "/admin/newsletters"); } #[tokio::test]