Skip to content

Commit

Permalink
refactor codes
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Jan 12, 2024
1 parent 392f6e6 commit 5215c9a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 77 deletions.
126 changes: 50 additions & 76 deletions src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,54 @@ use crate::response;
use crate::response::WithTotal;
use crate::MySQLDb;

fn build_tree(arr: &Vec<Category>, upload_url: &str) -> Vec<response::category::Category> {
let mut map: HashMap<Option<i64>, Vec<response::category::Category>> = HashMap::new();
let mut categories = arr
.clone()
.iter()
.map(|category| {
let icon = category.icon.clone();

let icon = if icon.starts_with("http") || icon.starts_with("https") {
icon
} else {
format!("{}/{}", upload_url, icon)
};

response::category::Category {
id: category.id,
name: category.name.clone(),
description: category.description.clone(),
icon,
children: None,
}
})
.collect::<Vec<response::category::Category>>();

for category in arr {
map.entry(category.parent_id)
.or_default()
.push(response::category::Category::from(category.clone()));
}

for category in &mut categories {
if let Some(children) = map.get(&Some(category.id)) {
category.children = Some(children.to_vec());
}
}

let ids_to_be_deleted = arr
.iter()
.filter(|x| x.parent_id.is_some())
.map(|x| x.id)
.collect::<Vec<i64>>();

for id in ids_to_be_deleted {
categories.remove(categories.iter().position(|x| x.id == id).unwrap());
}

categories
}
pub async fn get_categories(
page: i64,
size: i64,
Expand Down Expand Up @@ -90,7 +138,7 @@ pub async fn get_categories(
.get::<i64, &str>("count"),
};

let mut categories = match search {
let categories = match search {
Some(search) => query_as::<_, Category>(
r#"
WITH RECURSIVE category_hierarchy AS (
Expand Down Expand Up @@ -178,83 +226,9 @@ pub async fn get_categories(
.await?,
};

let mut category_map: HashMap<i64, Vec<Category>> = HashMap::new();

for category in &categories {
if let Some(parent_id) = category.parent_id {
category_map
.entry(parent_id)
.or_default()
.push(category.clone());
}
}

for x in category_map.keys() {
let ids = category_map
.get(x)
.unwrap()
.iter()
.map(|x| x.id)
.collect::<Vec<i64>>();

for id in ids {
categories.remove(categories.iter().position(|x| x.id == id).unwrap());
}
}

let mut categories: Vec<response::category::Category> = categories
.into_iter()
.map(|category| {
let icon = category.icon.clone();

let icon = if icon.starts_with("http") || icon.starts_with("https") {
icon
} else {
format!("{}/{}", upload_url, icon)
};

response::category::Category {
id: category.id,
name: category.name,
description: category.description,
icon,
children: None,
}
})
.collect();

for caregory in &mut categories {
if category_map.get(&caregory.id).is_some() {
let children = category_map.get(&caregory.id).unwrap();

caregory.children = Some(
children
.into_iter()
.map(|category| {
let icon = category.icon.clone();

let icon = if icon.starts_with("http") || icon.starts_with("https") {
icon
} else {
format!("{}/{}", upload_url, icon)
};

response::category::Category {
id: category.id,
name: category.name.clone(),
description: category.description.clone(),
icon,
children: None,
}
})
.collect(),
);
}
}

Ok(WithTotal {
total,
data: categories,
data: build_tree(&categories, upload_url),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/response/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Serialize;

use crate::models::category;

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Clone)]
pub struct Category {
pub id: i64,
pub name: String,
Expand Down

0 comments on commit 5215c9a

Please sign in to comment.