Implement the save_all method.
This commit is contained in:
@@ -20,19 +20,21 @@ impl VideoService {
|
||||
|
||||
pub fn list_root_folder(&self) -> Result<Vec<LibraryItem>, VideoError> {
|
||||
let read_items_with_possibly_no_id = self.video_port.list_root_folder()?;
|
||||
|
||||
|
||||
let new_items = read_items_with_possibly_no_id.iter()
|
||||
.filter(|item| item.id.is_none())
|
||||
.map(LibraryRawItem::map_to_library_item)
|
||||
.collect();
|
||||
|
||||
.collect::<Vec<LibraryItem>>();
|
||||
|
||||
let already_existing_items = read_items_with_possibly_no_id.iter()
|
||||
.filter(|item| item.id.is_some())
|
||||
.map(LibraryRawItem::map_to_library_item)
|
||||
.collect::<Vec<LibraryItem>>();
|
||||
|
||||
self.video_port.save_all(&new_items)?;
|
||||
|
||||
|
||||
if !new_items.is_empty() {
|
||||
self.video_port.save_all(&new_items)?;
|
||||
}
|
||||
|
||||
let all_items = already_existing_items.into_iter().chain(new_items).collect();
|
||||
Ok(all_items)
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ impl LibraryItemAdapter {
|
||||
|
||||
fn does_item_belong_to_db_items(database_items: &Vec<LibraryRawItem>, file_system_item: &LibraryRawItem) -> bool {
|
||||
database_items.iter()
|
||||
.any(|database_item| database_item.id == file_system_item.id)
|
||||
.any(|database_item| database_item.name == file_system_item.name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,8 +103,9 @@ impl VideoPort for LibraryItemAdapter {
|
||||
let entities = library_items.iter()
|
||||
.map(LibraryItemEntity::from_library_item)
|
||||
.collect::<Vec<LibraryItemEntity>>();
|
||||
|
||||
self.repository.save_all(entities)?;
|
||||
|
||||
self.repository.save_all(entities)
|
||||
.map_err(|_| VideoError::FileReadingTechnicalIssue)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -28,15 +28,15 @@ impl LibraryItemEntity {
|
||||
Self {
|
||||
id: library_item.id.clone(),
|
||||
name: library_item.name.clone(),
|
||||
r#type: Self::map_type_to_i8(library_item.r#type)?,
|
||||
r#type: Self::map_type_to_i8(library_item.r#type),
|
||||
parent_folder_id: None
|
||||
}
|
||||
}
|
||||
|
||||
fn map_type_to_i8(item_type: LibraryItemType) -> Result<i8, Error> {
|
||||
fn map_type_to_i8(item_type: LibraryItemType) -> i8 {
|
||||
match item_type {
|
||||
LibraryItemType::VIDEO => Ok(0),
|
||||
LibraryItemType::FOLDER => Ok(1)
|
||||
LibraryItemType::VIDEO => 0,
|
||||
LibraryItemType::FOLDER => 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,30 +62,27 @@ impl LibraryItemSqliteRepository {
|
||||
)
|
||||
}
|
||||
|
||||
fn save_all(&self, entities: Vec<LibraryItemEntity>) -> Result<(), Error> {
|
||||
pub fn save_all(&self, entities: Vec<LibraryItemEntity>) -> Result<(), Error> {
|
||||
let database_connection = self.get_connection();
|
||||
|
||||
/*
|
||||
Expected query if there is one single item in "entities":
|
||||
INSERT INTO library_item (id, name, type) VALUES (?1, ?2, ?3);
|
||||
|
||||
Expected query if there are two items in "entities":
|
||||
INSERT INTO library_item (id, name, type) VALUES (?1, ?2, ?3), ($4, $5, $6);
|
||||
*/
|
||||
let mut query = String::from("INSERT INTO library_item (id, name, type) VALUES ");
|
||||
let mut params: Vec<rusqlite::types::Value> = Vec::new();
|
||||
|
||||
for (i, entity) in entities.iter().enumerate() {
|
||||
if i > 0 {
|
||||
query.push_str(", ");
|
||||
}
|
||||
query.push_str(&format!("(${}, ${}, ${})", i * 3 + 1, i * 3 + 2, i * 3 + 3));
|
||||
params.push(entity.id.to_string().into());
|
||||
params.push(entity.name.clone().into());
|
||||
params.push(entity.r#type.into());
|
||||
}
|
||||
|
||||
let mut prepared_statement = database_connection
|
||||
.prepare(
|
||||
"INSERT INTO library_item (
|
||||
id,
|
||||
name,
|
||||
type,
|
||||
) VALUES (?1, ?2, ?3);",
|
||||
)
|
||||
.prepare(&query)
|
||||
.expect("Unable to prepare statement for user saving…");
|
||||
prepared_statement.execute(rusqlite::params![
|
||||
entity.id.to_string(),
|
||||
entity.name,
|
||||
entity.r#type
|
||||
]).expect("Unable to insert the library items.");
|
||||
|
||||
prepared_statement.execute(rusqlite::params_from_iter(params))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user