コード例 #1
0
 public static IEnumerable <AlbumViewModel> GetAlbumViewModelList()
 {
     using (var ctx = new ChinookContext())
     {
         List <TrackViewModel> tracks = new List <TrackViewModel>();
         //load tracks in memory
         foreach (var track in ctx.Track)
         {
             tracks.Add(TrackViewModel.Create(track.TrackId,
                                              track.Name,
                                              track.AlbumId,
                                              track.MediaTypeId,
                                              track.GenreId,
                                              track.Composer,
                                              track.Milliseconds,
                                              track.Bytes));
         }
         foreach (var album in ctx.Album)
         {
             yield return(AlbumViewModel.Create(album.AlbumId, album.Title, tracks.FindAll(x => x.AlbumId == album.AlbumId)));
         }
     }
 }
コード例 #2
0
        private async Task IndexFolders(IEnumerable <GetFolderFilesResult> folders)
        {
            async Task <IndexTrackTransactionResult> IndexTrackTransactionAsync(StorageFile f, Folder folder)
            {
                var tf  = TagLib.File.Create(await UwpFileAbstraction.CreateAsync(f));
                var tag = tf.Tag;

                using (var tr = await db.Database.BeginTransactionAsync())
                {
                    try
                    {
                        var track = await CreateTrackAsync(f, tf);

                        await db.Tracks.AddAsync(track);

                        await db.SaveChangesAsync();

                        // create artist entity
                        var artistName = string.IsNullOrEmpty(tag.FirstPerformer) ? "Unknown" : tag.FirstPerformer;
                        var artist     = await db.Artists
                                         .Include(x => x.Albums)
                                         .FirstOrDefaultAsync(a => a.Name == artistName);

                        var createArtist = artist == default;

                        if (createArtist)
                        {
                            artist = new Artist()
                            {
                                Name = artistName,
                            };

                            await db.Artists.AddAsync(artist);
                        }

                        await db.SaveChangesAsync();

                        // create album entity
                        var albumTitle = string.IsNullOrEmpty(tag.Album) ? "Unknown" : tag.Album;
                        var album      = artist.Albums.FirstOrDefault(x => x.Title == albumTitle);

                        var createAlbum = album == default;

                        if (createAlbum)
                        {
                            album = new Album()
                            {
                                Title = albumTitle,
                            };

                            await db.Albums.AddAsync(album);
                        }

                        if (string.IsNullOrEmpty(album.CoverCacheToken))
                        {
                            var picData = await FindAlbumCoverAsync(tag, f.Path);

                            if (picData != default(IBuffer))
                            {
                                var cover = await CacheService.CacheAsync(picData);

                                album.CoverCacheToken = cover;
                            }
                        }

                        await db.SaveChangesAsync();

                        track.Album  = album;
                        track.Folder = folder;
                        album.Tracks.Add(track);
                        if (createAlbum)
                        {
                            album.Artist = artist;
                            artist.Albums.Add(album);
                        }

                        await db.SaveChangesAsync();

                        await tr.CommitAsync();

                        return(new IndexTrackTransactionResult()
                        {
                            Successful = true,
                            Track = track,
                            ArtistCreated = createArtist,
                            Artist = artist,
                            AlbumCreated = createAlbum,
                            Album = album
                        });
                    }
                    catch (Exception)
                    {
                        await tr.RollbackAsync();

                        return(new IndexTrackTransactionResult()
                        {
                            Successful = false
                        });
                    }
                }
            }

            var group = await folders.GroupJoinAsync(TrackSource.Items, x => x.ViewModel.Id, x => x.FolderId, (mix, dbFiles) => (mix, dbFiles.ToList()));

            await group.ForEachAsync(async g =>
            {
                var dbFiles   = g.Item2;
                var diskFiles = g.mix.Files;
                var dbFolder  = await db.Folders.SingleAsync(x => x.Id == g.mix.ViewModel.Id);

                await diskFiles.ForEachAsync(async f =>
                {
                    var trackRx = await dbFiles.FirstOrDefaultAsync(x => x.FileName == f.Name && x.Path == f.Path);

                    if (trackRx == null)
                    {
                        var result = await IndexTrackTransactionAsync(f, dbFolder);
                        if (result.Successful == false)
                        {
                            return;
                        }

                        if (result.ArtistCreated)
                        {
                            var artistVm = ArtistViewModel.Create(result.Artist);
                            _artistSource.AddOrUpdate(artistVm);
                        }
                        else
                        {
                            var artistVm = _artistSource.Lookup(result.Artist.Id).Value;
                            if (result.AlbumCreated)
                            {
                                var albumVm = AlbumViewModel.Create(artistVm, result.Album);
                                artistVm.Albums.AddOrUpdate(albumVm);
                            }
                            else
                            {
                                var albumVm = artistVm.Albums.Items.Single(x => x.Id == result.Album.Id);
                                var trackVm = TrackViewModel.Create(albumVm, result.Track);
                                albumVm.Tracks.AddOrUpdate(trackVm);
                            }
                        }
                    }
                    else
                    {
                        dbFiles.Remove(dbFiles.First(x => x.Id == trackRx.Id));
                    }
                });

                await dbFiles.ForEachAsync(async f =>
                {
                    var track = await db.Tracks.SingleAsync(x => x.Id == f.Id);
                    await RemoveTrackAsync(track);
                });
            });
        }