Beispiel #1
0
        /// <summary>
        /// Process returns an AlbumArtistsResponse containing a list of artists, albums, and songs
        /// </summary>
        public void Process(UriWrapper uri, IHttpProcessor processor, User user)
        {
            // Lists of artists, albums, songs to be returned via handler
            IList <AlbumArtist>      albumArtists     = new List <AlbumArtist>();
            IList <Album>            albums           = new List <Album>();
            IList <Song>             songs            = new List <Song>();
            Dictionary <string, int> counts           = new Dictionary <string, int>();
            PairList <string, int>   sectionPositions = new PairList <string, int>();

            // Optional Last.fm info
            string lastfmInfo = null;

            // Check if an ID was passed
            if (uri.Id != null)
            {
                // Add artist by ID to the list
                AlbumArtist a = Injection.Kernel.Get <IAlbumArtistRepository>().AlbumArtistForId((int)uri.Id);
                albumArtists.Add(a);

                // Add artist's albums to response
                albums = a.ListOfAlbums();
                counts.Add("albums", albums.Count);

                // If requested, add artist's songs to response
                if (uri.Parameters.ContainsKey("includeSongs") && uri.Parameters["includeSongs"].IsTrue())
                {
                    songs = a.ListOfSongs();
                    counts.Add("songs", songs.Count);
                }
                else
                {
                    counts.Add("songs", a.ListOfSongs().Count);
                }

                // If requested, add artist's Last.fm info to response
                if (uri.Parameters.ContainsKey("lastfmInfo") && uri.Parameters["lastfmInfo"].IsTrue())
                {
                    logger.IfInfo("Querying Last.fm for artist: " + a.AlbumArtistName);
                    try
                    {
                        lastfmInfo = Lastfm.GetAlbumArtistInfo(a);
                        logger.IfInfo("Last.fm query complete!");
                    }
                    catch (Exception e)
                    {
                        logger.Error("Last.fm query failed!");
                        logger.Error(e);
                    }
                }

                // Get favorites count if available
                if (a.AlbumArtistId != null)
                {
                    int favoriteCount = Injection.Kernel.Get <IFavoriteRepository>().FavoritesForAlbumArtistId(a.AlbumArtistId, user.UserId).Count;
                    counts.Add("favorites", favoriteCount);
                }
                else
                {
                    counts.Add("favorites", 0);
                }
            }
            // Check for a request for range of artists
            else if (uri.Parameters.ContainsKey("range"))
            {
                string[] range = uri.Parameters["range"].Split(',');

                // Ensure valid range was parsed
                if (range.Length != 2)
                {
                    processor.WriteJson(new AlbumArtistsResponse("Parameter 'range' requires a valid, comma-separated character tuple", null, null, null, null, null, null));
                    return;
                }

                // Validate as characters
                char start, end;
                if (!Char.TryParse(range[0], out start) || !Char.TryParse(range[1], out end))
                {
                    processor.WriteJson(new AlbumArtistsResponse("Parameter 'range' requires characters which are single alphanumeric values", null, null, null, null, null, null));
                    return;
                }

                // Grab range of artists
                albumArtists = Injection.Kernel.Get <IAlbumArtistRepository>().RangeAlbumArtists(start, end);
            }

            // Check for a request to limit/paginate artists, like SQL
            // Note: can be combined with range or all artists
            if (uri.Parameters.ContainsKey("limit") && uri.Id == null)
            {
                string[] limit = uri.Parameters["limit"].Split(',');

                // Ensure valid limit was parsed
                if (limit.Length < 1 || limit.Length > 2)
                {
                    processor.WriteJson(new AlbumArtistsResponse("Parameter 'limit' requires a single integer, or a valid, comma-separated integer tuple", null, null, null, null, null, null));
                    return;
                }

                // Validate as integers
                int index    = 0;
                int duration = Int32.MinValue;
                if (!Int32.TryParse(limit[0], out index))
                {
                    processor.WriteJson(new AlbumArtistsResponse("Parameter 'limit' requires a valid integer start index", null, null, null, null, null, null));
                    return;
                }

                // Ensure positive index
                if (index < 0)
                {
                    processor.WriteJson(new AlbumArtistsResponse("Parameter 'limit' requires a non-negative integer start index", null, null, null, null, null, null));
                    return;
                }

                // Check for duration
                if (limit.Length == 2)
                {
                    if (!Int32.TryParse(limit[1], out duration))
                    {
                        processor.WriteJson(new AlbumArtistsResponse("Parameter 'limit' requires a valid integer duration", null, null, null, null, null, null));
                        return;
                    }

                    // Ensure positive duration
                    if (duration < 0)
                    {
                        processor.WriteJson(new AlbumArtistsResponse("Parameter 'limit' requires a non-negative integer duration", null, null, null, null, null, null));
                        return;
                    }
                }

                // Check if results list already populated by range
                if (albumArtists.Count > 0)
                {
                    // No duration?  Return just specified number of artists
                    if (duration == Int32.MinValue)
                    {
                        albumArtists = albumArtists.Skip(0).Take(index).ToList();
                    }
                    else
                    {
                        // Else, return artists starting at index, up to count duration
                        albumArtists = albumArtists.Skip(index).Take(duration).ToList();
                    }
                }
                else
                {
                    // If no artists in list, grab directly using model method
                    albumArtists = Injection.Kernel.Get <IAlbumArtistRepository>().LimitAlbumArtists(index, duration);
                }
            }

            // Finally, if no artists already in list, send the whole list
            if (albumArtists.Count == 0)
            {
                albumArtists     = Injection.Kernel.Get <IAlbumArtistRepository>().AllAlbumArtists();
                sectionPositions = Utility.SectionPositionsFromSortedList(new List <IGroupingItem>(albumArtists.Select(c => (IGroupingItem)c)));
            }

            // Send it!
            processor.WriteJson(new AlbumArtistsResponse(null, albumArtists, albums, songs, counts, lastfmInfo, sectionPositions));
            return;
        }