Skip to content

Commit

Permalink
feat: remove id from slug
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Aug 25, 2024
1 parent 08a5dbb commit a1c527a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ pub fn Component() -> impl IntoView {
on:click=move |_| {
increment_view.dispatch(post.id.id.to_string());
navigate(
&format!(
"/post/{}-{}",
post.slug.as_ref().map_or("", |v| v),
post.id.id.to_string(),
),
&format!("/post/{}", post.slug.as_ref().map_or("", |v| v)),
Default::default(),
);
}
Expand Down
16 changes: 2 additions & 14 deletions src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@ use crate::posts::select_post;
#[component]
pub fn Component() -> impl IntoView {
let params = use_params_map();
let id = move || {
params.with(|params| {
params
.get("slug")
.cloned()
.unwrap_or_default()
.split('-')
.last()
.unwrap_or_default()
.to_string()
})
};

let slug = move || params.with(|params| params.get("slug").cloned().unwrap_or_default());
let post = create_blocking_resource(
|| (),
move |_| async move { select_post(id()).await.unwrap() },
move |_| async move { select_post(slug()).await.unwrap() },
);

view! {
Expand Down
4 changes: 2 additions & 2 deletions src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ pub async fn select_posts() -> Result<Vec<Post>, ServerFnError> {
}

#[server(endpoint = "/post")]
pub async fn select_post(id: String) -> Result<Post, ServerFnError> {
pub async fn select_post(slug: String) -> Result<Post, ServerFnError> {
use crate::ssr::AppState;
use chrono::{DateTime, Utc};
use leptos::expect_context;

let AppState { db, .. } = expect_context::<AppState>();

let query = format!("SELECT *, author.* from post:{0}", id);
let query = format!(r#"SELECT *, author.* from post WHERE slug = "{slug}""#);
let query = db.query(&query).await;

if let Err(e) = query {
Expand Down

0 comments on commit a1c527a

Please sign in to comment.