Start to implement the save_all method.

This commit is contained in:
Florian THIERRY
2026-07-17 15:01:47 +02:00
parent ad44b181cb
commit 437314f304
4 changed files with 46 additions and 16 deletions
@@ -11,18 +11,18 @@ pub fn initialise_database_tables(connection: &Arc<Mutex<Connection>>) -> Result
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
initialisation_code VARCHAR initialisation_code VARCHAR
); );
CREATE TABLE IF NOT EXISTS system_resources (
id TEXT PRIMARY KEY,
datetime INTEGER NOT NULL,
cpu_usage INTEGER NOT NULL,
total_memory INTEGER NOT NULL,
used_memory INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS \"user\" ( CREATE TABLE IF NOT EXISTS \"user\" (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
username VARCHAR NOT NULL, username VARCHAR NOT NULL,
encrypted_password VARCHAR NOT NULL encrypted_password VARCHAR NOT NULL
); );
CREATE TABLE IF NOT EXISTS library_item (
id TEXT PRIMARY KEY,
name VARCHAR NOT NULL,
type INTEGER NOT NULL,
parent_folder_id TEXT NULL,
FOREIGN KEY (parent_folder_id) REFERENCES library_item (id)
);
COMMIT; COMMIT;
" "
)?; )?;
@@ -100,6 +100,11 @@ impl VideoPort for LibraryItemAdapter {
} }
fn save_all(&self, library_items: &Vec<LibraryItem>) -> Result<(), VideoError> { fn save_all(&self, library_items: &Vec<LibraryItem>) -> Result<(), VideoError> {
let entities = library_items.iter()
.map(LibraryItemEntity::from_library_item)
.collect::<Vec<LibraryItemEntity>>();
self.repository.save_all(entities)?;
Ok(()) Ok(())
} }
+1 -3
View File
@@ -24,15 +24,13 @@ pub struct LibraryItemEntity {
} }
impl LibraryItemEntity { impl LibraryItemEntity {
pub fn from_library_item(library_item: &LibraryItem) -> Result<Self, Error> { pub fn from_library_item(library_item: &LibraryItem) -> Self {
Ok(
Self { Self {
id: library_item.id.clone(), id: library_item.id.clone(),
name: library_item.name.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 parent_folder_id: None
} }
)
} }
fn map_type_to_i8(item_type: LibraryItemType) -> Result<i8, Error> { fn map_type_to_i8(item_type: LibraryItemType) -> Result<i8, Error> {
@@ -61,4 +61,31 @@ impl LibraryItemSqliteRepository {
} }
) )
} }
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 prepared_statement = database_connection
.prepare(
"INSERT INTO library_item (
id,
name,
type,
) VALUES (?1, ?2, ?3);",
)
.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.");
Ok(())
}
} }