use crate::generators::create_file;
use crate::writers::write_to_new_get_all_controller;
use eyre::Error;
use std::path::Path;
pub async fn create_get_all_controller(model_name: String) -> Result<(), Error> {
let model_path = format!("./src/models/{}.rs", model_name);
let model_path = Path::new(&model_path);
if !model_path.exists() {
println!("There is no model with that name. Please create a model with that name.");
return Ok(());
}
let controller_with_folder_dir = format!("./src/controllers/{}/{}.rs", model_name, model_name);
let mut has_controller = false;
let controller_path = Path::new(&controller_with_folder_dir);
if controller_path.exists() {
has_controller = true;
}
let current_dir = format!("./src/controllers/{}.rs", model_name);
let controller_path = Path::new(¤t_dir);
if controller_path.exists() {
has_controller = true;
}
if !has_controller {
create_file(&format!("./src/controllers/{}.rs", model_name)).unwrap_or_else(|why| {
println!("Failed to create file: {:?}", why.to_string());
});
}
write_to_new_get_all_controller(model_name.clone().to_string())
.expect("Failed to write to new get all controller");
Ok(())
}