@@ -6,6 +6,7 @@ use music_player_entity::{album as album_entity, artist as artist_entity, track
6
6
use music_player_scanner:: scan_directory;
7
7
use music_player_storage:: {
8
8
repo:: { album:: AlbumRepository , artist:: ArtistRepository , track:: TrackRepository } ,
9
+ searcher:: Searcher ,
9
10
Database ,
10
11
} ;
11
12
use music_player_types:: types:: { self , RemoteCoverUrl , RemoteTrackUrl } ;
@@ -58,7 +59,6 @@ impl LibraryQuery {
58
59
let results = TrackRepository :: new ( db. lock ( ) . await . get_connection ( ) )
59
60
. find_all ( 100 )
60
61
. await ?;
61
- println ! ( "album results:" ) ;
62
62
63
63
Ok ( results. into_iter ( ) . map ( Into :: into) . collect ( ) )
64
64
}
@@ -247,8 +247,9 @@ impl LibraryQuery {
247
247
248
248
async fn search ( & self , ctx : & Context < ' _ > , keyword : String ) -> Result < SearchResult , Error > {
249
249
let db = ctx. data :: < Arc < Mutex < Database > > > ( ) . unwrap ( ) ;
250
- let db = db. lock ( ) . await ;
251
- let indexer = db. get_searcher ( ) ;
250
+ let searcher = ctx. data :: < Arc < Mutex < Searcher > > > ( ) . unwrap ( ) ;
251
+ let mut db = db. lock ( ) . await ;
252
+ let indexer = searcher. lock ( ) . await ;
252
253
let artists = indexer. artist . search ( & keyword) ?;
253
254
let albums = indexer. album . search ( & keyword) ?;
254
255
let tracks = indexer. track . search ( & keyword) ?;
@@ -266,68 +267,72 @@ pub struct LibraryMutation;
266
267
#[ Object ]
267
268
impl LibraryMutation {
268
269
async fn scan ( & self , _ctx : & Context < ' _ > ) -> Result < bool , Error > {
269
- scan_directory ( move |song, db| {
270
- async move {
271
- let id = format ! ( "{:x}" , md5:: compute( song. artist. to_string( ) ) ) ;
272
- let item = artist_entity:: ActiveModel {
273
- id : ActiveValue :: set ( id) ,
274
- name : ActiveValue :: Set ( song. artist . clone ( ) ) ,
275
- } ;
276
- match item. insert ( db. get_connection ( ) ) . await {
277
- Ok ( _) => ( ) ,
278
- Err ( _) => ( ) ,
279
- }
280
-
281
- let id = format ! (
282
- "{:x}" ,
283
- md5:: compute( format!( "{}{}" , song. album, song. artist) )
284
- ) ;
285
- let item = album_entity:: ActiveModel {
286
- id : ActiveValue :: set ( id) ,
287
- artist : ActiveValue :: set ( song. artist . clone ( ) ) ,
288
- title : ActiveValue :: Set ( song. album . clone ( ) ) ,
289
- artist_id : ActiveValue :: Set ( Some ( format ! (
290
- "{:x}" ,
291
- md5:: compute( song. artist. to_string( ) )
292
- ) ) ) ,
293
- year : ActiveValue :: Set ( song. year ) ,
294
- cover : ActiveValue :: Set ( song. cover . clone ( ) ) ,
295
- } ;
296
- match item. insert ( db. get_connection ( ) ) . await {
297
- Ok ( _) => ( ) ,
298
- Err ( _) => ( ) ,
299
- }
300
- let id = format ! ( "{:x}" , md5:: compute( song. uri. as_ref( ) . unwrap( ) ) ) ;
301
- let item = track_entity:: ActiveModel {
302
- id : ActiveValue :: set ( id) ,
303
- title : ActiveValue :: Set ( song. title . clone ( ) ) ,
304
- artist : ActiveValue :: set ( song. artist . clone ( ) ) ,
305
- genre : ActiveValue :: Set ( song. genre . clone ( ) ) ,
306
- year : ActiveValue :: Set ( song. year ) ,
307
- track : ActiveValue :: Set ( song. track ) ,
308
- bitrate : ActiveValue :: Set ( song. bitrate ) ,
309
- sample_rate : ActiveValue :: Set ( song. sample_rate ) ,
310
- bit_depth : ActiveValue :: Set ( song. bit_depth ) ,
311
- channels : ActiveValue :: Set ( song. channels ) ,
312
- duration : ActiveValue :: Set ( Some ( song. duration . as_secs_f32 ( ) ) ) ,
313
- uri : ActiveValue :: Set ( song. uri . clone ( ) . unwrap_or_default ( ) ) ,
314
- album_id : ActiveValue :: Set ( Some ( format ! (
270
+ scan_directory (
271
+ move |song, db| {
272
+ async move {
273
+ let id = format ! ( "{:x}" , md5:: compute( song. artist. to_string( ) ) ) ;
274
+ let item = artist_entity:: ActiveModel {
275
+ id : ActiveValue :: set ( id) ,
276
+ name : ActiveValue :: Set ( song. artist . clone ( ) ) ,
277
+ } ;
278
+ match item. insert ( db. get_connection ( ) ) . await {
279
+ Ok ( _) => ( ) ,
280
+ Err ( _) => ( ) ,
281
+ }
282
+
283
+ let id = format ! (
315
284
"{:x}" ,
316
285
md5:: compute( format!( "{}{}" , song. album, song. artist) )
317
- ) ) ) ,
318
- artist_id : ActiveValue :: Set ( Some ( format ! (
319
- "{:x}" ,
320
- md5:: compute( song. artist. to_string( ) )
321
- ) ) ) ,
322
- } ;
323
-
324
- match item. insert ( db. get_connection ( ) ) . await {
325
- Ok ( _) => ( ) ,
326
- Err ( _) => ( ) ,
286
+ ) ;
287
+ let item = album_entity:: ActiveModel {
288
+ id : ActiveValue :: set ( id) ,
289
+ artist : ActiveValue :: set ( song. artist . clone ( ) ) ,
290
+ title : ActiveValue :: Set ( song. album . clone ( ) ) ,
291
+ artist_id : ActiveValue :: Set ( Some ( format ! (
292
+ "{:x}" ,
293
+ md5:: compute( song. artist. to_string( ) )
294
+ ) ) ) ,
295
+ year : ActiveValue :: Set ( song. year ) ,
296
+ cover : ActiveValue :: Set ( song. cover . clone ( ) ) ,
297
+ } ;
298
+ match item. insert ( db. get_connection ( ) ) . await {
299
+ Ok ( _) => ( ) ,
300
+ Err ( _) => ( ) ,
301
+ }
302
+ let id = format ! ( "{:x}" , md5:: compute( song. uri. as_ref( ) . unwrap( ) ) ) ;
303
+ let item = track_entity:: ActiveModel {
304
+ id : ActiveValue :: set ( id) ,
305
+ title : ActiveValue :: Set ( song. title . clone ( ) ) ,
306
+ artist : ActiveValue :: set ( song. artist . clone ( ) ) ,
307
+ genre : ActiveValue :: Set ( song. genre . clone ( ) ) ,
308
+ year : ActiveValue :: Set ( song. year ) ,
309
+ track : ActiveValue :: Set ( song. track ) ,
310
+ bitrate : ActiveValue :: Set ( song. bitrate ) ,
311
+ sample_rate : ActiveValue :: Set ( song. sample_rate ) ,
312
+ bit_depth : ActiveValue :: Set ( song. bit_depth ) ,
313
+ channels : ActiveValue :: Set ( song. channels ) ,
314
+ duration : ActiveValue :: Set ( Some ( song. duration . as_secs_f32 ( ) ) ) ,
315
+ uri : ActiveValue :: Set ( song. uri . clone ( ) . unwrap_or_default ( ) ) ,
316
+ album_id : ActiveValue :: Set ( Some ( format ! (
317
+ "{:x}" ,
318
+ md5:: compute( format!( "{}{}" , song. album, song. artist) )
319
+ ) ) ) ,
320
+ artist_id : ActiveValue :: Set ( Some ( format ! (
321
+ "{:x}" ,
322
+ md5:: compute( song. artist. to_string( ) )
323
+ ) ) ) ,
324
+ } ;
325
+
326
+ match item. insert ( db. get_connection ( ) ) . await {
327
+ Ok ( _) => ( ) ,
328
+ Err ( _) => ( ) ,
329
+ }
327
330
}
328
- }
329
- . boxed ( )
330
- } )
331
+ . boxed ( )
332
+ } ,
333
+ & Database :: new ( ) . await ,
334
+ & Searcher :: new ( ) ,
335
+ )
331
336
. await ?;
332
337
333
338
Ok ( false )
0 commit comments