More tests, not found page and dashboard fixes
Some checks failed
Rust / Test (push) Has been cancelled
Rust / Rustfmt (push) Has been cancelled
Rust / Clippy (push) Has been cancelled
Rust / Code coverage (push) Has been cancelled

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:
Alphonse Paix
2025-09-26 20:31:30 +02:00
parent 0f6b479af9
commit f9ae3f42a6
10 changed files with 233 additions and 48 deletions

View File

@@ -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
}