// GET - Admin UI for songs
        public ActionResult SongIndex(SongIndexOptions options, PagerParameters pagerParameters)
        {
            var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);

            // Default options
            if (options == null)
            {
                options = new SongIndexOptions();
            }

            // Filtering
            IContentQuery <SongPart, SongPartRecord> songsQuery;

            switch (options.Filter)
            {
            case SongIndexFilter.All:
                songsQuery = _songService.GetSongs();
                break;

            case SongIndexFilter.Open:
                songsQuery = _songService.GetSongs(true);
                break;

            case SongIndexFilter.Closed:
                songsQuery = _songService.GetSongs(false);
                break;

            default:
                throw new ArgumentOutOfRangeException("options");
            }

            var pagerShape = Shape.Pager(pager).TotalItemCount(songsQuery.Count());
            var entries    = songsQuery
                             .OrderByDescending <SongPartRecord>(song => song.Id)
                             .Slice(pager.GetStartIndex(), pager.PageSize)
                             .ToList()
                             .Select(song => CreateSongEntry(song.Record));

            var model = new SongIndexViewModel
            {
                Songs   = entries.ToList(),
                Options = options,
                Pager   = pagerShape
            };

            return(View((object)model));
        }
        // GET: Songs
        public ActionResult Index(string SongString)
        {
            var query = from a in db.Songs
                        select a;

            // Create a list of selected albums
            List <Song> SelectedSongs = new List <Song>();

            //Create a view bag to store the number of selected albums
            ViewBag.TotalSongCount = db.Songs.Count();

            //Create selected count of customers
            ViewBag.SelectedSongCount = db.Songs.Count();

            if (SongString == null || SongString == "") // they didn't select anything
            {
                SelectedSongs = query.ToList();
            }
            else //they picked something
            {
                //use linq to display searched names
                SelectedSongs = query.Where(a => a.SongName.Contains(SongString) || a.SongArtist.Any(r => r.ArtistName.Contains(SongString)) || a.SongAlbums.Any(x => x.AlbumName.Contains(SongString))).ToList();

                //Create selected count of customers
                ViewBag.SelectedSongCount = SelectedSongs.Count();

                //order the record to display sorted by lastname, first name, average sales
                SelectedSongs.OrderBy(a => a.SongName).ThenBy(a => a.SongPrice);
            }

            List <SongIndexViewModel> SongsDisplay = new List <SongIndexViewModel>();

            foreach (Song a in SelectedSongs)
            {
                SongIndexViewModel AVM = new SongIndexViewModel();

                AVM.Song = a;

                AVM.SongRating = getAverageRating(a.SongID).ToString("0.0");

                SongsDisplay.Add(AVM);
            }

            return(View(SongsDisplay));
        }
        public ActionResult SongIndex(FormCollection form)
        {
            var viewModel = new SongIndexViewModel {
                Songs = new List <SongEntry>(), Options = new SongIndexOptions()
            };

            UpdateModel(viewModel);

            IEnumerable <SongEntry> checkedEntries = viewModel.Songs.Where(s => s.IsChecked);

            switch (viewModel.Options.BulkAction)
            {
            case SongIndexBulkAction.None:
                break;

            case SongIndexBulkAction.Open:
                foreach (SongEntry entry in checkedEntries)
                {
                    throw new NotImplementedException();
                    // _songService.OpenSong(entry.Song.Id);
                }
                break;

            case SongIndexBulkAction.Close:
                foreach (SongEntry entry in checkedEntries)
                {
                    throw new NotImplementedException();
                    // _songService.CloseSong(entry.Song.Id);
                }
                break;

            case SongIndexBulkAction.Delete:
                foreach (SongEntry entry in checkedEntries)
                {
                    _songService.DeleteSong(entry.Song.Id);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("form");
            }
            return(RedirectToAction("SongIndex"));
        }
        // GET: Songs
        public async Task <IActionResult> Index()
        {
            var user = await GetCurrentUserAsync();

            if (user == null)
            {
                return(NotFound());
            }

            var userid   = user.Id;
            var songList = await _context.Song
                           .Include(s => s.Channels)
                           .Where(s => s.ApplicationUserId == userid).ToListAsync();

            SongIndexViewModel newModel = new SongIndexViewModel();

            newModel.ApplicationUser = user;
            newModel.Songs           = songList;
            return(View(newModel));
        }
        //POST: Advanced search
        public ActionResult SongSearchResults(string SongSearchString, string RatingString, SortOrder SelectedBounds, int[] SelectedGenre)
        {
            var query = from a in db.Songs
                        select a;



            if (SongSearchString == null || SongSearchString == "") //they didn't select anything
            {
                ViewBag.SongSearchString = "Search String was null";
            }
            else //they picked something up
            {
                ViewBag.SongSearchString = "The search string is" + SongSearchString;
                query = query.Where((a => a.SongName.Contains(SongSearchString) || a.SongArtist.Any(r => r.ArtistName.Contains(SongSearchString)) || a.SongAlbums.Any(x => x.AlbumName.Contains(SongSearchString))));
            }

            if (SelectedGenre == null) //nothing was selected
            {
                ViewBag.SelectedGenre = "No genres were selected";
            }
            else
            {
                String strSelectedGenre = "The selected genre(s) is/are: ";

                //get list of genres
                ViewBag.AllGenres = GetAllGenres();

                foreach (int GenreID in SelectedGenre)
                {
                    query = query.Where(s => s.SongGenre.Any(g => g.GenreID == GenreID));
                }
                ViewBag.SelectedGenre = strSelectedGenre;
            }


            if (RatingString != "")
            //make sure string is a valid number
            {
                Decimal decRating;
                try
                {
                    decRating = Convert.ToDecimal(RatingString);
                }
                catch // this code will disolay when something is wrong
                {
                    //Add a message for the viewbag
                    ViewBag.Message = RatingString + "is not valid number. Please try again";

                    //send user back to homepage
                    return(View("SongDetailedSearch"));
                }


                List <SongIndexViewModel> SongsDisplay_descend = new List <SongIndexViewModel>();
                List <SongIndexViewModel> SongsDisplay_ascend  = new List <SongIndexViewModel>();
                foreach (Song a in query)
                {
                    Decimal d = getAverageRating(a.SongID);
                    if (d >= decRating)
                    {
                        SongIndexViewModel ab = new SongIndexViewModel();
                        ab.Song       = a;
                        ab.SongRating = d.ToString("0.0");
                        SongsDisplay_ascend.Add(ab);
                    }
                    else
                    {
                        SongIndexViewModel ab = new SongIndexViewModel();
                        ab.Song       = a;
                        ab.SongRating = d.ToString("0.0");
                        SongsDisplay_descend.Add(ab);
                    }
                }
                IEnumerable <SongIndexViewModel> new_list_songs    = SongsDisplay_ascend;
                IEnumerable <SongIndexViewModel> new_list_songs_lt = SongsDisplay_descend;



                if (SelectedBounds == SortOrder.ascending)
                {
                    ViewBag.SelectedSortOrder = "The records should be sorted in ascending order";
                    return(View("Index", new_list_songs));
                }
                else
                {
                    ViewBag.SelectedSortOrder = "The records should be sored in descending order";
                    return(View("Index", new_list_songs_lt));
                }
            }


            List <SongIndexViewModel> SongsList = new List <SongIndexViewModel>();

            foreach (Song a in query)
            {
                Decimal            d  = getAverageRating(a.SongID);
                SongIndexViewModel ab = new SongIndexViewModel();
                ab.Song       = a;
                ab.SongRating = d.ToString("0.0");
                SongsList.Add(ab);
            }

            return(View("Index", SongsList));
        }
        public async Task <IActionResult> Index()
        {
            var topTenSongs = await _applicationDbContext.Songs
                              .Include(song => song.Album)
                              .ThenInclude(song => song.Band)
                              .IncludeFilter(song => song.SongReviews.Where(review => review.PostDate > DateTime.Now.AddDays(-10)))
                              .OrderByDescending(song => song.SongReviews.Count)
                              .Take(10)
                              .ToListAsync();

            var topTenPodcastEpisodes = await _applicationDbContext.PodcastEpisodes
                                        .Include(pe => pe.Podcast)
                                        .IncludeFilter(pe => pe.PodcastEpisodeReviews.Where(review => review.PostDate > DateTime.Now.AddDays(-10)))
                                        .OrderByDescending(song => song.PodcastEpisodeReviews.Count)
                                        .Take(10)
                                        .ToListAsync();

            var songModels = new List <SongIndexViewModel>();

            foreach (var song in topTenSongs)
            {
                if (song.Hidden)
                {
                    songModels.Add(new SongIndexViewModel
                    {
                        Hidden = true,
                        Id     = song.Id,
                    });
                }
                else
                {
                    var model = new SongIndexViewModel
                    {
                        Id        = song.Id,
                        Hidden    = false,
                        SongTitle = song.Title,
                        Duration  = song.Duration,
                    };

                    if (song.Album != null)
                    {
                        model.AlbumId    = song.AlbumId;
                        model.AlbumTitle = song.Album.Title;
                        if (song.Album.Band != null)
                        {
                            model.BandName = song.Album.Band.Name;
                        }
                    }

                    songModels.Add(model);
                }
            }

            var vm = new HomeIndexViewModel
            {
                TopTenSongs    = songModels,
                TopTenPodcasts = topTenPodcastEpisodes.Select(pe => new PodcastEpisodeIndexViewModel
                {
                    Id          = pe.Id,
                    EpisodeName = pe.EpisodeTitle,
                    Podcast     = pe.Podcast.Name
                })
            };

            return(View(vm));
        }
        public async Task <IActionResult> Index(bool alreadyAdded = false)
        {
            string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var songs = await _songService.GetSongs();

            var bands = await _applicationDbContext.Bands.ToListAsync();

            var albums = await _applicationDbContext.Albums.ToListAsync();

            List <SelectListItem> bandSelectList, albumSelectList;

            ToSelectList(bands, albums, out bandSelectList, out albumSelectList);

            var userPlaylists = await GetUserPlayLists(userId);

            var songModels = new List <SongIndexViewModel>();

            foreach (var song in songs)
            {
                if (song.Hidden)
                {
                    songModels.Add(new SongIndexViewModel
                    {
                        Hidden = true,
                        Id     = song.Id,
                    });
                }
                else
                {
                    var model = new SongIndexViewModel
                    {
                        Id        = song.Id,
                        Hidden    = false,
                        SongTitle = song.Title,
                        Duration  = song.Duration,
                    };

                    if (song.Album != null)
                    {
                        model.AlbumId     = song.AlbumId;
                        model.AlbumTitle  = song.Album.Title;
                        model.ReleaseDate = song.Album.ReleaseDate;
                        if (song.Album.Band != null)
                        {
                            model.BandName = song.Album.Band.Name;
                        }
                    }

                    songModels.Add(model);
                }
            }

            var vm = new MusicIndexViewModel
            {
                Songs        = songModels,
                PlayLists    = userPlaylists,
                BandNames    = bandSelectList,
                AlbumTitles  = albumSelectList,
                AlreadyAdded = alreadyAdded
            };

            return(View(vm));
        }
        public async Task <IActionResult> Index(SongFilterViewModel vm)
        {
            var selectedBand = (await _applicationDbContext.Bands
                                .FirstOrDefaultAsync(band => band.Id == vm.SelectedBand))?.Name ?? "";

            var selectedAlbum = (await _applicationDbContext.Albums
                                 .FirstOrDefaultAsync(album => album.Id == vm.SelectedAlbum))?.Title ?? "";

            if (vm.SelectedAlbum == 0 && vm.SelectedBand == 0)
            {
                return(RedirectToAction("Index"));
            }

            string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var bands = await _applicationDbContext.Bands.ToListAsync();

            var albums = await _applicationDbContext.Albums.ToListAsync();

            List <SelectListItem> bandSelectList, albumSelectList;

            ToSelectList(bands, albums, out bandSelectList, out albumSelectList);

            var songs = await _songService.GetSongs();

            IEnumerable <Song> albumsFiltered = vm.SelectedAlbum != 0
                ? songs.Where(song => song.Album.Title == selectedAlbum)
                : songs;

            IEnumerable <Song> allFiltered = vm.SelectedBand != 0
                ? albumsFiltered.Where(song => song.Album.Band.Name == selectedBand)
                : albumsFiltered;

            var userPlaylists = await GetUserPlayLists(userId);

            var songModels = new List <SongIndexViewModel>();

            foreach (var song in allFiltered)
            {
                if (song.Hidden)
                {
                    songModels.Add(new SongIndexViewModel
                    {
                        Hidden = true,
                        Id     = song.Id,
                    });
                }
                else
                {
                    var model = new SongIndexViewModel
                    {
                        Id        = song.Id,
                        Hidden    = false,
                        SongTitle = song.Title,
                        Duration  = song.Duration,
                    };

                    if (song.Album != null)
                    {
                        model.AlbumId     = song.AlbumId;
                        model.AlbumTitle  = song.Album.Title;
                        model.ReleaseDate = song.Album.ReleaseDate;
                        if (song.Album.Band != null)
                        {
                            model.BandName = song.Album.Band.Name;
                        }
                    }

                    songModels.Add(model);
                }
            }

            var newVm = new MusicIndexViewModel
            {
                Songs       = songModels,
                PlayLists   = userPlaylists,
                BandNames   = bandSelectList,
                AlbumTitles = albumSelectList,
            };

            return(View(newVm));
        }