Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Move newsletters endpoint to admin module
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Apr 12, 2024
1 parent 9f51d51 commit e8bc603
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/routes/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppState> {
Expand All @@ -17,6 +19,7 @@ pub fn router() -> Router<AppState> {
"/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)),
Expand Down
3 changes: 3 additions & 0 deletions src/routes/admin/newsletters/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod post;

pub(super) use post::publish_newsletter;
13 changes: 3 additions & 10 deletions src/routes/newsletters.rs → src/routes/admin/newsletters/post.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use crate::{
app_state::AppState,
authentication::middleware::AuthorizedSessionLayer,
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<AppState> {
Router::new()
.route("/newsletters", post(publish_newsletter))
.layer(AuthorizedSessionLayer)
}

#[tracing::instrument(name = "Publish newsletter", skip(app_state, body))]
async fn publish_newsletter(
pub(in crate::routes::admin) async fn publish_newsletter(
State(app_state): State<AppState>,
Json(body): Json<BodyData>,
) -> Result<(), HttpError<anyhow::Error>> {
Expand Down Expand Up @@ -75,7 +68,7 @@ async fn get_confirmed_subscribers(
}

#[derive(Deserialize)]
struct BodyData {
pub(in crate::routes::admin) struct BodyData {
title: String,
content: Content,
}
Expand Down
1 change: 0 additions & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 1 addition & 2 deletions src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
18 changes: 9 additions & 9 deletions tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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"))
Expand Down

0 comments on commit e8bc603

Please sign in to comment.