From 099bcaadc66607d77915dd6b7209519ec35581c9 Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 14 Apr 2021 17:07:10 +0200 Subject: [PATCH] List posts --- rust_web.org | 77 +++++++++++++++++++++ simple_text_board/Cargo.lock | 16 +++++ simple_text_board/Cargo.toml | 3 +- simple_text_board/src/main.rs | 23 ++++-- simple_text_board/src/models.rs | 20 +++++- simple_text_board/templates/index.html.tera | 11 +++ 6 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 simple_text_board/templates/index.html.tera diff --git a/rust_web.org b/rust_web.org index d7755be..15c7f6d 100644 --- a/rust_web.org +++ b/rust_web.org @@ -535,6 +535,83 @@ } #+END_SRC +**** Create a template to list all posts + +***** Add a template for the index page ~templates/index.html.tera~ + #+BEGIN_SRC html + {% extends "base" %} + + {% block content %} +

simple text board

+

index

+ + {% endblock content %} + #+END_SRC + +***** Add serde dependencies to ~Cargo.toml~ + #+BEGIN_SRC toml + chrono = { version = "0.4", features = ["serde"] } + ... + serde = { version = "1.0", features = ["derive"] } + #+END_SRC + +***** Add `Post` to ~src/models.rs~ + #+BEGIN_SRC rust + ... + use diesel::{QueryResult, RunQueryDsl, SqliteConnection}; + use serde::Serialize; + + ... + + #[derive(Debug, Queryable, Serialize)] + pub struct Post { + pub id: i32, + pub parent: Option, + pub timestamp: NaiveDateTime, + pub author: String, + pub email: String, + pub title: String, + pub content: String, + } + + impl Post { + pub fn get_all(connection: &SqliteConnection) -> QueryResult> { + posts::table.load::(connection) + } + } + #+END_SRC + +***** Add index template in ~src/main.rs~ + #+BEGIN_SRC rust + ... + use serde::Serialize; + + ... + + use models::{NewPost, Post}; + + ... + + #[derive(Serialize)] + struct Posts { + posts: Vec, + } + + #[get("/")] + fn index(conn: DbCon) -> Template { + Template::render( + "index", + Posts { + posts: Post::get_all(&conn).unwrap(), + }, + ) + } + #+END_SRC + * Conclusion ** What did we do? diff --git a/simple_text_board/Cargo.lock b/simple_text_board/Cargo.lock index 57dfce3..77d9e9e 100644 --- a/simple_text_board/Cargo.lock +++ b/simple_text_board/Cargo.lock @@ -195,6 +195,7 @@ dependencies = [ "libc", "num-integer", "num-traits", + "serde", "time", "winapi 0.3.9", ] @@ -1124,6 +1125,20 @@ name = "serde" version = "1.0.119" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.8", + "syn 1.0.58", +] [[package]] name = "serde_json" @@ -1169,6 +1184,7 @@ dependencies = [ "log 0.4.13", "rocket", "rocket_contrib", + "serde", ] [[package]] diff --git a/simple_text_board/Cargo.toml b/simple_text_board/Cargo.toml index 462cfb5..05a9df5 100644 --- a/simple_text_board/Cargo.toml +++ b/simple_text_board/Cargo.toml @@ -6,9 +6,10 @@ edition = "2018" [dependencies] rocket = "0.4" -chrono = "0.4" +chrono = { version = "0.4", features = ["serde"] } diesel = { version = "1.4", features = ["sqlite", "chrono"] } log = "0.4" +serde = { version = "1.0", features = ["derive"] } [dependencies.rocket_contrib] version = "0.4" diff --git a/simple_text_board/src/main.rs b/simple_text_board/src/main.rs index 75a4094..dcbf5d4 100644 --- a/simple_text_board/src/main.rs +++ b/simple_text_board/src/main.rs @@ -15,11 +15,12 @@ use rocket::{ routes, uri, FromForm, }; use rocket_contrib::{database, templates::Template}; +use serde::Serialize; mod models; mod schema; -use models::NewPost; +use models::{NewPost, Post}; #[database("sqlite")] struct DbCon(SqliteConnection); @@ -32,6 +33,21 @@ struct PostForm { content: String, } +#[derive(Serialize)] +struct Posts { + posts: Vec, +} + +#[get("/")] +fn index(conn: DbCon) -> Template { + Template::render( + "index", + Posts { + posts: Post::get_all(&conn).unwrap(), + }, + ) +} + #[get("/create")] fn create_form(flash: Option) -> Template { let mut context = HashMap::new(); @@ -73,11 +89,6 @@ fn create_post( } } -#[get("/")] -fn index() -> &'static str { - "Hello, world!" -} - fn main() { rocket::ignite() .mount("/", routes![index, create_form, create_post]) diff --git a/simple_text_board/src/models.rs b/simple_text_board/src/models.rs index 96edd43..dc8078b 100644 --- a/simple_text_board/src/models.rs +++ b/simple_text_board/src/models.rs @@ -1,6 +1,7 @@ use crate::schema::posts::{self, dsl::posts as table_posts}; use chrono::NaiveDateTime; -use diesel::{QueryResult, RunQueryDsl}; +use diesel::{QueryResult, RunQueryDsl, SqliteConnection}; +use serde::Serialize; #[derive(Insertable)] #[table_name = "posts"] @@ -18,3 +19,20 @@ impl NewPost<'_> { diesel::insert_into(table_posts).values(self).execute(conn) } } + +#[derive(Debug, Queryable, Serialize)] +pub struct Post { + pub id: i32, + pub parent: Option, + pub timestamp: NaiveDateTime, + pub author: String, + pub email: String, + pub title: String, + pub content: String, +} + +impl Post { + pub fn get_all(connection: &SqliteConnection) -> QueryResult> { + posts::table.load::(connection) + } +} diff --git a/simple_text_board/templates/index.html.tera b/simple_text_board/templates/index.html.tera new file mode 100644 index 0000000..5ab3970 --- /dev/null +++ b/simple_text_board/templates/index.html.tera @@ -0,0 +1,11 @@ +{% extends "base" %} + +{% block content %} +

simple text board

+

index

+
    + {% for post in posts %} +
  • {{ loop.index }} - {{ post.title }} - {{ post.author }}
  • + {% endfor %} +
+{% endblock content %}