From 131789bf83d0ad1667a87628d56ba48112e256a9 Mon Sep 17 00:00:00 2001 From: Lumither Tao Date: Sat, 4 May 2024 15:55:00 +0800 Subject: [PATCH] feat(backend/post) new id strategy --- backend/src/model/post.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/src/model/post.rs b/backend/src/model/post.rs index 81877fd..025a16f 100644 --- a/backend/src/model/post.rs +++ b/backend/src/model/post.rs @@ -2,6 +2,7 @@ use std::{env, fs}; use std::error::Error; use std::process::Command; use std::str; +use std::str::FromStr; use chrono::{NaiveDate, NaiveDateTime, Utc}; use uuid::Uuid; @@ -14,6 +15,11 @@ impl Post { let md_content = fs::read_to_string(&path)?; let meta = parse_meta(&md_content); + let post_id = match meta.get("id") { + None => { Uuid::new_v4() } + Some(id) => { Uuid::from_str(id.as_str()).expect("Invalid UUID String") } + }; + let title = match meta.get("title") { None => "Title Not Found".to_string(), Some(title) => title.to_owned(), @@ -53,7 +59,7 @@ impl Post { }; Ok(Post { - post_id: Uuid::new_v4(), + post_id, title, summary, content: md_content,