From e7805cfb0417c34ce3564ece8106d4b9bca3a8a9 Mon Sep 17 00:00:00 2001 From: Piotr Orzechowski Date: Fri, 12 Apr 2024 11:40:11 +0200 Subject: [PATCH] Move newsletters endpoint to admin module --- src/routes/admin/mod.rs | 3 +++ src/routes/admin/newsletters/mod.rs | 3 +++ .../newsletters/post.rs} | 14 ++++---------- src/routes/mod.rs | 1 - src/startup.rs | 3 +-- tests/api/helpers.rs | 18 +++++++++--------- 6 files changed, 20 insertions(+), 22 deletions(-) create mode 100644 src/routes/admin/newsletters/mod.rs rename src/routes/{newsletters.rs => admin/newsletters/post.rs} (87%) diff --git a/src/routes/admin/mod.rs b/src/routes/admin/mod.rs index f042179..bc67115 100644 --- a/src/routes/admin/mod.rs +++ b/src/routes/admin/mod.rs @@ -5,10 +5,12 @@ use axum::{ }; use dashboard::admin_dashboard; use logout::log_out; +use newsletters::publish_newsletter; use password::{change_password, change_password_form}; mod dashboard; mod logout; +mod newsletters; mod password; pub fn router() -> Router { @@ -17,6 +19,7 @@ pub fn router() -> Router { "/admin", Router::new() .route("/dashboard", get(admin_dashboard)) + .route("/newsletters", post(publish_newsletter)) .route("/password", get(change_password_form)) .route("/password", post(change_password)) .route("/logout", post(log_out)), diff --git a/src/routes/admin/newsletters/mod.rs b/src/routes/admin/newsletters/mod.rs new file mode 100644 index 0000000..6991fe3 --- /dev/null +++ b/src/routes/admin/newsletters/mod.rs @@ -0,0 +1,3 @@ +mod post; + +pub(super) use post::publish_newsletter; diff --git a/src/routes/newsletters.rs b/src/routes/admin/newsletters/post.rs similarity index 87% rename from src/routes/newsletters.rs rename to src/routes/admin/newsletters/post.rs index 82f424c..050fa38 100644 --- a/src/routes/newsletters.rs +++ b/src/routes/admin/newsletters/post.rs @@ -1,26 +1,20 @@ use crate::{ app_state::AppState, - authentication::{extract::SessionUserId, middleware::AuthorizedSessionLayer}, + authentication::extract::SessionUserId, domain::{SubscriberEmail, SubscriptionStatus}, utils::{e500, HttpError}, }; use anyhow::Context; -use axum::{extract::State, routing::post, Json, Router}; +use axum::{extract::State, Json}; use serde::Deserialize; use sqlx::PgPool; -pub fn router() -> Router { - Router::new() - .route("/newsletters", post(publish_newsletter)) - .layer(AuthorizedSessionLayer) -} - #[tracing::instrument( name = "Publish newsletter", skip(app_state, user_id, body), fields(username=tracing::field::Empty, user_id=tracing::field::Empty) )] -async fn publish_newsletter( +pub(in crate::routes::admin) async fn publish_newsletter( State(app_state): State, SessionUserId(user_id): SessionUserId, Json(body): Json, @@ -82,7 +76,7 @@ async fn get_confirmed_subscribers( } #[derive(Deserialize)] -struct BodyData { +pub(in crate::routes::admin) struct BodyData { title: String, content: Content, } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 1f9e88a..780f687 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -2,6 +2,5 @@ pub mod admin; pub mod health_check; pub mod home; pub mod login; -pub mod newsletters; pub mod subscriptions; pub mod subscriptions_confirm; diff --git a/src/startup.rs b/src/startup.rs index eb4492e..3e484be 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -3,7 +3,7 @@ use crate::{ configuration::{ApplicationSettings, DatabaseSettings, Settings}, email_client::EmailClient, request_id::RequestUuid, - routes::{admin, health_check, home, login, newsletters, subscriptions, subscriptions_confirm}, + routes::{admin, health_check, home, login, subscriptions, subscriptions_confirm}, telemetry::request_span, }; use anyhow::anyhow; @@ -131,7 +131,6 @@ async fn run( .merge(health_check::router()) .merge(subscriptions::router()) .merge(subscriptions_confirm::router()) - .merge(newsletters::router()) .merge(home::router()) .merge(login::router()) .merge(admin::router()) diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index 2cca5e8..5c4c9ea 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -109,15 +109,6 @@ impl TestApp { .expect(Self::FAILED_TO_EXECUTE_REQUEST) } - pub async fn post_newsletters(&self, body: &serde_json::Value) -> reqwest::Response { - self.client - .post(self.url("/newsletters")) - .json(body) - .send() - .await - .expect(Self::FAILED_TO_EXECUTE_REQUEST) - } - pub fn get_confirmation_links(&self, request: &wiremock::Request) -> ConfirmationLinks { let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap(); @@ -177,6 +168,15 @@ impl TestApp { self.get_admin_dashboard().await.text().await.unwrap() } + pub async fn post_newsletters(&self, body: &serde_json::Value) -> reqwest::Response { + self.client + .post(self.url("/admin/newsletters")) + .json(body) + .send() + .await + .expect(Self::FAILED_TO_EXECUTE_REQUEST) + } + pub async fn get_change_password_form(&self) -> Response { self.client .get(self.url("/admin/password"))