diff --git a/src/lib.rs b/src/lib.rs index 52757e1..6c3de23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,8 @@ use axum::{ }; use static_serve::embed_assets; -use crate::{routes::template_ctx::TemplateCtx, state::AppState}; +use crate::routes::router::Router as InternalRouter; +use crate::state::AppState; mod migrations; mod models; @@ -46,12 +47,12 @@ pub fn build_app(state: AppState) -> Router { #[derive(Template, WebTemplate)] #[template(path = "404.html")] struct NotFoundTemplate { - pub ctx: TemplateCtx, + pub router: InternalRouter, } pub async fn error_handler(State(state): State) -> impl axum::response::IntoResponse { NotFoundTemplate { - ctx: TemplateCtx { + router: InternalRouter { base_path: state.config.base_path, }, } diff --git a/src/routes/book.rs b/src/routes/book.rs index 1b1dbfc..8d4fa03 100644 --- a/src/routes/book.rs +++ b/src/routes/book.rs @@ -13,9 +13,7 @@ use serde::Deserialize; use serde_with::{NoneAsEmptyString, serde_as}; use snafu::prelude::*; -use crate::{ - models::book::Model as BookModel, routes::template_ctx::TemplateCtx, state::error::CSVSnafu, -}; +use crate::{models::book::Model as BookModel, routes::router::Router, state::error::CSVSnafu}; use crate::{models::user::Model as UserModel, state::error::IOSnafu}; use crate::{ @@ -57,7 +55,7 @@ struct BookIndexTemplate { current_page: u64, total_page: u64, base_query: String, - ctx: TemplateCtx, + router: Router, } pub async fn index( @@ -130,7 +128,7 @@ pub async fn index( current_page: books_paginate.current_page, total_page: books_paginate.total_page, base_query, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) @@ -142,7 +140,7 @@ struct ShowBookTemplate { book: BookModel, owner: UserModel, current_holder: Option, - ctx: TemplateCtx, + router: Router, } pub async fn show( @@ -175,7 +173,7 @@ pub async fn show( book, owner, current_holder, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) @@ -210,7 +208,7 @@ pub async fn create( #[template(path = "books/new.html")] struct NewBookTemplate { users: Vec, - ctx: TemplateCtx, + router: Router, } pub async fn new( @@ -223,7 +221,7 @@ pub async fn new( Ok(NewBookTemplate { users, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) @@ -234,7 +232,7 @@ pub async fn new( struct EditBookTemplate { users: Vec, book: BookModel, - ctx: TemplateCtx, + router: Router, } pub async fn edit( @@ -253,7 +251,7 @@ pub async fn edit( Ok(EditBookTemplate { users, book, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index b8d18ab..2bbba56 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,3 +1,3 @@ pub mod book; -pub mod template_ctx; +pub mod router; pub mod user; diff --git a/src/routes/router.rs b/src/routes/router.rs new file mode 100644 index 0000000..52bcd0b --- /dev/null +++ b/src/routes/router.rs @@ -0,0 +1,54 @@ +#[derive(Clone)] +pub struct Router { + pub base_path: String, +} + +impl Router { + pub fn assets(&self, path: &str) -> String { + if self.base_path.is_empty() || self.base_path == "/" { + format!("/{}", path) + } else { + format!("{}/{}", self.base_path.trim_end_matches('/'), path) + } + } + + pub fn root_path(&self) -> String { + format!("{}/", &self.base_path) + } + + // BOOKS ROUTES + + pub fn new_book_path(&self) -> String { + format!("{}/books/new", &self.base_path) + } + + pub fn create_book_path(&self) -> String { + format!("{}/books", &self.base_path) + } + + pub fn update_book_path(&self, id: &i32) -> String { + format!("{}/books/{}", &self.base_path, id) + } + + pub fn download_csv_book_path(&self) -> String { + format!("{}/books/download_csv", &self.base_path) + } + + // USERS + + pub fn index_user_path(&self) -> String { + format!("{}/users", &self.base_path) + } + + pub fn new_user_path(&self) -> String { + format!("{}/users/new", &self.base_path) + } + + pub fn create_user_path(&self) -> String { + format!("{}/users", &self.base_path) + } + + pub fn update_user_path(&self, id: &i32) -> String { + format!("{}/users/{}", &self.base_path, id) + } +} diff --git a/src/routes/template_ctx.rs b/src/routes/template_ctx.rs deleted file mode 100644 index 43c09b6..0000000 --- a/src/routes/template_ctx.rs +++ /dev/null @@ -1,14 +0,0 @@ -#[derive(Clone)] -pub struct TemplateCtx { - pub base_path: String, -} - -impl TemplateCtx { - pub fn asset(&self, path: &str) -> String { - if self.base_path.is_empty() || self.base_path == "/" { - format!("/{}", path) - } else { - format!("{}/{}", self.base_path.trim_end_matches('/'), path) - } - } -} diff --git a/src/routes/user.rs b/src/routes/user.rs index d870ed7..04eed56 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -16,7 +16,7 @@ use crate::{ book::BookOperator, user::{self, UserOperator}, }, - routes::template_ctx::TemplateCtx, + routes::router::Router, state::{ AppState, error::{AppStateError, BookSnafu, UserSnafu}, @@ -28,7 +28,7 @@ use crate::{ struct UsersIndexTemplate { users_with_books_number: Vec, query: IndexQuery, - ctx: TemplateCtx, + router: Router, } pub struct UserWithBookNumber { @@ -88,7 +88,7 @@ pub async fn index( Ok(UsersIndexTemplate { users_with_books_number: result, query, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) @@ -140,7 +140,7 @@ pub async fn delete( #[template(path = "users/edit.html")] struct EditTemplate { user: user::Model, - ctx: TemplateCtx, + router: Router, } pub async fn edit( @@ -154,7 +154,7 @@ pub async fn edit( Ok(EditTemplate { user, - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, }) @@ -163,12 +163,12 @@ pub async fn edit( #[derive(Template, WebTemplate)] #[template(path = "users/new.html")] struct NewTemplate { - ctx: TemplateCtx, + router: Router, } pub async fn new(State(state): State) -> impl axum::response::IntoResponse { NewTemplate { - ctx: TemplateCtx { + router: Router { base_path: state.config.base_path, }, } diff --git a/src/state/error.rs b/src/state/error.rs index 3a9a1f4..6a031b6 100644 --- a/src/state/error.rs +++ b/src/state/error.rs @@ -6,7 +6,7 @@ use snafu::prelude::*; use crate::{ models::{book::BookError, user::UserError}, - routes::template_ctx::TemplateCtx, + routes::router::Router, state::config::ConfigError, }; @@ -48,7 +48,7 @@ pub enum AppStateError { #[template(path = "error.html")] struct ErrorTemplate { state: AppStateErrorContext, - ctx: TemplateCtx, + router: Router, } struct AppStateErrorContext { @@ -68,7 +68,7 @@ impl IntoResponse for AppStateError { let error_context = AppStateErrorContext::from(self); ErrorTemplate { state: error_context, - ctx: TemplateCtx { + router: Router { base_path: "".to_string(), }, } diff --git a/templates/404.html b/templates/404.html index e76f185..6f35ef6 100644 --- a/templates/404.html +++ b/templates/404.html @@ -8,6 +8,6 @@

{{ t!("error.error_404.title") }}

{{ t!("error.error_404.subtitle") }}

- {{ t!("error.error_404.button") }} + {{ t!("error.error_404.button") }}
{% endblock %} diff --git a/templates/base.html b/templates/base.html index dd8aa4f..5c50fb4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,12 +4,12 @@ {% block title %}{{ t!("name") }}{% endblock %} - - + + - - + + {% block extra_head %}{% endblock extra_head %} @@ -26,7 +26,7 @@ {% endblock %} - - + + diff --git a/templates/books/edit.html b/templates/books/edit.html index ecb8734..f80be6a 100644 --- a/templates/books/edit.html +++ b/templates/books/edit.html @@ -10,7 +10,7 @@ {{ typography::heading(t!("book.edit.title")) }} {% call cards::card() %} -
+
diff --git a/templates/books/new.html b/templates/books/new.html index 719c9e4..30deb91 100644 --- a/templates/books/new.html +++ b/templates/books/new.html @@ -11,7 +11,7 @@ {{ typography::heading(t!("book.new.title")) }} {% call cards::card() %} - + {{ form_helpers::input("title", t!("book.attributes.title"), is_required = true, placeholder = "Ex: La Petite Dernière") }} {{ form_helpers::input("authors", t!("book.attributes.authors"), is_required = true, placeholder = "Ex: Fatima Daas") }} diff --git a/templates/components/dropdown.html b/templates/components/dropdown.html index 29c0841..7b0e55d 100644 --- a/templates/components/dropdown.html +++ b/templates/components/dropdown.html @@ -18,9 +18,9 @@
diff --git a/templates/error.html b/templates/error.html index abc0eee..09e125b 100644 --- a/templates/error.html +++ b/templates/error.html @@ -12,6 +12,6 @@

{{ error }}

{% endfor %} - {{ t!("error.error_404.button") }} + {{ t!("error.error_404.button") }} {% endblock %} diff --git a/templates/index.html b/templates/index.html index 641f7a7..2701d7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@ {% block main %} {% call typography::heading(t!("book.index.title")) %} - + {{ t!("common.download") }} (csv) {% endcall %} @@ -87,7 +87,7 @@ @@ -98,7 +98,7 @@

{{ t!("common.no_result") }}

- + {{ t!("book.new.button_short") }}
@@ -142,19 +142,19 @@ diff --git a/templates/layout/footer.html b/templates/layout/footer.html index 0cafa82..3f29c8b 100644 --- a/templates/layout/footer.html +++ b/templates/layout/footer.html @@ -2,4 +2,4 @@

{{ t!("footer.message") }}

- \ No newline at end of file + diff --git a/templates/layout/header.html b/templates/layout/header.html index dd9ecbc..114aa1f 100644 --- a/templates/layout/header.html +++ b/templates/layout/header.html @@ -1,6 +1,6 @@