From 785ca3b0d1390de367a4e68fdf9987e1c82a6a85 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Sun, 31 Mar 2024 16:54:11 -0500 Subject: [PATCH] (feat): add new struct for the file display logic --- src/{models.rs => db_models.rs} | 2 +- src/main.rs | 6 +++++- src/routes.rs | 21 ++++++++++++++++++++- src/structs.rs | 16 ++++++++++++++++ src/templates.rs | 4 +++- 5 files changed, 45 insertions(+), 4 deletions(-) rename src/{models.rs => db_models.rs} (64%) create mode 100644 src/structs.rs diff --git a/src/models.rs b/src/db_models.rs similarity index 64% rename from src/models.rs rename to src/db_models.rs index 2f28880..42d69ec 100644 --- a/src/models.rs +++ b/src/db_models.rs @@ -1,2 +1,2 @@ // use serde::{Deserialize, Serialize}; -// put models here \ No newline at end of file +// put DB models here diff --git a/src/main.rs b/src/main.rs index 64088f0..27d4285 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,11 @@ +use shuttle_axum::AxumService; +use shuttle_runtime; + +mod db_models; mod errors; -mod models; mod router; mod routes; +mod structs; mod templates; mod utils; diff --git a/src/routes.rs b/src/routes.rs index 3b35b6a..806daca 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,7 +7,9 @@ use comrak::markdown_to_html; use crate::{ errors::ApiError, + structs::FileDisplay, templates::{self, Tut}, + utils, }; pub async fn portfolio() -> impl IntoResponse { @@ -19,7 +21,24 @@ pub async fn notes() -> impl IntoResponse { } pub async fn tuts(paths: Vec) -> impl IntoResponse { - templates::Tuts { tuts_list: paths } + let mut new_paths: Vec = vec![]; + for path in paths { + let new_struct = FileDisplay { + file_path: path.clone().to_string(), + title: utils::upper_all_words_first_letters(utils::rm_delimeter( + utils::rm_path_ext(path), + "_".to_string(), + " ".to_string(), + )) + .unwrap(), + }; + + new_paths.push(new_struct); + } + + templates::Tuts { + tuts_list: new_paths, + } } pub async fn tuts_builder(path: String, tut_title: String) -> Result { diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..10919ed --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,16 @@ +use std::fmt::Display; + +impl Display for FileDisplay { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{{file_path: {}, title: {}}}", + self.file_path, self.title + ) + } +} + +pub struct FileDisplay { + pub file_path: String, + pub title: String, +} diff --git a/src/templates.rs b/src/templates.rs index 24e297b..3447ae1 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,5 +1,7 @@ use askama::Template; +use crate::structs::FileDisplay; + #[derive(Template)] #[template(path = "portfolio.html")] pub struct Portfolio; @@ -11,7 +13,7 @@ pub struct Notes; #[derive(Template)] #[template(path = "tuts.html")] pub struct Tuts { - pub tuts_list: Vec, + pub tuts_list: Vec, } #[derive(Template)]