Pass all_titles to add_all_titles as a Vec

This commit is contained in:
flyingscorpio@clevo 2023-01-03 21:25:57 +01:00
parent 0728ba9da8
commit c53f31d21a
2 changed files with 5 additions and 6 deletions

View file

@ -5,10 +5,11 @@
mod crud;
mod errors;
use crate::models::InsertTitle;
use errors::DatabaseError;
/// Insert all titles from Mirabel into the local database
pub fn add_all_titles() -> Result<(), DatabaseError> {
pub fn add_all_titles(all_titles: Vec<InsertTitle>) -> Result<(), DatabaseError> {
let connection = &mut crud::establish_connection()?;
Ok(())
@ -34,7 +35,6 @@ pub fn sql_example() -> Result<(), DatabaseError> {
vec![Some("fre")],
vec![],
Some("https://reseau-mirabel.info/revue/1915"),
)?;
println!("READ:");

View file

@ -32,17 +32,16 @@ pub fn run() -> Result<(), Box<dyn Error>> {
process::exit(1);
});
let all_titles = &all_titles
let all_titles = all_titles
.as_array()
.unwrap() // we just checked for Option above so we can safely unwrap here
.iter()
.map(|val| json_to_insert_title(val))
.collect::<Vec<InsertTitle>>();
dbg!(&all_titles);
// TODO: don't collect all_titles and pass the iterator to database::add_all_titles
// Add the fetched titles to the database
database::add_all_titles().unwrap_or_else(|err| {
database::add_all_titles(all_titles).unwrap_or_else(|err| {
eprintln!("{err}");
process::exit(1);
});