diff --git a/backend/infrastructure/src/common/database_initialisation.rs b/backend/infrastructure/src/common/database_initialisation.rs index fb59fa2..d6bb689 100644 --- a/backend/infrastructure/src/common/database_initialisation.rs +++ b/backend/infrastructure/src/common/database_initialisation.rs @@ -11,18 +11,18 @@ pub fn initialise_database_tables(connection: &Arc>) -> Result id TEXT PRIMARY KEY, 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\" ( id TEXT PRIMARY KEY, username 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; " )?; diff --git a/backend/infrastructure/src/video/adapter.rs b/backend/infrastructure/src/video/adapter.rs index 8e374cf..79dce3e 100644 --- a/backend/infrastructure/src/video/adapter.rs +++ b/backend/infrastructure/src/video/adapter.rs @@ -100,6 +100,11 @@ impl VideoPort for LibraryItemAdapter { } fn save_all(&self, library_items: &Vec) -> Result<(), VideoError> { + let entities = library_items.iter() + .map(LibraryItemEntity::from_library_item) + .collect::>(); + + self.repository.save_all(entities)?; Ok(()) } diff --git a/backend/infrastructure/src/video/model.rs b/backend/infrastructure/src/video/model.rs index 4213765..539fddd 100644 --- a/backend/infrastructure/src/video/model.rs +++ b/backend/infrastructure/src/video/model.rs @@ -24,15 +24,13 @@ pub struct LibraryItemEntity { } impl LibraryItemEntity { - pub fn from_library_item(library_item: &LibraryItem) -> Result { - Ok( - Self { - id: library_item.id.clone(), - name: library_item.name.clone(), - r#type: Self::map_type_to_i8(library_item.r#type)?, - parent_folder_id: None - } - ) + pub fn from_library_item(library_item: &LibraryItem) -> Self { + Self { + id: library_item.id.clone(), + name: library_item.name.clone(), + r#type: Self::map_type_to_i8(library_item.r#type)?, + parent_folder_id: None + } } fn map_type_to_i8(item_type: LibraryItemType) -> Result { diff --git a/backend/infrastructure/src/video/repository.rs b/backend/infrastructure/src/video/repository.rs index adc4da3..baee4de 100644 --- a/backend/infrastructure/src/video/repository.rs +++ b/backend/infrastructure/src/video/repository.rs @@ -61,4 +61,31 @@ impl LibraryItemSqliteRepository { } ) } + + fn save_all(&self, entities: Vec) -> 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(()) + } } \ No newline at end of file