private bool TryValidateSongGuids(IEnumerable<string> guidStrings, out IEnumerable<Song> foundSongs, out ResponseInfo responseInfo)
        {
            var guids = new List<Guid>();

            foreach (string guidString in guidStrings)
            {
                Guid guid;

                bool valid = Guid.TryParse(guidString, out guid);

                if (valid)
                {
                    guids.Add(guid);
                }

                else
                {
                    responseInfo = CreateResponse(ResponseStatus.MalformedRequest, "One or more GUIDs are malformed");
                    foundSongs = null;
                    return false;
                }
            }

            // Look if any song in our local library or any song of the last SoundCloud or YouTube
            // requests has the requested Guid
            Dictionary<Guid, Song> dic = this.library.Songs
                .Concat(this.lastSoundCloudRequest.Cast<Song>())
                .Concat(this.lastYoutubeRequest)
                .ToDictionary(x => x.Guid);

            List<Song> songs = guids.Select(x =>
            {
                Song song;

                dic.TryGetValue(x, out song);

                return song;
            })
            .Where(x => x != null)
            .ToList();

            if (guids.Count != songs.Count)
            {
                responseInfo = CreateResponse(ResponseStatus.NotFound, "One or more songs could not be found");
                foundSongs = null;
                return false;
            }

            responseInfo = null;
            foundSongs = songs;
            return true;
        }
Exemple #2
0
        private bool TryValidateSongGuids(IEnumerable<string> guidStrings, out IEnumerable<Song> foundSongs, out ResponseInfo responseInfo)
        {
            var guids = new List<Guid>();

            foreach (string guidString in guidStrings)
            {
                Guid guid;

                bool valid = Guid.TryParse(guidString, out guid);

                if (valid)
                {
                    guids.Add(guid);
                }

                else
                {
                    responseInfo = CreateResponse(ResponseStatus.MalformedRequest, "One or more GUIDs are malformed");
                    foundSongs = null;
                    return false;
                }
            }

            Dictionary<Guid, LocalSong> dic = this.library.Songs.ToDictionary(x => x.Guid);

            List<LocalSong> songs = guids.Select(x =>
            {
                LocalSong song;

                dic.TryGetValue(x, out song);

                return song;
            })
            .Where(x => x != null)
            .ToList();

            if (guids.Count != songs.Count)
            {
                responseInfo = CreateResponse(ResponseStatus.NotFound, "One or more songs could not be found");
                foundSongs = null;
                return false;
            }

            responseInfo = null;
            foundSongs = songs;
            return true;
        }