More tests, not found page and dashboard fixes
When post was deleted, website shows a 404 page insead of an 500 page. Also made the dashboard empty page message more explicit.
This commit is contained in:
@@ -25,7 +25,7 @@ pub use unsubscribe::*;
|
||||
|
||||
use crate::{
|
||||
authentication::AuthError,
|
||||
templates::{InternalErrorTemplate, MessageTemplate, NotFoundTemplate},
|
||||
templates::{HtmlTemplate, InternalErrorTemplate, MessageTemplate, NotFoundTemplate},
|
||||
};
|
||||
|
||||
pub fn generate_token() -> String {
|
||||
@@ -155,9 +155,6 @@ impl From<AuthError> for AppError {
|
||||
}
|
||||
|
||||
pub async fn not_found() -> Response {
|
||||
(
|
||||
StatusCode::NOT_FOUND,
|
||||
Html(NotFoundTemplate.render().unwrap()),
|
||||
)
|
||||
.into_response()
|
||||
let template = HtmlTemplate(NotFoundTemplate);
|
||||
(StatusCode::NOT_FOUND, template).into_response()
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use anyhow::Context;
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
Extension, Form,
|
||||
extract::State,
|
||||
extract::{Path, State},
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use chrono::Utc;
|
||||
@@ -121,3 +121,26 @@ pub async fn create_newsletter(
|
||||
};
|
||||
insert_newsletter_issue(transaction, post_title, &template).await
|
||||
}
|
||||
|
||||
pub async fn delete_post(
|
||||
State(AppState {
|
||||
connection_pool, ..
|
||||
}): State<AppState>,
|
||||
Path(post_id): Path<Uuid>,
|
||||
) -> Result<Response, AppError> {
|
||||
let res = sqlx::query!("DELETE FROM posts WHERE post_id = $1", post_id)
|
||||
.execute(&connection_pool)
|
||||
.await
|
||||
.context("Failed to delete post from database.")
|
||||
.map_err(AppError::unexpected_message)?;
|
||||
if res.rows_affected() > 1 {
|
||||
Err(AppError::unexpected_message(anyhow::anyhow!(
|
||||
"We could not find the post in the database."
|
||||
)))
|
||||
} else {
|
||||
let template = MessageTemplate::Success {
|
||||
message: "The subscriber has been deleted.".into(),
|
||||
};
|
||||
Ok(template.render().unwrap().into_response())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
domain::PostEntry,
|
||||
routes::AppError,
|
||||
routes::{AppError, not_found},
|
||||
startup::AppState,
|
||||
templates::{PostListTemplate, PostTemplate, PostsTemplate},
|
||||
templates::{HtmlTemplate, PostListTemplate, PostTemplate, PostsTemplate},
|
||||
};
|
||||
use anyhow::Context;
|
||||
use askama::Template;
|
||||
@@ -75,27 +75,41 @@ pub async fn see_post(
|
||||
Query(PostParams { origin }): Query<PostParams>,
|
||||
) -> Result<Response, AppError> {
|
||||
if let Some(origin) = origin {
|
||||
sqlx::query!(
|
||||
"UPDATE notifications_delivered SET opened = TRUE WHERE email_id = $1",
|
||||
origin,
|
||||
)
|
||||
.execute(&connection_pool)
|
||||
.await
|
||||
.context("Failed to mark email as opened.")
|
||||
.map_err(AppError::unexpected_page)?;
|
||||
mark_email_as_opened(&connection_pool, origin).await?;
|
||||
return Ok(Redirect::to(&format!("/posts/{}", post_id)).into_response());
|
||||
}
|
||||
let post = get_post_data(&connection_pool, post_id)
|
||||
|
||||
if let Some(post) = get_post_data(&connection_pool, post_id)
|
||||
.await
|
||||
.context(format!("Failed to fetch post #{}", post_id))
|
||||
.map_err(AppError::unexpected_page)?
|
||||
.to_html()
|
||||
.context("Could not render markdown with extension.")?;
|
||||
let template = PostTemplate { post };
|
||||
Ok(Html(template.render().unwrap()).into_response())
|
||||
{
|
||||
let post = post
|
||||
.to_html()
|
||||
.context("Could not render markdown with extension.")?;
|
||||
let template = HtmlTemplate(PostTemplate { post });
|
||||
Ok(template.into_response())
|
||||
} else {
|
||||
Ok(not_found().await)
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_post_data(connection_pool: &PgPool, post_id: Uuid) -> Result<PostEntry, sqlx::Error> {
|
||||
async fn mark_email_as_opened(connection_pool: &PgPool, email_id: Uuid) -> Result<(), AppError> {
|
||||
sqlx::query!(
|
||||
"UPDATE notifications_delivered SET opened = TRUE WHERE email_id = $1",
|
||||
email_id,
|
||||
)
|
||||
.execute(connection_pool)
|
||||
.await
|
||||
.context("Failed to mark email as opened.")
|
||||
.map_err(AppError::unexpected_page)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_post_data(
|
||||
connection_pool: &PgPool,
|
||||
post_id: Uuid,
|
||||
) -> Result<Option<PostEntry>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
PostEntry,
|
||||
r#"
|
||||
@@ -106,7 +120,7 @@ async fn get_post_data(connection_pool: &PgPool, post_id: Uuid) -> Result<PostEn
|
||||
"#,
|
||||
post_id
|
||||
)
|
||||
.fetch_one(connection_pool)
|
||||
.fetch_optional(connection_pool)
|
||||
.await
|
||||
}
|
||||
|
||||
|
||||
@@ -124,7 +124,8 @@ pub fn app(
|
||||
.route("/password", post(change_password))
|
||||
.route("/newsletters", post(publish_newsletter))
|
||||
.route("/posts", post(create_post))
|
||||
.route("/logout", post(logout))
|
||||
.route("/posts/{post_id}", delete(delete_post))
|
||||
.route("/logout", get(logout))
|
||||
.route("/subscribers", get(get_subscribers_page))
|
||||
.route("/subscribers/{subscriber_id}", delete(delete_subscriber))
|
||||
.layer(middleware::from_fn(require_auth));
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
use crate::{
|
||||
domain::{PostEntry, SubscriberEntry},
|
||||
routes::DashboardStats,
|
||||
routes::{AppError, DashboardStats},
|
||||
};
|
||||
use askama::Template;
|
||||
use axum::response::{Html, IntoResponse};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct HtmlTemplate<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self.0.render() {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(err) => AppError::unexpected_page(anyhow::anyhow!(err)).into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
pub enum MessageTemplate {
|
||||
#[template(path = "../templates/success.html")]
|
||||
|
||||
Reference in New Issue
Block a user