From 1832a5bdd57c29165affd4b0cd11829223d5279a Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sun, 17 Nov 2019 16:09:36 -0600 Subject: [PATCH] add method and route to display posts --- src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3cc03df..c23c714 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,10 +74,37 @@ fn index(page: Option) -> Template { Template::render("index", context) } +fn get_post(id_number: i32) -> Post { + use schema::posts::dsl::*; + + let connection = establish_connection(); + let post = posts.find(id_number).first(&connection); + + post.unwrap() +} + +#[get("/posts/")] +fn show_post(title: String) -> Template { + let mut context = Context::new(); + + let title_splited: Vec<&str> = title.split('-').collect(); + let id = title_splited.last().unwrap().parse().unwrap(); + + let mut rng = rand::thread_rng(); + let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; + let title = str::replace(title_fmt, "{}", "CódigoComentado"); + + let post = get_post(id); + + context.insert("title", &title); + context.insert("post", &post); + Template::render("posts", context) +} + fn main() { rocket::ignite() .attach(Template::fairing()) - .mount("/", routes![index]) + .mount("/", routes![index, show_post]) .mount("/static", StaticFiles::from("static")) .launch(); }