コード例 #1
0
        public HttpResponseMessage Index(int genreId = 0, string artistName = null)
        {
            using (var dbContext = new DbContext())
            {
                IQueryable<Artist> artists = dbContext.Artists
                    .OrderBy(artist => artist.Name);

                if (artistName != null)
                {
                    artists = artists.Where(x => x.Name.Contains(artistName));
                }

                if (genreId != 0)
                {
                    artists = artists.Where(x => x.Genres.Select(y => y.Id).Contains(genreId));
                }

                return Request.CreateResponse(artists.Select(artist => new Models.Artist
                {
                    Id = artist.Id,
                    Name = artist.Name,

                    Albums = artist.Albums.Count
                }).ToList());
            }
        }
コード例 #2
0
        public HttpResponseMessage Index(string genreName = null)
        {
            using (var dbContext = new DbContext())
            {
                IQueryable<Genre> genres = dbContext.Genres
                    .OrderBy(genre => genre.Name);

                if (genreName != null)
                {
                    genres = genres.Where(x => x.Name.Contains(genreName));
                }

                return Request.CreateResponse(genres.Select(genre => new Models.Genre
                {
                    Id = genre.Id,
                    Name = genre.Name,

                    Artists = genre.Artists.Count,
                    Albums = genre.Albums.Count
                }).ToList());
            }
        }
