Esempio n. 1
0
File: MediaDB.cs Progetto: XCVG/XSMP
        /// <summary>
        /// Gets a unique cname for a (new) playlist
        /// </summary>
        public string GetPlaylistUniqueName(string name)
        {
            ThrowIfNotReady();

            var cname = MediaUtils.GetCanonicalName(name);

            //easy case: cname does not exist in playlist list
            if (!Playlists.Keys.Contains(cname))
            {
                return(cname);
            }
            else
            {
                //contains at least one partial match
                int highestNumber = 0;
                foreach (var key in Playlists.Keys)
                {
                    if (key.StartsWith(cname))
                    {
                        var ks = key.Split('_');
                        if (ks.Length > 1)
                        {
                            int keyNumber = int.Parse(ks[1]);
                            if (keyNumber > highestNumber)
                            {
                                highestNumber = keyNumber;
                            }
                        }
                    }
                }

                var newNumber = highestNumber + 1;

                return($"{cname}_{newNumber}");
            }
        }
Esempio n. 2
0
        private static void AddSong(SongInfo song, mediadbContext dbContext)
        {
            //check to see if artists exit and add them if they do not
            //var artistsCNames = song.Artists.Select(a => new KeyValuePair<string, string>(MediaUtils.GetCanonicalName(a), a)).Distinct().ToDictionary(kvp => kvp.Key,;
            var artistsCNames = new Dictionary <string, string>();

            foreach (var artist in song.Artists)
            {
                string artistCName = MediaUtils.GetCanonicalName(artist);
                if (artistsCNames.ContainsKey(artistCName))
                {
                    continue;
                }
                artistsCNames.Add(artistCName, artist);
            }

            foreach (var kvp in artistsCNames)
            {
                string artistCName = kvp.Key;
                string artistName  = kvp.Value;

                if (string.IsNullOrEmpty(artistCName))
                {
                    continue;
                }

                if (dbContext.Artist.Where(a => a.Name == artistCName).Count() == 0)
                {
                    var artist = new Artist()
                    {
                        Name = artistCName, NiceName = artistName
                    };
                    dbContext.Artist.Add(artist);
                }
            }

            //check if album artist exists and add it if it does not
            string albumArtistCName = MediaUtils.GetCanonicalName(song.AlbumArtistName);

            if (!string.IsNullOrEmpty(albumArtistCName) && !artistsCNames.ContainsKey(albumArtistCName))
            {
                if (dbContext.Artist.Where(a => a.Name == albumArtistCName).Count() == 0)
                {
                    var artist = new Artist()
                    {
                        Name = albumArtistCName, NiceName = song.AlbumArtistName
                    };
                    dbContext.Artist.Add(artist);
                }
            }

            //check if album exists and add it if it does not
            string albumCName = MediaUtils.GetCanonicalName(song.AlbumName);

            if (!string.IsNullOrEmpty(albumCName) && !string.IsNullOrEmpty(albumArtistCName))
            {
                if (dbContext.Album.Where(a => a.Name == albumCName && a.ArtistName == albumArtistCName).Count() == 0)
                {
                    var album = new Album()
                    {
                        Name = albumCName, ArtistName = albumArtistCName, Title = song.AlbumName
                    };
                    dbContext.Album.Add(album);
                }
            }

            //genre should be a cname
            string genreCName = MediaUtils.GetCanonicalName(song.Genre);

            //create song object and add!
            var songObject = new Song()
            {
                Hash            = song.Hash,
                Title           = song.Title,
                Genre           = genreCName,
                Length          = song.Length,
                Set             = song.Set,
                Track           = song.Track,
                Path            = song.Path,
                AlbumName       = albumCName,
                AlbumArtistName = albumArtistCName
            };

            dbContext.Song.Add(songObject);

            //create artist-song link objects
            foreach (var artistCName in artistsCNames.Keys)
            {
                var artistsong = new ArtistSong()
                {
                    ArtistName = artistCName, SongHash = song.Hash
                };
                dbContext.ArtistSong.Add(artistsong);
            }
        }