diff --git a/backend/infrastructure/src/video/repository.rs b/backend/infrastructure/src/video/repository.rs index 7a437a3..a6dc2be 100644 --- a/backend/infrastructure/src/video/repository.rs +++ b/backend/infrastructure/src/video/repository.rs @@ -153,9 +153,37 @@ impl LibraryItemSqliteRepository { let raw_data = rows.map(Self::map_to_raw_entity).collect::>()?; - - - Ok(None) + // Build a map of id to LibraryItemEntity for quick lookup + let mut entity_map: std::collections::HashMap = std::collections::HashMap::new(); + + // First pass: create all entities without parent relationships + for raw_entity in &raw_data { + let entity = LibraryItemEntity { + id: raw_entity.id.clone(), + name: raw_entity.name.clone(), + r#type: raw_entity.r#type, + parent_folder_id: None, // Will be set in second pass + }; + entity_map.insert(raw_entity.id.clone(), entity); + } + + // Second pass: set up parent-child relationships + for raw_entity in &raw_data { + if let Some(parent_id) = &raw_entity.parent_item_id { + if let Some(child_entity) = entity_map.get_mut(&raw_entity.id) { + if let Some(parent_entity) = entity_map.get(parent_id) { + child_entity.parent_folder_id = Some(Box::new(parent_entity.clone())); + } + } + } + } + + // Find the requested item and return it with its full hierarchy + if let Some(entity) = entity_map.get(library_item_id) { + Ok(Some(entity.clone())) + } else { + Ok(None) + } } fn map_to_raw_entity(row: &Row) -> Result {