Skip to content

Commit 74ed1bd

Browse files
committed
feat: mapping using ORM
1 parent 3760552 commit 74ed1bd

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ opt-level= 's'
2626
askama = {version="0.12.0", features=["markdown"]}
2727
tokio = {version="1.28.0", features = ["macros", "rt-multi-thread"]}
2828
axum = "0.6.17"
29-
kip-sql = "0.0.1-alpha.0"
29+
kip-sql = "0.0.1-alpha.3"
3030
tower-http = {version = "0.4", features=["full"]}
3131
itertools = "0.10"
3232
chrono = "0.4.26"

src/main.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ use tower_http::services::ServeDir;
99
use std::sync::Arc;
1010

1111
use askama::Template;
12+
use chrono::NaiveDateTime;
1213
use itertools::Itertools;
1314
use kip_sql::db::{Database, DatabaseError};
15+
use kip_sql::implement_from_tuple;
1416
use kip_sql::storage::kip::KipStorage;
17+
use kip_sql::types::value::DataValue;
18+
use kip_sql::types::tuple::Tuple;
19+
use kip_sql::types::LogicalType;
1520

1621
pub(crate) const BANNER: &str = "
1722
█████ ████ ███ ███████████ ████
@@ -32,7 +37,7 @@ pub(crate) const BANNER: &str = "
3237
#[template(path = "posts.html")]
3338
struct PostTemplate<'a> {
3439
post_title: &'a str,
35-
post_date: &'a str,
40+
post_date: String,
3641
post_body: &'a str,
3742
}
3843

@@ -47,13 +52,31 @@ pub struct IndexTemplate<'a> {
4752

4853
// SQL query will return all posts
4954
// into a Vec<Post>
50-
#[derive(Debug, Clone)]
55+
#[derive(Debug, Clone, Default)]
5156
pub struct Post {
5257
pub post_title: String,
53-
pub post_date: String,
58+
pub post_date: NaiveDateTime,
5459
pub post_body: String,
5560
}
5661

62+
implement_from_tuple!(Post, (
63+
post_title: String => |post: &mut Post, value: DataValue| {
64+
if let Some(title) = value.utf8() {
65+
post.post_title = title;
66+
}
67+
},
68+
post_date: NaiveDateTime => |post: &mut Post, value: DataValue| {
69+
if let Some(date_time) = value.datetime() {
70+
post.post_date = date_time;
71+
}
72+
},
73+
post_body: String => |post: &mut Post, value: DataValue| {
74+
if let Some(body) = value.utf8() {
75+
post.post_body = body;
76+
}
77+
}
78+
));
79+
5780
// Our custom Askama filter to replace spaces with dashes in the title
5881
mod filters {
5982

@@ -67,14 +90,19 @@ mod filters {
6790
// Path to extract the query: localhost:4000/post/thispart
6891
// State that holds a Vec<Post> used to render the post that the query matches
6992
async fn post(Path(query_title): Path<String>, State(state): State<Arc<Database<KipStorage>>>) -> impl IntoResponse {
70-
let mut template = PostTemplate{post_title: "none", post_date: "none", post_body: "none"};
93+
let mut template = PostTemplate{post_title: "none", post_date: "none".to_string(), post_body: "none"};
7194
let posts = get_posts(&state).await.unwrap();
7295
// if the user's query matches a post title then render a template
7396
for post in &posts {
7497
if query_title == post.post_title {
98+
let post_date = post
99+
.post_date
100+
.format("%Y-%m-%d %H:%M:%S")
101+
.to_string();
102+
75103
template = PostTemplate{
76104
post_title: &post.post_title,
77-
post_date: &post.post_date,
105+
post_date,
78106
post_body: &post.post_body
79107
};
80108
break;
@@ -136,12 +164,6 @@ async fn get_posts(kip_sql: &Database<KipStorage>) -> Result<Vec<Post>, Database
136164
Ok(kip_sql.run("select post_title, post_date, post_body from myposts")
137165
.await?
138166
.into_iter()
139-
.map(|tuple| {
140-
Post {
141-
post_title: tuple.values[0].to_string(),
142-
post_date: tuple.values[1].to_string(),
143-
post_body: tuple.values[2].to_string(),
144-
}
145-
})
167+
.map(|tuple| Post::from(tuple))
146168
.collect_vec())
147169
}

0 commit comments

Comments
 (0)