Create library item repository.

This commit is contained in:
Florian THIERRY
2026-07-17 10:39:11 +02:00
parent 36eec1e694
commit 56d5abe964
4 changed files with 107 additions and 4 deletions
+1 -3
View File
@@ -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 {
+2 -1
View File
@@ -1,3 +1,4 @@
pub mod adapter;
pub mod model;
pub mod video_format_detector;
pub mod video_format_detector;
mod repository;
+39
View File
@@ -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<Uuid>
}
impl LibraryItemEntity {
pub fn from_library_item(library_item: &LibraryItem) -> Result<Self, Error> {
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<i8, Error> {
match item_type {
LibraryItemType::VIDEO => Ok(0),
LibraryItemType::FOLDER => Ok(1)
}
}
fn map_type_from_i8(item_type_number: i8) -> Result<LibraryItemType, Error> {
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}")))
}
}
}
@@ -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<Mutex<Connection>>,
}
impl LibraryItemSqliteRepository {
pub fn new(database_connection: Arc<Mutex<Connection>>) -> 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<Vec<LibraryItem>, 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::<Vec<LibraryItem>>()
}
fn map_to_library_item(row: &Row) -> Result<LibraryItemEntity, Error> {
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
}
)
}
}