List posts
This commit is contained in:
parent
4948195495
commit
099bcaadc6
6 changed files with 142 additions and 8 deletions
77
rust_web.org
77
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 %}
|
||||
<h1>simple text board</h1>
|
||||
<h2>index</h2>
|
||||
<ul>
|
||||
{% for post in posts %}
|
||||
<li>{{ loop.index }} - <a href="post/{{ post.id }}">{{ post.title }}</a> - {{ post.author }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% 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<i32>,
|
||||
pub timestamp: NaiveDateTime,
|
||||
pub author: String,
|
||||
pub email: String,
|
||||
pub title: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub fn get_all(connection: &SqliteConnection) -> QueryResult<Vec<Post>> {
|
||||
posts::table.load::<Post>(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<Post>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(conn: DbCon) -> Template {
|
||||
Template::render(
|
||||
"index",
|
||||
Posts {
|
||||
posts: Post::get_all(&conn).unwrap(),
|
||||
},
|
||||
)
|
||||
}
|
||||
#+END_SRC
|
||||
|
||||
* Conclusion
|
||||
|
||||
** What did we do?
|
||||
|
|
16
simple_text_board/Cargo.lock
generated
16
simple_text_board/Cargo.lock
generated
|
@ -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]]
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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<Post>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(conn: DbCon) -> Template {
|
||||
Template::render(
|
||||
"index",
|
||||
Posts {
|
||||
posts: Post::get_all(&conn).unwrap(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/create")]
|
||||
fn create_form(flash: Option<FlashMessage>) -> 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])
|
||||
|
|
|
@ -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<i32>,
|
||||
pub timestamp: NaiveDateTime,
|
||||
pub author: String,
|
||||
pub email: String,
|
||||
pub title: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub fn get_all(connection: &SqliteConnection) -> QueryResult<Vec<Post>> {
|
||||
posts::table.load::<Post>(connection)
|
||||
}
|
||||
}
|
||||
|
|
11
simple_text_board/templates/index.html.tera
Normal file
11
simple_text_board/templates/index.html.tera
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>simple text board</h1>
|
||||
<h2>index</h2>
|
||||
<ul>
|
||||
{% for post in posts %}
|
||||
<li>{{ loop.index }} - <a href="post/{{ post.id }}">{{ post.title }}</a> - {{ post.author }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock content %}
|
Loading…
Reference in a new issue