Create library item repository.
This commit is contained in:
@@ -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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user