renamed editor
This commit is contained in:
@@ -30,6 +30,29 @@ pub async fn create_post(
|
||||
.join("posts")
|
||||
.join(format!("{}.md", payload.slug));
|
||||
|
||||
// Handle renaming
|
||||
if let Some(ref old_slug) = payload.old_slug {
|
||||
if old_slug != &payload.slug {
|
||||
let old_file_path = state.data_dir.join("posts").join(format!("{}.md", old_slug));
|
||||
if old_file_path.exists() {
|
||||
// If new path already exists and it's different from old path, error out
|
||||
if file_path.exists() {
|
||||
return Err(AppError::BadRequest(
|
||||
"A post with this new title already exists".to_string(),
|
||||
));
|
||||
}
|
||||
if let Err(e) = fs::rename(&old_file_path, &file_path) {
|
||||
error!("Rename error from {} to {}: {}", old_slug, payload.slug, e);
|
||||
return Err(AppError::Internal(
|
||||
"Rename error".to_string(),
|
||||
Some(e.to_string()),
|
||||
));
|
||||
}
|
||||
info!("Renamed post from {} to {}", old_slug, payload.slug);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut file_content = String::new();
|
||||
if let Some(ref summary) = payload.summary {
|
||||
if !summary.trim().is_empty() {
|
||||
|
||||
@@ -44,6 +44,7 @@ pub struct PostDetail {
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreatePostRequest {
|
||||
pub slug: String,
|
||||
pub old_slug: Option<String>,
|
||||
pub summary: Option<String>,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user