refactor: html routes finished

This commit is contained in:
Per Stark
2025-03-15 22:14:17 +01:00
parent 89155130e6
commit f2a434a071
9 changed files with 227 additions and 551 deletions

View File

@@ -1,46 +1,36 @@
use axum::{
extract::{Path, State},
response::{IntoResponse, Redirect},
response::IntoResponse,
};
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal};
use serde::Serialize;
use common::storage::types::{text_content::TextContent, user::User};
use crate::{error::HtmlError, html_state::HtmlState, page_data};
use crate::{
html_state::HtmlState,
middleware_auth::RequireUser,
template_response::{HtmlError, TemplateResponse},
};
use super::render_template;
page_data!(ContentPageData, "content/base.html", {
#[derive(Serialize)]
pub struct ContentPageData {
user: User,
text_contents: Vec<TextContent>
});
text_contents: Vec<TextContent>,
}
pub async fn show_content_page(
State(state): State<HtmlState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
RequireUser(user): RequireUser,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
let text_contents = User::get_text_contents(&user.id, &state.db)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
ContentPageData::template_name(),
Ok(TemplateResponse::new_template(
"content/base.html",
ContentPageData {
user,
text_contents,
},
state.templates,
)?;
Ok(output.into_response())
))
}
#[derive(Serialize)]
@@ -51,57 +41,33 @@ pub struct TextContentEditModal {
pub async fn show_text_content_edit_form(
State(state): State<HtmlState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
RequireUser(user): RequireUser,
Path(id): Path<String>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.db).await?;
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.db)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
Ok(TemplateResponse::new_template(
"content/edit_text_content_modal.html",
TextContentEditModal { user, text_content },
state.templates,
)?;
Ok(output.into_response())
))
}
pub async fn patch_text_content(
State(state): State<HtmlState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
RequireUser(user): RequireUser,
Path(id): Path<String>,
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/signin").into_response()),
};
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.db)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let text_content = User::get_and_validate_text_content(&id, &user.id, &state.db).await?;
// ADD FUNCTION TO PATCH CONTENT
let text_contents = User::get_text_contents(&user.id, &state.db)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let text_contents = User::get_text_contents(&user.id, &state.db).await?;
let output = render_template(
Ok(TemplateResponse::new_template(
"content/content_list.html",
ContentPageData {
user,
text_contents,
},
state.templates,
)?;
Ok(output.into_response())
))
}