From b17e8a19eb6082338dcdaf0857dd8eda39e401c3 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Sat, 4 May 2024 00:35:47 +0800 Subject: [PATCH] feat(api) update /api/get/post_list --- backend/src/api/get/get_post_list.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/backend/src/api/get/get_post_list.rs b/backend/src/api/get/get_post_list.rs index 9984cee..6e12285 100644 --- a/backend/src/api/get/get_post_list.rs +++ b/backend/src/api/get/get_post_list.rs @@ -14,19 +14,24 @@ pub async fn get_post_list( .unwrap(); let mut res = Map::new(); - for post in post_list { - let mut tmp = Map::new(); - let post_id: Uuid = post.get("post_id"); - let title: String = post.get("title"); - let summary: String = post.get("summary"); - let last_update: NaiveDateTime = post.get("last_update"); + let posts: Vec = post_list + .into_iter() + .map(|post| { + let mut tmp = Map::new(); + let post_id: Uuid = post.get("post_id"); + let title: String = post.get("title"); + let summary: String = post.get("summary"); + let last_update: NaiveDateTime = post.get("last_update"); - tmp.insert("title".to_string(), json!(title)); - tmp.insert("summary".to_string(), json!(summary)); - tmp.insert("last_update".to_string(), json!(last_update)); + tmp.insert("post_id".to_string(), json!(post_id.to_string())); + tmp.insert("title".to_string(), json!(title)); + tmp.insert("summary".to_string(), json!(summary)); + tmp.insert("last_update".to_string(), json!(last_update)); - res.insert(post_id.to_string(), Value::from(tmp)); - } + Value::from(tmp) + }) + .collect(); + res.insert("posts".to_string(), Value::from(posts)); Ok(Json::from(Value::from(res))) }