diff --git a/backend/infrastructure/src/video/adapter.rs b/backend/infrastructure/src/video/adapter.rs index 182c401..8dfc59c 100644 --- a/backend/infrastructure/src/video/adapter.rs +++ b/backend/infrastructure/src/video/adapter.rs @@ -10,9 +10,7 @@ use lumiere_domain::video::model::video::VideoFormat::{AVI, MKV, MP4, WEBM}; use crate::video::video_format_detector::get_content_type; #[derive(Clone)] -pub struct VideoAdapter { - -} +pub struct VideoAdapter {} impl VideoAdapter { pub fn new() -> Self { diff --git a/backend/infrastructure/src/video/mod.rs b/backend/infrastructure/src/video/mod.rs index bb40162..d40714e 100644 --- a/backend/infrastructure/src/video/mod.rs +++ b/backend/infrastructure/src/video/mod.rs @@ -1,3 +1,4 @@ pub mod adapter; pub mod model; -pub mod video_format_detector; \ No newline at end of file +pub mod video_format_detector; +mod repository; \ No newline at end of file diff --git a/backend/infrastructure/src/video/model.rs b/backend/infrastructure/src/video/model.rs index 22070c7..38a0cf0 100644 --- a/backend/infrastructure/src/video/model.rs +++ b/backend/infrastructure/src/video/model.rs @@ -1,4 +1,7 @@ use std::fs::File; +use std::io::{Error, ErrorKind}; +use uuid::Uuid; +use lumiere_domain::video::model::library_item::{LibraryItem, LibraryItemType}; pub struct TokioVideoFile { pub file: File @@ -10,4 +13,40 @@ impl TokioVideoFile { file: File::open(file_path).expect(format!("Failed to open file {file_path}").as_str()) } } +} + + +pub struct LibraryItemEntity { + pub id: Uuid, + pub name: String, + pub r#type: i8, + pub parent_folder_id: Option +} + +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 + } + ) + } + + fn map_type_to_i8(item_type: LibraryItemType) -> Result { + match item_type { + LibraryItemType::VIDEO => Ok(0), + LibraryItemType::FOLDER => Ok(1) + } + } + + fn map_type_from_i8(item_type_number: i8) -> Result { + match item_type_number { + 0 => Ok(LibraryItemType::VIDEO), + 1 => Ok(LibraryItemType::FOLDER), + unknown_value => Err(Error::new(ErrorKind::InvalidData, format!("Unknown library item type value from database: {unknown_value}"))) + } + } } \ No newline at end of file diff --git a/backend/infrastructure/src/video/repository.rs b/backend/infrastructure/src/video/repository.rs new file mode 100644 index 0000000..a882239 --- /dev/null +++ b/backend/infrastructure/src/video/repository.rs @@ -0,0 +1,65 @@ +use std::sync::{Arc, Mutex, MutexGuard}; +use rusqlite::{Connection, Error, Row}; +use rusqlite::fallible_iterator::FallibleIterator; +use uuid::Uuid; +use lumiere_domain::video::model::library_item::{LibraryItem, LibraryItemType}; +use crate::video::model::LibraryItemEntity; + +#[derive(Clone)] +pub struct LibraryItemSqliteRepository { + pub database_connection: Arc>, +} + +impl LibraryItemSqliteRepository { + pub fn new(database_connection: Arc>) -> Self { + Self { + database_connection, + } + } + + fn get_connection(&self) -> MutexGuard<'_, Connection> { + self.database_connection.lock().unwrap() + } + + pub fn create_table(&self) -> Result<(), rusqlite::Error> { + let connection = self.get_connection(); + connection.execute( + "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 REFERENCES library_item (id) + );", + [] + )?; + Ok(()) + } + + pub fn get_all_of_root_folder(&self) -> Result, Error> { + let database_connection = self.get_connection(); + + let mut prepared_statement = database_connection.prepare( + "SELECT + id, + name, + type + FROM library_item + WHERE parent_folder_id IS NULL", + )?; + let rows = prepared_statement.query([])?; + rows.map(Self::map_to_library_item).collect::>() + } + + fn map_to_library_item(row: &Row) -> Result { + let id_text: String = row.get(0)?; + let type_as_number: i8 = row.get(2)?; + Ok( + LibraryItemEntity { + id: Uuid::parse_str(&id_text).unwrap(), + name: row.get(1)?, + r#type: type_as_number, + parent_folder_id: None + } + ) + } +} \ No newline at end of file