feat(api) add refresh.rs

This commit is contained in:
lumither committed 2024-05-03 02:17:11 +08:00
1 parent dce7a28b89
commit 2d42e933c8
10 files changed
+127 -9

No files matched your search

+7
View File
@@ -5,3 +5,10 @@ CREATE TABLE IF NOT EXISTS Post
content TEXT NOT NULL,
last_update TIMESTAMP
);
INSERT INTO Post (post_id, title, content, last_update)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT (post_id)
DO UPDATE SET title = excluded.title,
content = excluded.content,
last_update = excluded.last_update
File renamed without changes.
File renamed without changes.
+2
View File
@@ -0,0 +1,2 @@
pub mod get_post;
pub mod get_post_list;
+2 -2
View File
@@ -1,2 +1,2 @@
pub mod get_post;
pub mod get_post_list;
pub mod get;
pub mod post;
+1
View File
@@ -0,0 +1 @@
pub mod refresh;
+88
View File
@@ -0,0 +1,88 @@
use std::env;
use std::path::Path;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use serde_json::{json, Value};
use sqlx::{PgPool, query};
use crate::model::Post;
pub async fn refresh(
State(db_connection): State<PgPool>,
) -> Result<StatusCode, (StatusCode, Json<Value>)> {
let data_repo_dir = match env::var("GIT_WORK_DIR") {
Ok(value) => value,
Err(_) => {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json::from(json!(format!(
"{{error: {}}}",
"error at parsing environment variable: GIT_WORK_DIR"
))),
));
}
};
let md_list = search_md(Path::new(&data_repo_dir));
let post_list: Vec<Post> = md_list
.into_iter()
.map(Post::from_path)
.collect();
for post in post_list {
match query(
r##"
INSERT INTO Post (post_id, title, content, last_update)
VALUES ($1, $2, $3, $4)
ON CONFLICT (post_id)
DO UPDATE SET title = excluded.title,
content = excluded.content,
last_update = excluded.last_update
"##,
)
.bind(post.post_id)
.bind(post.title)
.bind(post.content)
.bind(post.last_update)
.execute(&db_connection)
.await
{
Ok(_) => {
dbg!("inserted");
}
Err(e) => {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json::from(json!(format!("{{error: {}}}", e.to_string()))),
));
}
}
}
Ok(StatusCode::OK)
}
fn search_md(entry: &Path) -> Vec<String> {
if entry.ends_with(".git") {
return vec![];
}
if entry.is_dir() {
if let Ok(files) = entry.read_dir() {
files
.flat_map(|file| {
let file_path = file.unwrap().path();
search_md(file_path.as_path())
})
.collect()
} else {
vec![]
}
} else if entry.extension().is_some_and(|ext| ext == "md") {
vec![entry.to_str().unwrap().to_string()]
} else {
vec![]
}
}
+5 -3
View File
@@ -2,12 +2,13 @@ use std::env;
use std::error::Error;
use axum::Router;
use axum::routing::get;
use axum::routing::{get, post};
use dotenv::dotenv;
use sqlx::postgres::PgPoolOptions;
use crate::api::get_post::get_post;
use crate::api::get_post_list::get_post_list;
use crate::api::get::get_post::get_post;
use crate::api::get::get_post_list::get_post_list;
use crate::api::post::refresh::refresh;
mod api;
mod model;
@@ -29,6 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.expect("failed to load database");
let app = Router::new()
.route("/api/post/refresh", post(refresh))
.route("/api/get/post_list", get(get_post_list))
.route("/api/get/post/:post_id", get(get_post))
.with_state(db_pool);
+6 -4
View File
@@ -3,10 +3,12 @@ use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
pub mod post;
#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct Post {
post_id: Uuid,
title: String,
content: String,
last_update: NaiveDateTime,
pub post_id: Uuid,
pub title: String,
pub content: String,
pub last_update: NaiveDateTime,
}
+16
View File
@@ -0,0 +1,16 @@
use chrono::Utc;
use uuid::Uuid;
use crate::model::Post;
impl super::Post {
pub fn from_path(path: String) -> Self {
// todo: remove test
Post {
post_id: Uuid::new_v4(),
title: "test title".to_string(),
content: "test content".to_string(),
last_update: Utc::now().naive_utc(),
}
}
}