mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
ref(backend) major refactor
- remove struct Post, use database schema and Map instead - add sub_title - replace NaiveDateTime with DateTime<Utc>
This commit is contained in:
9 files changed
+170
-159
No files matched your search
+19
-15
@@ -1,18 +1,22 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod post;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
||||
pub struct Post {
|
||||
pub post_id: Uuid,
|
||||
pub title: String,
|
||||
pub summary: String,
|
||||
pub content: String,
|
||||
pub last_update: NaiveDateTime,
|
||||
pub first_update: NaiveDateTime,
|
||||
}
|
||||
|
||||
pub const POST_DB_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Post
|
||||
(
|
||||
post_id UUID PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
summary TEXT,
|
||||
sub_title TEXT,
|
||||
content TEXT NOT NULL,
|
||||
last_update TIMESTAMPTZ NOT NULL,
|
||||
first_update TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
"##;
|
||||
|
||||
pub const TAG_DB_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Tag
|
||||
(
|
||||
post_id UUID NOT NULL,
|
||||
tag VARCHAR(255) NOT NULL
|
||||
);
|
||||
"##;
|
||||
+67
-53
@@ -4,67 +4,81 @@ use std::process::Command;
|
||||
use std::str;
|
||||
use std::str::FromStr;
|
||||
|
||||
use chrono::{NaiveDate, NaiveDateTime, Utc};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde_json::{Map, Value};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::constants::GLOBAL_TIME_FORMAT;
|
||||
use crate::markdown::meta::parse_meta;
|
||||
use crate::model::Post;
|
||||
|
||||
impl Post {
|
||||
pub fn from_path(path: String) -> Result<Self, Box<dyn Error>> {
|
||||
let md_content = fs::read_to_string(&path)?;
|
||||
let meta = parse_meta(&md_content);
|
||||
pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
let mut res = Map::new();
|
||||
|
||||
let post_id = match meta.get("id") {
|
||||
None => { Uuid::new_v4() }
|
||||
Some(id) => { Uuid::from_str(id.as_str()).expect("Invalid UUID String") }
|
||||
};
|
||||
let md_content = fs::read_to_string(&path)?;
|
||||
res.insert("content".to_string(), Value::from(md_content.to_owned()));
|
||||
|
||||
let title = match meta.get("title") {
|
||||
None => "Title Not Found".to_string(),
|
||||
Some(title) => title.to_owned(),
|
||||
};
|
||||
let summary = match meta.get("summary") {
|
||||
None => "".to_string(),
|
||||
Some(summary) => summary.to_owned(),
|
||||
};
|
||||
let meta = parse_meta(&md_content);
|
||||
let meta_map: Map<String, Value> = meta
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, Value::from(v)))
|
||||
.collect();
|
||||
res.insert("meta".to_string(), Value::from(meta_map));
|
||||
|
||||
let git_output = Command::new("git")
|
||||
.arg("--no-pager")
|
||||
.arg("log")
|
||||
.arg("-1")
|
||||
.arg("--format=%cI")
|
||||
.arg("--")
|
||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to execute git command");
|
||||
let post_id = match meta.get("id") {
|
||||
None => Uuid::new_v4(),
|
||||
Some(id) => Uuid::from_str(id.as_str()).expect("Invalid UUID String"),
|
||||
};
|
||||
res.insert("post_id".to_string(), Value::from(post_id.to_string()));
|
||||
|
||||
let last_update = if git_output.status.success() {
|
||||
NaiveDateTime::parse_from_str(
|
||||
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
||||
"%Y-%m-%dT%H:%M:%S%z",
|
||||
)
|
||||
.unwrap_or_else(|_| Utc::now().naive_utc())
|
||||
} else {
|
||||
Utc::now().naive_utc()
|
||||
};
|
||||
let title = match meta.get("title") {
|
||||
None => "Title Not Found".to_string(),
|
||||
Some(title) => title.to_owned(),
|
||||
};
|
||||
res.insert("title".to_string(), Value::from(title));
|
||||
|
||||
let first_update: NaiveDateTime = match meta.get("date") {
|
||||
None => Utc::now().naive_utc(),
|
||||
Some(date) => NaiveDateTime::from(
|
||||
NaiveDate::parse_from_str(date, "%Y-%m-%d%z")
|
||||
.expect("Failed to parse first_update time"),
|
||||
),
|
||||
};
|
||||
let summary = match meta.get("summary") {
|
||||
None => "".to_string(),
|
||||
Some(summary) => summary.to_owned(),
|
||||
};
|
||||
res.insert("summary".to_string(), Value::from(summary));
|
||||
|
||||
Ok(Post {
|
||||
post_id,
|
||||
title,
|
||||
summary,
|
||||
content: md_content,
|
||||
last_update,
|
||||
first_update,
|
||||
})
|
||||
}
|
||||
let git_output = Command::new("git")
|
||||
.arg("--no-pager")
|
||||
.arg("log")
|
||||
.arg("-1")
|
||||
.arg("--format=%cI")
|
||||
.arg("--")
|
||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to execute git command");
|
||||
let last_update: DateTime<Utc> = if git_output.status.success() {
|
||||
chrono::DateTime::parse_from_str(
|
||||
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
||||
GLOBAL_TIME_FORMAT,
|
||||
)
|
||||
.unwrap_or_else(|_| DateTime::from(Utc::now()))
|
||||
.to_utc()
|
||||
} else {
|
||||
Utc::now().to_utc()
|
||||
};
|
||||
res.insert(
|
||||
"last_update".to_string(),
|
||||
Value::from(last_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
||||
);
|
||||
|
||||
let first_update: DateTime<Utc> = match meta.get("date") {
|
||||
None => Utc::now().to_utc(),
|
||||
Some(date) => DateTime::from(
|
||||
DateTime::parse_from_str(format!("{}T000000", date).as_str(), "%Y-%m-%d%zT%H%M%S")
|
||||
.expect("Failed to parse first_update time"),
|
||||
),
|
||||
};
|
||||
res.insert(
|
||||
"first_update".to_string(),
|
||||
Value::from(first_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
||||
);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
Reference in new issue
Block a user