diff --git a/src/main.rs b/src/main.rs index 1473706..42439b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,60 +27,102 @@ fn main() -> Result<(), csv::Error> { let mut rdr = csv::Reader::from_reader(file); let mut records: Vec = Vec::new(); - + let mut old_source_url: String = "".to_string(); + for result in rdr.deserialize() { let record: ExportRecord = result?; - println!("{:?}", record); + //println!("{:?}", record); + let mut temp_target_url_copy: Vec = record.path.split("/").map(|s| s.to_string()).collect(); let mut temp_target_url: Vec = record.path.split("/").map(|s| s.to_string()).collect(); - - // Modify to new target url - if temp_target_url[0].eq("gewuerze") { - temp_target_url.remove(0); + + if temp_target_url_copy.len() > 2 && temp_target_url_copy[temp_target_url_copy.len() - 2].parse::().ok().is_some() { + println!("{:#?}", temp_target_url_copy[temp_target_url_copy.len() - 2]); } - - if temp_target_url.len() > 2 && temp_target_url[temp_target_url.len() - 2].parse::().ok().is_some() { - temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name - } - - if !record.attr1.clone().unwrap().eq("NULL") { - temp_target_url.push(record.attr1.unwrap()); // Add article number if not null - } - - // Combine to new target url - let target_url = temp_target_url.join("/"); - - - // Build source url - let mut source_url_vec: Vec = Vec::new(); - source_url_vec.push(record.path); - - // Combine source url - let source_url = source_url_vec.join("/"); let sales_channel; let id = record.subshop_id.unwrap(); - + if id.clone().eq("3") { // Lodner Geschmackswelten - sales_channel = "0197660872e672e9ba3140ddf689f4fb"; + sales_channel = "019774250010707285e7d325962e0089"; + + if temp_target_url.len() > 2 && temp_target_url[temp_target_url.len() - 2].parse::().ok().is_some() && !record.attr1.clone().unwrap().eq("NULL") { + temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name + + //temp_target_url.insert(0, "pfeffer-kontor".to_string()); + + if !record.attr1.clone().unwrap().eq("NULL") { + temp_target_url.push(record.attr1.unwrap()); // Add article number if not null + } + + // Combine to new target url + let target_url = temp_target_url.join("/"); + + + // Build source url + let mut source_url_vec: Vec = Vec::new(); + source_url_vec.push(record.path); + + // Combine source url + let source_url = source_url_vec.join("/"); + + + if !old_source_url.eq(source_url.clone().as_str()) { + old_source_url = source_url.clone(); + + records.push(Record{ + source_url, + target_url: Some(target_url), + http_code: Some("301".to_string()), + enabled: Some("1".to_string()), + query_params_handling: Some("1".to_string()), // 1 to ignore query parameters + sales_channel_id: Some(sales_channel.to_string()), + }) + } + } + } else if id.clone().eq("1") { // Lodner Gewürze - sales_channel = "0197660872d9706581cbe77994a4caa1"; + //sales_channel = "0197742500027367a2f45220911057e8"; + sales_channel = "019774250010707285e7d325962e0089"; // Set Geschmackswelten ID because, both shops got combined + + + } else if id.clone().eq("2") { // Pfefferkontor - sales_channel = "0197660872e1717c9854226725628bf7"; + sales_channel = "01977425000c72f3b2f6015decfed1f4"; + + if temp_target_url.len() > 2 && temp_target_url[temp_target_url.len() - 2].parse::().ok().is_some() && !record.attr1.clone().unwrap().eq("NULL") { + temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name + + if !record.attr1.clone().unwrap().eq("NULL") { + temp_target_url.push(record.attr1.unwrap()); // Add article number if not null + } + + // Combine to new target url + let target_url = temp_target_url.join("/"); + + + // Build source url + let mut source_url_vec: Vec = Vec::new(); + source_url_vec.push(record.path); + + // Combine source url + let source_url = source_url_vec.join("/"); + + + records.push(Record{ + source_url, + target_url: Some(target_url), + http_code: Some("301".to_string()), + enabled: Some("1".to_string()), + query_params_handling: Some("1".to_string()), // 1 to ignore query parameters + sales_channel_id: Some(sales_channel.to_string()), + }) + } } else if id.eq("4") { // Crazy Snack sales_channel = "0197660872ea73468ba300fc2cb73d35"; } else { // Set for all when no subshop id sales_channel = ""; } - - records.push(Record{ - source_url, - target_url: Some(target_url), - http_code: Some("301".to_string()), - enabled: Some("1".to_string()), - query_params_handling: Some("1".to_string()), // 1 to ignore query parameters - sales_channel_id: Some(sales_channel.to_string()), - }) } let new_csv = File::create("redirects.csv")?;