remove path_cache
This commit is contained in:
@@ -30,7 +30,6 @@ pub struct Folder {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub parent_id: Option<Uuid>,
|
||||
pub path_cache: Option<String>,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
}
|
||||
@@ -41,7 +40,6 @@ pub struct NewFolder {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub parent_id: Option<Uuid>,
|
||||
pub path_cache: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||
|
||||
@@ -55,7 +55,6 @@ pub struct FolderInfo {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub parent_id: Option<Uuid>,
|
||||
pub path_cache: Option<String>,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
@@ -97,12 +96,10 @@ pub async fn ensure_folder_path(
|
||||
let folder = if let Some(folder) = existing {
|
||||
folder
|
||||
} else {
|
||||
let path_cache = build_path_cache(conn, current_parent, name)?;
|
||||
let new_folder = NewFolder {
|
||||
id: Uuid::new_v4(),
|
||||
name: name.to_string(),
|
||||
parent_id: current_parent,
|
||||
path_cache,
|
||||
};
|
||||
|
||||
diesel::insert_into(folders::table)
|
||||
@@ -133,13 +130,11 @@ pub async fn create_folder(
|
||||
}
|
||||
|
||||
let mut conn = state.db()?;
|
||||
let path_cache = build_path_cache(&mut conn, payload.parent_id, &payload.name)?;
|
||||
|
||||
let new_folder = NewFolder {
|
||||
id: Uuid::new_v4(),
|
||||
name: payload.name.trim().to_string(),
|
||||
parent_id: payload.parent_id,
|
||||
path_cache,
|
||||
};
|
||||
|
||||
diesel::insert_into(folders::table)
|
||||
@@ -346,49 +341,24 @@ pub async fn update_folder(
|
||||
));
|
||||
}
|
||||
|
||||
let new_path = build_path_cache(conn, next_parent, &new_name)?;
|
||||
|
||||
diesel::update(folders::table.find(folder_id))
|
||||
.set((
|
||||
folders::parent_id.eq(next_parent),
|
||||
folders::name.eq(&new_name),
|
||||
folders::path_cache.eq(new_path),
|
||||
))
|
||||
.execute(conn)?;
|
||||
|
||||
refresh_descendant_paths(conn, folder_id)?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
fn build_path_cache(
|
||||
conn: &mut PgConnection,
|
||||
parent_id: Option<Uuid>,
|
||||
name: &str,
|
||||
) -> AppResult<Option<String>> {
|
||||
let path = if let Some(parent_id) = parent_id {
|
||||
let parent: Folder = folders::table.find(parent_id).first(conn)?;
|
||||
let base = parent
|
||||
.path_cache
|
||||
.unwrap_or_else(|| "/".to_string())
|
||||
.trim_end_matches('/')
|
||||
.to_string();
|
||||
format!("{}/{}", base, name)
|
||||
} else {
|
||||
format!("/{}", name)
|
||||
};
|
||||
Ok(Some(path))
|
||||
}
|
||||
|
||||
fn folder_to_info(folder: Folder) -> FolderInfo {
|
||||
FolderInfo {
|
||||
id: folder.id,
|
||||
name: folder.name,
|
||||
parent_id: folder.parent_id,
|
||||
path_cache: folder.path_cache,
|
||||
created_at: to_iso(folder.created_at),
|
||||
updated_at: to_iso(folder.updated_at),
|
||||
}
|
||||
@@ -412,19 +382,3 @@ pub(super) fn gather_descendant_folder_ids(
|
||||
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
fn refresh_descendant_paths(conn: &mut PgConnection, parent_id: Uuid) -> AppResult<()> {
|
||||
let children: Vec<Folder> = folders::table
|
||||
.filter(folders::parent_id.eq(Some(parent_id)))
|
||||
.load(conn)?;
|
||||
|
||||
for child in children {
|
||||
let path_cache = build_path_cache(conn, Some(parent_id), &child.name)?;
|
||||
diesel::update(folders::table.find(child.id))
|
||||
.set(folders::path_cache.eq(path_cache))
|
||||
.execute(conn)?;
|
||||
refresh_descendant_paths(conn, child.id)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -86,8 +86,6 @@ diesel::table! {
|
||||
#[max_length = 255]
|
||||
name -> Varchar,
|
||||
parent_id -> Nullable<Uuid>,
|
||||
#[max_length = 1000]
|
||||
path_cache -> Nullable<Varchar>,
|
||||
created_at -> Timestamptz,
|
||||
updated_at -> Timestamptz,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user