Skip to content

Commit ae036d2

Browse files
committed
feat: add meta_title
1 parent 7386f7a commit ae036d2

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/posts.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct Post {
2020
updated_at: String,
2121
author_id: String,
2222
image_url: Option<String>,
23+
meta_title: Option<String>,
2324
tags: String,
2425
}
2526

@@ -33,6 +34,19 @@ pub struct PostReply {
3334
author_id: String,
3435
}
3536

37+
fn get_meta_title(post: &Post) -> String {
38+
let meta_title = if let Some(meta_title_string) = &post.meta_title {
39+
return meta_title_string.to_string();
40+
} else {
41+
"".to_string()
42+
};
43+
if post.title.len() > 30 {
44+
meta_title
45+
} else {
46+
post.title.to_string()
47+
}
48+
}
49+
3650
fn insert_post(mut conn: mysql::PooledConn, author_id: String, post: Post) -> impl IntoResponse {
3751
let post_id = generate_truncated_uuid();
3852
let uuid = Uuid::new_v4().to_string();
@@ -188,6 +202,24 @@ fn insert_post(mut conn: mysql::PooledConn, author_id: String, post: Post) -> im
188202
Err(e) => tracing::error!("failed to insert post revision: {:?}", &e),
189203
}
190204

205+
let post_meta_id = generate_truncated_uuid();
206+
let meta_title = get_meta_title(&post);
207+
let result_meta = conn.exec_drop(
208+
"INSERT INTO posts_meta (id, post_id, meta_title, meta_description) VALUES (?, ?, ?, ?)",
209+
(
210+
&post_meta_id,
211+
&post_id,
212+
&meta_title.clone(),
213+
&post.excerpt.clone(),
214+
));
215+
match result_meta {
216+
Ok(_) => {
217+
tracing::info!("inserted post meta");
218+
}
219+
Err(err_resuolt_meta) => {
220+
tracing::error!("meta failed to insert new author: {:?}", &err_resuolt_meta);
221+
}
222+
}
191223
let response = PostReply {
192224
id: post_id,
193225
title: post.title,
@@ -198,7 +230,8 @@ fn insert_post(mut conn: mysql::PooledConn, author_id: String, post: Post) -> im
198230
};
199231
(StatusCode::CREATED, Json(response)).into_response()
200232
}
201-
Err(_) => {
233+
Err(error) => {
234+
tracing::error!("Error post inset, {:?}", error);
202235
tracing::error!("add_post failed to insert new post");
203236
(
204237
StatusCode::INTERNAL_SERVER_ERROR,
@@ -227,12 +260,17 @@ pub async fn add_post(Json(post): Json<Post>) -> impl IntoResponse {
227260
};
228261

229262
let query = "SELECT user_id FROM users_migration WHERE external_id = :external_id";
263+
tracing::info!("search author_id: {:?}", post.author_id);
264+
230265
let res_author: Option<String> = conn
231266
.exec_first(query, params! { "external_id" => post.author_id.clone() })
232267
.unwrap_or(None);
233268

269+
tracing::info!("search author: {:?}", res_author);
270+
234271
match res_author {
235272
Some(author_id) => {
273+
tracing::info!("author id found: {}", author_id);
236274
tracing::info!("author id found: {}", author_id);
237275
insert_post(conn, author_id, post).into_response()
238276
}

0 commit comments

Comments
 (0)