ouverture_core/
library.rs

1use crate::config::Config;
2use crate::music::song::*;
3use async_walkdir::{DirEntry, WalkDir};
4use futures_lite::stream::StreamExt;
5use log::{debug, error, info, trace, warn};
6use std::path::Path;
7
8use crate::database::add_db;
9
10use sea_orm::entity::prelude::*;
11use sea_orm::{entity::*, query::*};
12use sea_orm::{Database, DatabaseConnection};
13
14use crate::database::*;
15
16pub async fn scan(config: &Config) {
17    for path_to_dir in &config.library {
18        let mut entries = WalkDir::new(path_to_dir);
19        loop {
20            match entries.next().await {
21                Some(Ok(entry)) => {
22                    trace!("Found file in library: {e}", e = entry.path().display());
23                    let song = Song::from_path(&entry.path());
24                    let res = add_db(config, song).await;
25                    trace!("added correctly ? {res:?}");
26                }
27                Some(Err(e)) => {
28                    warn!("error: {}", e);
29                    break;
30                }
31                None => break,
32            }
33        }
34    }
35}
36
37pub async fn list(config: &Config, query: Option<String>) -> Vec<Song> {
38    let database_url = "postgres://ouverture:ouverture@localhost:".to_string()
39        + &config.database_port.to_string()
40        + "/ouverture";
41    let db = Database::connect(&database_url).await.unwrap();
42
43    let song_found: Vec<setup::Model> = setup::Entity::find().all(&db).await.unwrap();
44    let song_found: Vec<Song> = song_found.into_iter().map(|m| Song::from(m)).collect();
45
46    println!("{song_found:?}");
47    return song_found;
48}