コード例 #3
0
        public void Index()
        {
            var folderNames = Directory.GetDirectories(@"C:\mp3", "*", SearchOption.AllDirectories).ToList();

            folderNames.ForEach(folderName =>
            {
                using (var dbContext = new DbContext())
                {
                    var fileNames = Directory.GetFiles(folderName).Where(x => x.EndsWith(".flac") || x.EndsWith(".mp3")).ToList();

                    if (fileNames.Count > 0)
                    {
                        var torrentCreator = new MonoTorrent.Common.TorrentCreator();
                        var torrentFileSource = new MonoTorrent.Common.TorrentFileSource(folderName);

                        torrentCreator.Announces.Add(new List<string>
                        {
                            "https://psy-trance.com/announce/"
                        });

                        var torrent = new List<Torrent>();

                        using (var memoryStream = new MemoryStream())
                        {
                            torrentCreator.Create(torrentFileSource, memoryStream);

                            torrent.Add(dbContext.Torrent.FirstOrDefault(x => x.Name == folderName) ??
                                        new Torrent
                                        {
                                            Name = folderName,

                                            Data = memoryStream.ToArray()
                                        });
                        }

                        fileNames.ForEach(fileName =>
                        {
                            using (var file = TagLib.File.Create(fileName))
                            {
                                file.Tag.AlbumArtists = file.Tag.JoinedAlbumArtists.Split(';').Select(x => x.Trim()).ToArray();
                                file.Tag.Artists = file.Tag.JoinedArtists.Split(';').Select(x => x.Trim()).ToArray();
                                file.Tag.Genres = file.Tag.JoinedGenres.Split(';').Select(x => x.Trim()).ToArray();

                                file.Save();

                                var albums = new List<Album>();

                                albums.Add(dbContext.Albums.FirstOrDefault(x => x.Folder == folderName) ??
                                           new Album
                                           {
                                               Name = file.Tag.Album,

                                               Artists = new List<Artist>(),
                                               Countries = new List<Country>(),
                                               Genres = new List<Genre>(),
                                               Labels = new List<Label>(),
                                               Songs = new List<Song>(),

                                               Folder = folderName,
                                               Folders = new List<Folder>(),

                                               Jpeg = new List<Jpeg>(),
                                               Torrent = new List<Torrent>()
                                           });

                                var artists = new List<Artist>();

                                file.Tag.Artists.Union(file.Tag.AlbumArtists).ToList().ForEach(artistName =>
                                {
                                    artists.Add(dbContext.Artists.FirstOrDefault(x => x.Name == artistName) ??
                                                new Artist
                                                {
                                                    Name = artistName,

                                                    Albums = new List<Album>(),
                                                    Countries = new List<Country>(),
                                                    Genres = new List<Genre>(),
                                                    Labels = new List<Label>(),
                                                    Songs = new List<Song>(),
                                                });
                                });

                                var countries = new List<Country>();

                                var genres = new List<Genre>();

                                file.Tag.Genres.ToList().ForEach(genreName =>
                                {
                                    genres.Add(dbContext.Genres.FirstOrDefault(x => x.Name == genreName) ??
                                               new Genre
                                               {
                                                   Name = genreName,

                                                   Albums = new List<Album>(),
                                                   Artists = new List<Artist>(),
                                                   Countries = new List<Country>(),
                                                   Labels = new List<Label>(),
                                                   Songs = new List<Song>(),
                                               });
                                });

                                var labels = new List<Label>();

                                var songs = new List<Song>();

                                songs.Add(dbContext.Songs.FirstOrDefault(x => x.File == fileName) ??
                                          new Song
                                          {
                                              Name = file.Tag.Title,

                                              Albums = new List<Album>(),
                                              Artists = new List<Artist>(),
                                              Countries = new List<Country>(),
                                              Genres = new List<Genre>(),
                                              Labels = new List<Label>(),

                                              File = fileName
                                          });

                                var jpeg = new List<Jpeg>();

                                file.Tag.Pictures.ToList().ForEach(picture =>
                                {
                                    using (var memoryStream = new MemoryStream(picture.Data.Data))
                                    {
                                        jpeg.Add(dbContext.Jpeg.FirstOrDefault(x => x.Name == folderName) ??
                                                 new Jpeg
                                                 {
                                                     Name = folderName,

                                                     Data = memoryStream.ToArray()
                                                 });
                                    }
                                });

                                albums.ForEach(album =>
                                {
                                    album.Artists = album.Artists.Union(artists.Intersect(file.Tag.AlbumArtists.Select(x => new Artist {Name = x}))).ToList();
                                    album.Countries = album.Countries.Union(countries).ToList();
                                    album.Genres = album.Genres.Union(genres).ToList();
                                    album.Labels = album.Labels.Union(labels).ToList();
                                    album.Songs = album.Songs.Union(songs).ToList();

                                    album.Jpeg = album.Jpeg.Union(jpeg).ToList();
                                    album.Torrent = album.Torrent.Union(torrent).ToList();

                                    dbContext.Albums.AddOrUpdate(album);
                                });

                                artists.ForEach(artist =>
                                {
                                    artist.Albums = artist.Albums.Union(albums).ToList();
                                    artist.Countries = artist.Countries.Union(countries).ToList();
                                    artist.Genres = artist.Genres.Union(genres).ToList();
                                    artist.Labels = artist.Labels.Union(labels).ToList();
                                    artist.Songs = artist.Songs.Union(songs).ToList();

                                    dbContext.Artists.AddOrUpdate(artist);
                                });

                                countries.ForEach(country =>
                                {
                                    country.Albums = country.Albums.Union(albums).ToList();
                                    country.Artists = country.Artists.Union(artists).ToList();
                                    country.Genres = country.Genres.Union(genres).ToList();
                                    country.Labels = country.Labels.Union(labels).ToList();
                                    country.Songs = country.Songs.Union(songs).ToList();

                                    dbContext.Countries.AddOrUpdate(country);
                                });

                                genres.ForEach(genre =>
                                {
                                    genre.Albums = genre.Albums.Union(albums).ToList();
                                    genre.Artists = genre.Artists.Union(artists).ToList();
                                    genre.Countries = genre.Countries.Union(countries).ToList();
                                    genre.Labels = genre.Labels.Union(labels).ToList();
                                    genre.Songs = genre.Songs.Union(songs).ToList();

                                    dbContext.Genres.AddOrUpdate(genre);
                                });

                                labels.ForEach(label =>
                                {
                                    label.Albums = label.Albums.Union(albums).ToList();
                                    label.Artists = label.Artists.Union(artists).ToList();
                                    label.Countries = label.Countries.Union(countries).ToList();
                                    label.Genres = label.Genres.Union(genres).ToList();
                                    label.Songs = label.Songs.Union(songs).ToList();

                                    dbContext.Labels.AddOrUpdate(label);
                                });

                                songs.ForEach(song =>
                                {
                                    song.Albums = song.Albums.Union(albums).ToList();
                                    song.Artists = song.Artists.Union(artists.Intersect(file.Tag.Artists.Select(x => new Artist { Name = x }))).ToList();
                                    song.Countries = song.Countries.Union(countries).ToList();
                                    song.Genres = song.Genres.Union(genres).ToList();
                                    song.Labels = song.Labels.Union(labels).ToList();

                                    dbContext.Songs.AddOrUpdate(song);
                                });

                                dbContext.SaveChanges();
                            }
                        });
                    }
                }
            });
        }