zoodex/src/persistence.rs

52 lines
1.4 KiB
Rust
Raw Normal View History

2024-11-20 16:32:37 +01:00
use { fallible_iterator :: * , rusqlite :: * } ;
use crate :: collection :: * ;
pub fn read_collection_file () -> Result <Collection> {
let sqlite_connection = Connection :: open ("zoodex.sqlite") ? ;
let mut films_query = sqlite_connection
. prepare ("select * from films order by release_date desc") ? ;
let films = films_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let release_date = row . get (3) ? ;
let runtime_minutes = row . get (4) ? ;
let poster_file_path : Option <String> = row . get (5) ? ;
let video_file_path : Option <String> = row . get (6) ? ;
Ok ( Film {
uuid ,
name ,
original_name ,
release_date ,
runtime_minutes ,
poster_file_path : poster_file_path . map ( String :: into ) ,
video_file_path : video_file_path . map ( String :: into ) ,
} )
} ) ;
let mut series_query = sqlite_connection . prepare ("select * from series") ? ;
let series = series_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let poster_file_path : Option <String> = row . get (3) ? ;
Ok ( Series {
uuid ,
name ,
original_name ,
poster_file_path : poster_file_path . map ( String :: into ) ,
} )
} ) ;
Ok ( Collection {
films : films . collect () ? ,
series : series . collect () ? ,
} )
}